一、HelloWorld程序
参考:
Spring深入浅出(一),基本概念,HelloWorld实例
二、依赖注入
参考:
Spring深入浅出(五),依赖注入,构造函数/设值/注入内部Bean
Spring深入浅出(六),依赖注入,注入集合(含注入 Bean 引用)
三、Bean的实例化
1. 构造器实例化
Spring深入浅出(一),基本概念,HelloWorld实例
2. 静态工厂方式实例化
2.1 Bean类
package com.itheima.instance.static_factory; public class Bean2 { }
2.2 Factory类
package com.itheima.instance.static_factory; public class MyBean2Factory { //使用自己的工厂创建Bean2实例 public static Bean2 createBean(){ return new Bean2(); } }
2.3 主程序
package com.itheima.instance.static_factory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest2 { public static void main(String[] args) { // 定义配置文件路径 String xmlPath = "com/itheima/instance/static_factory/beans2.xml"; // ApplicationContext在加载配置文件时,对Bean进行实例化 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); System.out.println(applicationContext.getBean("bean2")); } }
2.4 配置文件
<?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/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> <bean id="bean2" class="com.itheima.instance.static_factory.MyBean2Factory" factory-method="createBean" /> </beans>
Bean配置中的class属性所指定的,不是Bean实例的实现类,而是静态工厂类;同时需要使用factory-method属性来指定所创建的静态工厂方法。
3. 实例工厂方式实例化
3.1 Bean类
package com.itheima.instance.factory; public class Bean3 { }
3.2 实例工厂类
package com.itheima.instance.factory; public class MyBean3Factory { public MyBean3Factory() { System.out.println("bean3工厂实例化中"); } //创建Bean3实例的方法 public Bean3 createBean(){ return new Bean3(); } }
3.3 主程序
package com.itheima.instance.factory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest3 { public static void main(String[] args) { // 指定配置文件路径 String xmlPath = "com/itheima/instance/factory/beans3.xml"; // ApplicationContext在加载配置文件时,对Bean进行实例化 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); System.out.println(applicationContext.getBean("bean3")); } }
3.4 配置文件
<?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/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> <!-- 配置工厂 --> <bean id="myBean3Factory" class="com.itheima.instance.factory.MyBean3Factory" /> <!-- 使用factory-bean属性指向配置的实例工厂, 使用factory-method属性确定使用工厂中的哪个方法--> <bean id="bean3" factory-bean="myBean3Factory" factory-method="createBean" /> </beans>
配置文件中,需要实例化的Bean,不是通过class属性直接指向的实例化类,而是通过factory-bean属性指向配置的实例工厂,然后通过factory-method属性确定使用工厂听哪个方法。
四、Bean的作用域
singleton(单例)/prototype(原型)/request/session/globalSession/application/websocket
五、Bean的生命周期
参考:
Spring深入浅出(二),Spring Bean生命周期,3种方法实现Bean初始化回调,以及@PostConstruct注解的配置
Spring深入浅出(三),Spring Bean生命周期,BeanPostProcessor(Spring后置处理器)
六、Bean的装配方式
Bean的装配可以理解为依赖关系注入,Bean的装配方式即Bean依赖注入的方式。Spring支持多种形式的Bean装配方式。如,基于XML的装配、基于注解(Annotation)的装配和自动装配。
1. 基于XML的装配
参考:
Spring深入浅出(五),依赖注入,构造函数/设值/注入内部Bean
Spring深入浅出(六),依赖注入,注入集合(含注入 Bean 引用)
Spring深入浅出(九),注解,@Autowired/@Resource,及其它(@Component/@Repository/@Service/@Controller)
3. 自动装配
参考:
Spring深入浅出(七),自动装配,byName/byType
Spring深入浅出(八),自动装配,constructor
本文参考:《Java EE企业级应用开发教程》