官方文檔: Spring Framework 5
翻譯后文檔: Spring Framework 5 中文文檔
IOC容器
概念:請(qǐng)參考以上文檔。
通俗點(diǎn)說(shuō)法就像是管家(Beans存儲(chǔ)管家)善玫;主要說(shuō)一下Bean的依賴注入
新建項(xiàng)目maven躲模塊項(xiàng)目引入一下依賴
<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>
<!-- 簡(jiǎn)化簡(jiǎn)單實(shí)體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>
創(chuàng)建Bean
@ToString
public class MessageBean {
private static final Logger logger = Logger.getLogger(MessageBean.class);
private String message;
public MessageBean() {
super();
logger.info("==================無(wú)參數(shù)構(gòu)造函數(shù)====================");
}
public MessageBean(String message) {
super();
logger.info("==================有參數(shù)構(gòu)造函數(shù)====================");
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 無(wú)參數(shù)構(gòu)造方法==============");
}
public Animal(String species) {
super();
this.species = species;
logger.info("=============Animal 有參數(shù)構(gòu)造方法==============");
}
}
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 無(wú)參數(shù)構(gòu)造方法==============");
}
public Bird(String species) {
super(species);
logger.info("=============Bird 有參數(shù)構(gòu)造方法==============");
}
}
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 無(wú)參數(shù)構(gòu)造方法==============");
}
public Dog(String species) {
super(species);
logger.info("=============Dog 有參數(shù)構(gòu)造方法==============");
}
}
XML示例:構(gòu)造函數(shù) 和 工廠模式
創(chuàng)建工廠
public interface AnimalFactory {
Animal build(String className);
}
工廠實(shí)現(xiàn)
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">
<!-- 無(wú)參構(gòu)造函數(shù) -->
<bean id="messageBean_1" class="bertram.wang.beans.MessageBean"></bean>
<!-- 無(wú)參構(gòu)造函數(shù) + set屬性 -->
<bean id="messageBean_2" class="bertram.wang.beans.MessageBean">
<property name="message" value="set -- 發(fā)送一條消息"></property>
</bean>
<!-- 有參構(gòu)造函數(shù) -->
<bean id="messageBean_3" class="bertram.wang.beans.MessageBean">
<constructor-arg name="message" value="發(fā)送一條消息"></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();
}
}
執(zhí)行日志
[INFO ] 2019-06-06 14:09:16,942 method:bertram.wang.beans.MessageBean.<init>(MessageBean.java:19)
==================無(wú)參數(shù)構(gòu)造函數(shù)====================
[INFO ] 2019-06-06 14:09:16,954 method:bertram.wang.beans.MessageBean.<init>(MessageBean.java:19)
==================無(wú)參數(shù)構(gòu)造函數(shù)====================
[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)
==================有參數(shù)構(gòu)造函數(shù)====================
[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 -- 發(fā)送一條消息)
[INFO ] 2019-06-06 14:09:16,987 method:bertram.wang.MainApp.main(MainApp.java:32)
messageBean_3===============MessageBean(message=發(fā)送一條消息)
[INFO ] 2019-06-06 14:09:16,989 method:bertram.wang.beans.Animal.<init>(Animal.java:24)
=============Animal 無(wú)參數(shù)構(gòu)造方法==============
[INFO ] 2019-06-06 14:09:16,989 method:bertram.wang.beans.Dog.<init>(Dog.java:20)
=============Dog 無(wú)參數(shù)構(gòu)造方法==============
[INFO ] 2019-06-06 14:09:16,990 method:bertram.wang.beans.Animal.<init>(Animal.java:24)
=============Animal 無(wú)參數(shù)構(gòu)造方法==============
[INFO ] 2019-06-06 14:09:16,990 method:bertram.wang.beans.Bird.<init>(Bird.java:19)
=============Bird 無(wú)參數(shù)構(gòu)造方法==============
[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();
}
}