概述、体系结构
Spring IoC容器
Inverse Of Controller即为控制反转,简称IOC IOC反转了依赖关系的满足方式,由之前的自己创建依赖关系,变为由工厂推送
IOC有两个不同的类型BeanFactory容器和ApplicationContext容器
ApplicationContext包含BeanFactory,该容器添加了更多企业特定的功能 例如从一个属性文件中解析文本信息的能力,发布应用程序事件给感兴趣的事件监听器的能力 通常不建议使用BeanFactory。BeanFactory 仍然可以用于轻量级的应用程序,如移动设备或基于 applet 的应用程序,其中它的数据量和速度是显著。
BeanFactory容器
// hello.javapublic class hello {private String message;public void setMessage(String message){this.message = message;}public void getMessage(){System.out.println("Your Message : " + message);}}// main.javapublic class main {public static void main(String[] args) {XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("Beans.xml"));HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");obj.getMessage();}}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="hello" class="com.tutorialspoint.hello"><property name="message" value="Hello World!"/></bean></beans>
ApplicationContext容器
// hello.javapublic class hello {private String message;public void setMessage(String message){this.message = message;}public void getMessage(){System.out.println("Your Message : " + message);}}// main.javapublic class main {public static void main(String[] args) {ApplicationContext context = new FileSystemXmlApplicationContext("C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml");HelloWorld obj = (HelloWorld) context.getBean("helloWorld");obj.getMessage();}}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="hello" class="com.tutorialspoint.hello"><property name="message" value="Hello World!"/></bean></beans>
Bean的对象
bean对象是由Spring IoC容器管理的,bean定义包含成为配置元数据
- bean生命周期
- bean的定义
- bean的初始化
init-method="initMethod" - bean的使用
- bean的销毁
destroy-method="destroyMethod"
- bean后置配置器生命周期
- 使用接口
BeanPostProcessor
- 使用接口
- bean的定义
- 后置配置器初始化前调用
postProcessBeforeInitialization - bean的初始化
init-method="initMethod" - 后置配置器初始化后调用
postProcessAfterInitialization - bean的使用
- bean的销毁
destroy-method="destroyMethod"Spring 依赖注入
通过依赖注入的方式管理bean之间的依赖关系 依赖注入就是将互相有关联性的java类通过依赖注入的方式,让它们粘合起来 保持独立的同时,又能相互关联 例如:Controller->Service->Dao
<bean id="userController" class="com.atguigu.Controller"><constructor-arg ref="userService"/></bean><bean id="userService" class="com.atguigu.Service"><constructor-arg ref="userDao"/></bean><bean id="userDao" class="com.atguigu.Dao"><property name="name" value="张三"/></bean>
Spring Beans 自动装配
Spring 基于注解的配置
// 注解方式IOC@Compoonent用在三层结构之外的其它要进行IOC的类上@Controller用在表现层要进行IOC的类上@Service用在业务层要进行IOC的类上@Repository用在持久层要进行IOC的类上@Required注入Bean类型的对象(IOC 容器中的对象)它默认是按照类型进行自动装配,要求IOC容器中只有一个要注入的类型的对象// 注解方式DI@Value注入简单类型的数据值@PropertySource引入外部的properties文件// 自动装配@AutowiredbyType 查找Bean如果能找到唯一的Bean,则将其注入进来如果找不到Bean, 则报错如果找到了多个该类型的Bean,那么就会byName 查找BeanbyName 查找Bean如果没有结合Qualifier注解指定Bean的id/name: 根据属性名匹配Bean的id/name,如果能匹配到唯一Bean则注入,匹配不到则报错如果集合Qualifier注解一起使用: 则直接byName 查找Bean,根据Qualifier注解中配置的name/id查找Bean@Resource如果同时配置了name和type属性: 就根据name和type属性查找唯一的Bean,能找到就注入,不能找到就报错如果只配置了name属性: 就根据name属性查找为一的Bean,能找到就注入,不能找到就报错如果只配置了type属性: 就根据type属性查找唯一的Bean,能找到就注入,不能找到就报错,如果能找到多个则byName查找Bean(属性名与Bean的id匹配)如果既没有配置type属性,又没有配置name属性:先byName: 根据属性名对应Bean的id查找唯一的Bean,能找到则注入,不能找到则会byType查找byType: 根据类型查找唯一的Bean@Qualifier@PostConstruct@PreDestroy// 面向切面的编程(AOP)框架@Pointcut声明切入点,其他类可以引用这个切入点,易于维护@Aspect前置通知@AfterReturning返回通知@AfterThrowing异常通知@After后置通知@Around环绕通知@Before
四种包扫描方法
1. 1. 基本扫描<context:component-scan base-package="com.atguigu"></context:component-scan>2. 扫描具体规则<context:component-scan base-package="com.atguigu" resource-pattern="*Impl.class"></context:component-scan>3. 排除某个注解<context:component-scan base-package="com.atguigu"><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller"/></context:component-scan>4. 针对性扫描,扫描指定包以及后代包下的IOC注解<context:component-scan base-package="com.atguigu" use-default-filters="false"><context:include-filter type="annotation"expression="org.springframework.stereotype.Controller"/></context:component-scan>
Spring 框架的AOP
Spring JDBC框架
Spring 事务管理
Spring Web MVC框架

