官方文档: Spring Framework 5
翻译后文档: Spring Framework 5 中文文档
IOC容器
概念:请参考以上文档。
通俗点说法就像是管家(Beans存储管家);主要说一下Bean的依赖注入
新建项目maven躲模块项目引入一下依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 简化简单实体getter,setter -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
创建Bean
@ToString
public class MessageBean {
private static final Logger logger = Logger.getLogger(MessageBean.class);
private String message;
public MessageBean() {
super();
logger.info("==================无参数构造函数====================");
}
public MessageBean(String message) {
super();
logger.info("==================有参数构造函数====================");
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
logger.info("==================setMessage====================");
}
}
@ToString
public abstract class Animal {
private static final Logger logger = Logger.getLogger(Animal.class);
private String species;
public abstract void talk();
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
public Animal() {
super();
logger.info("=============Animal 无参数构造方法==============");
}
public Animal(String species) {
super();
this.species = species;
logger.info("=============Animal 有参数构造方法==============");
}
}
public class Bird extends Animal {
private static final Logger logger = Logger.getLogger(Bird.class);
@Override
public void talk() {
logger.info("=============Bird.talk==============");
}
public Bird() {
super();
logger.info("=============Bird 无参数构造方法==============");
}
public Bird(String species) {
super(species);
logger.info("=============Bird 有参数构造方法==============");
}
}
public class Dog extends Animal {
private static final Logger logger = Logger.getLogger(Dog.class);
@Override
public void talk() {
logger.info("=============Dog.talk==============");
}
public Dog() {
super();
logger.info("=============Dog 无参数构造方法==============");
}
public Dog(String species) {
super(species);
logger.info("=============Dog 有参数构造方法==============");
}
}
XML示例:构造函数 和 工厂模式
创建工厂
public interface AnimalFactory {
Animal build(String className);
}
工厂实现
public class AnimalFactoryImpl implements AnimalFactory {
private static final Logger logger = Logger.getLogger(AnimalFactoryImpl.class);
@Override
public Animal build(String className) {
try {
Class<?> clazz = Class.forName(className);
Object instance = clazz.newInstance();
if (instance instanceof Animal) {
return (Animal) instance;
}
} catch (Exception e) {
logger.error(e.getMessage());
}
return null;
}
}
XML示例
<?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.xsd">
<!-- 无参构造函数 -->
<bean id="messageBean_1" class="bertram.wang.beans.MessageBean"></bean>
<!-- 无参构造函数 + set属性 -->
<bean id="messageBean_2" class="bertram.wang.beans.MessageBean">
<property name="message" value="set -- 发送一条消息"></property>
</bean>
<!-- 有参构造函数 -->
<bean id="messageBean_3" class="bertram.wang.beans.MessageBean">
<constructor-arg name="message" value="发送一条消息"></constructor-arg>
</bean>
<bean id="animalFactory" class="bertram.wang.beans.interfaces.AnimalFactoryImpl" ></bean>
<bean id="dog" factory-bean="animalFactory" factory-method="build">
<constructor-arg value = "bertram.wang.beans.Dog"/>
</bean>
<bean id="bird" factory-bean="animalFactory" factory-method="build">
<constructor-arg value = "bertram.wang.beans.Bird"/>
</bean>
</beans>
程序入口
public class MainApp {
private static final Logger logger = Logger.getLogger(MainApp.class);
public static void main(String[] args) {
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(new DefaultListableBeanFactory());
reader.loadBeanDefinitions(new ClassPathResource("beans/message-bean.xml"));
reader.loadBeanDefinitions(new ClassPathResource("beans/animal-bean.xml"));
BeanFactory beanFactory = (BeanFactory)reader.getBeanFactory();
MessageBean messageBean_1 = beanFactory.getBean("messageBean_1", MessageBean.class);
MessageBean messageBean_2 = beanFactory.getBean("messageBean_2", MessageBean.class);
MessageBean messageBean_3 = beanFactory.getBean("messageBean_3", MessageBean.class);
logger.info("messageBean_1===============" + messageBean_1.toString());
logger.info("messageBean_2===============" + messageBean_2.toString());
logger.info("messageBean_3===============" + messageBean_3.toString());
Dog dog = beanFactory.getBean("dog", Dog.class);
Bird bird = beanFactory.getBean("bird", Bird.class);
dog.talk();
bird.talk();
}
}
执行日志
[INFO ] 2019-06-06 14:09:16,942 method:bertram.wang.beans.MessageBean.<init>(MessageBean.java:19)
==================无参数构造函数====================
[INFO ] 2019-06-06 14:09:16,954 method:bertram.wang.beans.MessageBean.<init>(MessageBean.java:19)
==================无参数构造函数====================
[INFO ] 2019-06-06 14:09:16,974 method:bertram.wang.beans.MessageBean.setMessage(MessageBean.java:34)
==================setMessage====================
[INFO ] 2019-06-06 14:09:16,986 method:bertram.wang.beans.MessageBean.<init>(MessageBean.java:24)
==================有参数构造函数====================
[INFO ] 2019-06-06 14:09:16,986 method:bertram.wang.MainApp.main(MainApp.java:30)
messageBean_1===============MessageBean(message=null)
[INFO ] 2019-06-06 14:09:16,986 method:bertram.wang.MainApp.main(MainApp.java:31)
messageBean_2===============MessageBean(message=set -- 发送一条消息)
[INFO ] 2019-06-06 14:09:16,987 method:bertram.wang.MainApp.main(MainApp.java:32)
messageBean_3===============MessageBean(message=发送一条消息)
[INFO ] 2019-06-06 14:09:16,989 method:bertram.wang.beans.Animal.<init>(Animal.java:24)
=============Animal 无参数构造方法==============
[INFO ] 2019-06-06 14:09:16,989 method:bertram.wang.beans.Dog.<init>(Dog.java:20)
=============Dog 无参数构造方法==============
[INFO ] 2019-06-06 14:09:16,990 method:bertram.wang.beans.Animal.<init>(Animal.java:24)
=============Animal 无参数构造方法==============
[INFO ] 2019-06-06 14:09:16,990 method:bertram.wang.beans.Bird.<init>(Bird.java:19)
=============Bird 无参数构造方法==============
[INFO ] 2019-06-06 14:09:16,990 method:bertram.wang.beans.Dog.talk(Dog.java:16)
=============Dog.talk==============
[INFO ] 2019-06-06 14:09:16,990 method:bertram.wang.beans.Bird.talk(Bird.java:15)
=============Bird.talk==============
居于JAVA注入
@Configuration
public class BeanConfig {
@Bean
public AnimalFactory animalFactory() {
return new AnimalFactoryImpl();
}
@Bean
public Animal dog(AnimalFactory animalFactory) {
return animalFactory.build("bertram.wang.beans.Dog");
}
@Bean
public Animal bird(AnimalFactory animalFactory) {
return animalFactory.build("bertram.wang.beans.Bird");
}
}
程序入口
public class MainApp {
private static final Logger logger = Logger.getLogger(MainApp.class);
@SuppressWarnings("resource")
public static void main(String[] args) {
//ApplicationContext beanFactory = new ClassPathXmlApplicationContext("beans/message-bean.xml", "beans/animal-bean.xml");
ApplicationContext beanFactory = new AnnotationConfigApplicationContext(BeanConfig.class);
/*MessageBean messageBean_1 = beanFactory.getBean("messageBean_1", MessageBean.class);
MessageBean messageBean_2 = beanFactory.getBean("messageBean_2", MessageBean.class);
MessageBean messageBean_3 = beanFactory.getBean("messageBean_3", MessageBean.class);
logger.info("messageBean_1===============" + messageBean_1.toString());
logger.info("messageBean_2===============" + messageBean_2.toString());
logger.info("messageBean_3===============" + messageBean_3.toString());*/
Dog dog = beanFactory.getBean("dog", Dog.class);
Bird bird = beanFactory.getBean("bird", Bird.class);
dog.talk();
bird.talk();
}
}