版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
目录
一、实现功能
通过五种方式,注入到对应的Bean实例。通过构造方法、set方法、集合类的注入、null值注入、注入时创建内部Bean。
二、准备环境
1.pom.xml文件
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
2.Spring.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"
default-autowire="byName"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
</beans>
三、实现步骤
1.Singleton
(1)每次项上下文context请求该bean,都是只创建唯一一个Bean,然后会被注入到需要他的地方
(2)具体类
-》Bean2
package com.spring.ioc;
/**
* Created by Administrator on 2019/10/26.
*/
public class Bean2 {
}
-》Bean1
package com.spring.ioc;
/**
* Created by Administrator on 2019/10/26.
*/
public class Bean1 {
private Bean2 bean2;
public Bean2 getBean2() {
return bean2;
}
public void setBean2(Bean2 bean2) {
this.bean2 = bean2;
}
@Override
public String toString() {
return "Bean1{" +
"bean2=" + bean2 +
'}';
}
}
(3)spring.xml:设置Bean2为单例模式,Bean1不设置(默认是单例模式)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p ="http://www.springframework.org/schema/p"
default-autowire="byName"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean class="com.spring.ioc.Bean2" name="bean2" scope="singleton"/>
<bean class="com.spring.ioc.Bean1" name="bean1">
<property name="bean2" ref="bean2"/>
</bean>
</beans>
(4)测试代码:单个上下文环境,则Bean2都是一个实例
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2019/10/26.
*/
public class testCode {
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Bean2 bean2_1=context.getBean("bean2",Bean2.class);
System.out.println("bean2_1 = " + bean2_1);
Bean2 bean2_2=context.getBean("bean2",Bean2.class);
System.out.println("bean2_2 = " + bean2_2);
Bean1 bean1=context.getBean("bean1",Bean1.class);
System.out.println("bean1 = " + bean1);
}
}
结果:
bean2_1 = com.spring.ioc.Bean2@10bbd20a
bean2_2 = com.spring.ioc.Bean2@10bbd20a
bean1 = Bean1{bean2=com.spring.ioc.Bean2@10bbd20a}
(5)测试代码:多个个上下文环境,则Bean2每一个环境是一个实例
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2019/10/26.
*/
public class testCode {
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Bean2 bean2_1=context.getBean("bean2",Bean2.class);
System.out.println("bean2_1 = " + bean2_1);
Bean2 bean2_2=context.getBean("bean2",Bean2.class);
System.out.println("bean2_2 = " + bean2_2);
Bean1 bean1=context.getBean("bean1",Bean1.class);
System.out.println("bean1 = " + bean1);
ApplicationContext context1=new ClassPathXmlApplicationContext("spring.xml");
Bean2 bean2_3=context1.getBean("bean2",Bean2.class);
System.out.println("bean2_3 = " + bean2_3);
Bean2 bean2_4=context1.getBean("bean2",Bean2.class);
System.out.println("bean2_4 = " + bean2_4);
}
}
结果:
bean2_1 = com.spring.ioc.Bean2@10bbd20a
bean2_2 = com.spring.ioc.Bean2@10bbd20a
bean1 = Bean1{bean2=com.spring.ioc.Bean2@10bbd20a}
bean2_3 = com.spring.ioc.Bean2@15d0c81b
bean2_4 = com.spring.ioc.Bean2@15d0c81b
2.prototype
(1)每次项上下文context请求该bean,都会创建一个新的Bean,然后会被注入到需要他的地方
(2)spring.xml实现
(3)测试代码:每次请求Bean2,都会新建一个新的实例
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2019/10/26.
*/
public class testCode {
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Bean2 bean2_1=context.getBean("bean2",Bean2.class);
System.out.println("bean2_1 = " + bean2_1);
Bean2 bean2_2=context.getBean("bean2",Bean2.class);
System.out.println("bean2_2 = " + bean2_2);
Bean1 bean1=context.getBean("bean1",Bean1.class);
System.out.println("bean1 = " + bean1);
// ApplicationContext context1=new ClassPathXmlApplicationContext("spring.xml");
// Bean2 bean2_3=context1.getBean("bean2",Bean2.class);
// System.out.println("bean2_3 = " + bean2_3);
// Bean2 bean2_4=context1.getBean("bean2",Bean2.class);
// System.out.println("bean2_4 = " + bean2_4);
}
}
结果
bean2_1 = com.spring.ioc.Bean2@10bbd20a
bean2_2 = com.spring.ioc.Bean2@48503868
bean1 = Bean1{bean2=com.spring.ioc.Bean2@6895a785}
3.Bean1和Bean2的单例和多例模式组合
备注:bean1一个属性为bean2,即bean1依赖于bean2
|
Bean1作用域 |
||
Singleton |
prototype |
||
Bean2作用域
|
Singleton |
Bean1单实例 Bean2单实例 |
Bean1多实例 Bean2单实例 |
prototype |
Bean1单实例 Bean2单实例 |
Bean1多实例 Bean2多实例 |
4.特例:Bean2多例,Bean1为单例
(1)具体场景
Bean1一个属性为Bean2,Bean1为单例,Bean2为多例。每次调用Bean1的方法时,拿到的Bean2都是一个新的实例。解决方法:方法注入
(2)修改Bean1代码,通过方法定义Bean2
package com.spring.ioc;
/**
* Created by Administrator on 2019/10/26.
*/
public abstract class Bean1 {
protected abstract Bean2 createBean2();
public void pringBean2(){
System.out.println("bean2 = " + createBean2());
}
}
(3)修改spring.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"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p ="http://www.springframework.org/schema/p"
default-autowire="byName"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean class="com.spring.ioc.Bean2" name="bean2" scope="prototype"/>
<bean class="com.spring.ioc.Bean1" name="bean1">
<!--<property name="bean2" ref="bean2"/>-->
<lookup-method name="createBean2" bean="bean2"/>
</bean>
</beans>
(3)测试代码
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2019/10/26.
*/
public class testCode {
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Bean1 bean1=context.getBean("bean1",Bean1.class);
bean1.pringBean2();
bean1.pringBean2();
bean1.pringBean2();
}
}
结果:
bean2 = com.spring.ioc.Bean2@5204062d
bean2 = com.spring.ioc.Bean2@4fcd19b3
bean2 = com.spring.ioc.Bean2@376b4233
四、总结
1.上传代码到github~