SpringIoc注解注入Bean
构造方法注入
pom
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
实例对象
package com.example.springboot02.class14;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:25
* @Modified By:
*/
@Component
public class MyBean {
private AnotherBean anotherBean1;
/**
* 注入实例
* @param anotherBean1
*/
@Autowired
public MyBean(AnotherBean anotherBean1) {
this.anotherBean1 = anotherBean1;
}
@Override
public String toString() {
return "MyBean{" +
"anotherBean1=" + anotherBean1 +
'}';
}
}
package com.example.springboot02.class14;
import org.springframework.stereotype.Component;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:25
* @Modified By:
*/
@Component
public class AnotherBean {
}
package com.example.springboot02.class14;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:27
* @Modified By:
*/
@Configuration
@ComponentScan("com.example.springboot02.class14")
public class MyConfigruation {
}
测试类
package com.example.springboot02.class14;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:28
* @Modified By:
*/
public class class14 {
@Test
public void test(){
ApplicationContext context=new AnnotationConfigApplicationContext(MyConfigruation.class);
MyBean myBean=context.getBean("myBean",MyBean.class);
System.out.println("myBean="+myBean);
}
}
结果
set方法注入
实例对象
package com.example.springboot02.class14;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:25
* @Modified By:
*/
@Component
public class MyBean {
private AnotherBean anotherBean1;
private AnotherBean anotherBean2;
/**
* set方法注入
* @param anotherBean2
*/
@Autowired
public void setAnotherBean2(AnotherBean anotherBean2) {
this.anotherBean2 = anotherBean2;
}
/**
* 注入实例
* @param anotherBean1
*/
@Autowired
public MyBean(AnotherBean anotherBean1) {
this.anotherBean1 = anotherBean1;
}
@Override
public String toString() {
return "MyBean{" +
"anotherBean1=" + anotherBean1 +
", anotherBean2=" + anotherBean2 +
'}';
}
}
测试类
package com.example.springboot02.class14;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:28
* @Modified By:
*/
public class class14 {
@Test
public void test(){
ApplicationContext context=new AnnotationConfigApplicationContext(MyConfigruation.class);
MyBean myBean=context.getBean("myBean",MyBean.class);
System.out.println("myBean="+myBean);
}
}
结果
私有方法注入
package com.example.springboot02.class14;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:25
* @Modified By:
*/
@Component
public class MyBean {
private AnotherBean anotherBean1;
private AnotherBean anotherBean2;
/**
* 私有化属性注入 不需要get和set方法
*/
@Autowired
private AnotherBean anotherBean3;
/**
* set方法注入
* @param anotherBean2
*/
@Autowired
public void setAnotherBean2(AnotherBean anotherBean2) {
this.anotherBean2 = anotherBean2;
}
/**
* 注入实例
* @param anotherBean1
*/
@Autowired
public MyBean(AnotherBean anotherBean1) {
this.anotherBean1 = anotherBean1;
}
@Override
public String toString() {
return "MyBean{" +
"anotherBean1=" + anotherBean1 +
", anotherBean2=" + anotherBean2 +
", anotherBean3=" + anotherBean3 +
'}';
}
}
结果
List注入
修改实例对象属性 新增list属性
package com.example.springboot02.class14;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:25
* @Modified By:
*/
@Component
public class MyBean {
private AnotherBean anotherBean1;
private AnotherBean anotherBean2;
/**
* 私有化属性注入 不需要get和set方法
*/
@Autowired
private AnotherBean anotherBean3;
private List<String> stringList;
public List<String> getStringList() {
return stringList;
}
@Autowired
public void setStringList(List<String> stringList) {
this.stringList = stringList;
}
/**
* set方法注入
* @param anotherBean2
*/
@Autowired
public void setAnotherBean2(AnotherBean anotherBean2) {
this.anotherBean2 = anotherBean2;
}
/**
* 注入实例
* @param anotherBean1
*/
@Autowired
public MyBean(AnotherBean anotherBean1) {
this.anotherBean1 = anotherBean1;
}
@Override
public String toString() {
return "MyBean{" +
"anotherBean1=" + anotherBean1 +
", anotherBean2=" + anotherBean2 +
", anotherBean3=" + anotherBean3 +
", stringList=" + stringList +
'}';
}
}
注入类
package com.example.springboot02.class14;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:27
* @Modified By:
*/
@Configuration
@ComponentScan("com.example.springboot02.class14")
public class MyConfigruation {
/**
* 交给Spring管理的javabean
* @return
*/
@Bean
public List<String> stringList(){
List<String> list = new ArrayList<String>();
list.add("111");
list.add("2222");
return list;
}
}
测试类
package com.example.springboot02.class14;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:28
* @Modified By:
*/
public class class14 {
@Test
public void test(){
ApplicationContext context=new AnnotationConfigApplicationContext(MyConfigruation.class);
MyBean myBean=context.getBean("myBean",MyBean.class);
System.out.println("myBean="+myBean);
for (String s:myBean.getStringList()){
System.out.println("s ="+s);
}
}
}
结果
也可以使用
package com.example.springboot02.class14;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:27
* @Modified By:
*/
@Configuration
@ComponentScan("com.example.springboot02.class14")
public class MyConfigruation {
/**
* 交给Spring管理的javabean
* @return
*/
/* @Bean
public List<String> stringList(){
List<String> list = new ArrayList<String>();
list.add("111");
list.add("2222");
return list;
}*/
@Bean
public String string1(){
return "333";
}
@Bean
public String string2(){
return "444";
}
}
结果
就是说,如果定义注入的是list的话,那么spring会找到所有返回属性和list中类型一致的方法,进行注入
如果两种方式都放开执行
package com.example.springboot02.class14;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:27
* @Modified By:
*/
@Configuration
@ComponentScan("com.example.springboot02.class14")
public class MyConfigruation {
/**
* 交给Spring管理的javabean
* @return
*/
@Bean
public List<String> stringList(){
List<String> list = new ArrayList<String>();
list.add("111");
list.add("2222");
return list;
}
@Bean
public String string1(){
return "333";
}
@Bean
public String string2(){
return "444";
}
}
结果输出
如果想要指定输出的bean的话 就得设置beanid了
注入的地方也需要设置id
再执行的结果就是
设置list输出顺序
@Order注解 不需要从0开始,也不用设置连续的
package com.example.springboot02.class14;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:27
* @Modified By:
*/
@Configuration
@ComponentScan("com.example.springboot02.class14")
public class MyConfigruation {
/**
* 交给Spring管理的javabean
* @return
*/
/* @Bean(value = "stringList")
public List<String> stringList(){
List<String> list = new ArrayList<String>();
list.add("111");
list.add("2222");
return list;
}*/
@Bean
@Order(33)
public String string1(){
return "333";
}
@Bean
@Order(22)
public String string2(){
return "444";
}
}
结果
map注入
新增map属性值
set方法加@Autowired注入
package com.example.springboot02.class14;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:25
* @Modified By:
*/
@Component
public class MyBean {
private AnotherBean anotherBean1;
private AnotherBean anotherBean2;
/**
* 私有化属性注入 不需要get和set方法
*/
@Autowired
private AnotherBean anotherBean3;
private List<String> stringList;
private Map<String,String> map;
public Map<String, String> getMap() {
return map;
}
@Autowired
public void setMap(Map<String, String> map) {
this.map = map;
}
public List<String> getStringList() {
return stringList;
}
@Autowired
//@Qualifier("stringList")
public void setStringList(List<String> stringList) {
this.stringList = stringList;
}
/**
* set方法注入
* @param anotherBean2
*/
@Autowired
public void setAnotherBean2(AnotherBean anotherBean2) {
this.anotherBean2 = anotherBean2;
}
/**
* 注入实例
* @param anotherBean1
*/
@Autowired
public MyBean(AnotherBean anotherBean1) {
this.anotherBean1 = anotherBean1;
}
@Override
public String toString() {
return "MyBean{" +
"anotherBean1=" + anotherBean1 +
", anotherBean2=" + anotherBean2 +
", anotherBean3=" + anotherBean3 +
", stringList=" + stringList +
'}';
}
}
实例对象
package com.example.springboot02.class14;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:27
* @Modified By:
*/
@Configuration
@ComponentScan("com.example.springboot02.class14")
public class MyConfigruation {
/**
* 交给Spring管理的javabean
* @return
*/
/* @Bean(value = "stringList")
public List<String> stringList(){
List<String> list = new ArrayList<String>();
list.add("111");
list.add("2222");
return list;
}*/
@Bean
public Map<String,String> map(){
Map<String,String> map = new HashMap<String,String>();
map.put("111","111");
map.put("222","222");
return map;
}
@Bean
@Order(33)
public String string1(){
return "333";
}
@Bean
@Order(22)
public String string2(){
return "444";
}
}
测试类
package com.example.springboot02.class14;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.Map;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:28
* @Modified By:
*/
public class class14 {
@Test
public void test(){
ApplicationContext context=new AnnotationConfigApplicationContext(MyConfigruation.class);
MyBean myBean=context.getBean("myBean",MyBean.class);
System.out.println("myBean="+myBean);
for (String s:myBean.getStringList()){
System.out.println("s ="+s);
}
for (Map.Entry<String,String> map:myBean.getMap().entrySet()) {
System.out.println("map="+map);
}
}
}
结果如下:
简单类型注入
测试类
package com.example.springboot02.class14;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.Map;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:28
* @Modified By:
*/
public class class14 {
@Test
public void test(){
ApplicationContext context=new AnnotationConfigApplicationContext(MyConfigruation.class);
MyBean myBean=context.getBean("myBean",MyBean.class);
System.out.println("myBean="+myBean);
for (String s:myBean.getStringList()){
System.out.println("s ="+s);
}
for (Map.Entry<String,String> map:myBean.getMap().entrySet()) {
System.out.println("map="+map);
}
System.out.println("string="+myBean.getString());
}
}
结果:
ApplicationContext注入
新增ApplicationContext属性
package com.example.springboot02.class14;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:25
* @Modified By:
*/
@Component
public class MyBean {
private AnotherBean anotherBean1;
private AnotherBean anotherBean2;
/**
* 私有化属性注入 不需要get和set方法
*/
@Autowired
private AnotherBean anotherBean3;
private List<String> stringList;
private Map<String,String> map;
private String string;
private ApplicationContext context;
public ApplicationContext getContext() {
return context;
}
@Autowired
public void setContext(ApplicationContext context) {
this.context = context;
}
public String getString() {
return string;
}
@Value("555")
public void setString(String string) {
this.string = string;
}
public Map<String, String> getMap() {
return map;
}
@Autowired
public void setMap(Map<String, String> map) {
this.map = map;
}
public List<String> getStringList() {
return stringList;
}
@Autowired
//@Qualifier("stringList")
public void setStringList(List<String> stringList) {
this.stringList = stringList;
}
/**
* set方法注入
* @param anotherBean2
*/
@Autowired
public void setAnotherBean2(AnotherBean anotherBean2) {
this.anotherBean2 = anotherBean2;
}
/**
* 注入实例
* @param anotherBean1
*/
@Autowired
public MyBean(AnotherBean anotherBean1) {
this.anotherBean1 = anotherBean1;
}
@Override
public String toString() {
return "MyBean{" +
"anotherBean1=" + anotherBean1 +
", anotherBean2=" + anotherBean2 +
", anotherBean3=" + anotherBean3 +
", stringList=" + stringList +
'}';
}
}
测试类
package com.example.springboot02.class14;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.Map;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 17:28
* @Modified By:
*/
public class class14 {
@Test
public void test(){
ApplicationContext context=new AnnotationConfigApplicationContext(MyConfigruation.class);
MyBean myBean=context.getBean("myBean",MyBean.class);
System.out.println("myBean="+myBean);
for (String s:myBean.getStringList()){
System.out.println("s ="+s);
}
for (Map.Entry<String,String> map:myBean.getMap().entrySet()) {
System.out.println("map="+map);
}
System.out.println("string="+myBean.getString());
AnotherBean anotherBean=myBean.getContext().getBean("anotherBean",AnotherBean.class);
System.out.println("anotherBean="+anotherBean);
}
}
结果
通过注解设置bean作用域 使用@Scope注解
pom
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
实例化类
package com.example.springboot02.class15;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 18:44
* @Modified By:
*/
@Component("test2")
@Scope("prototype")
public class TestBean {
}
package com.example.springboot02.class15;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 18:45
* @Modified By:
*/
@Configuration
@ComponentScan("com.example.springboot02.class15")
public class TestConfiguration {
@Bean(value = "test1")
@Scope("prototype")
public TestBean testBean(){
return new TestBean();
}
}
测试类
package com.example.springboot02.class15;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 18:48
* @Modified By:
*/
public class Class15 {
@Test
public void test(){
ApplicationContext context=new AnnotationConfigApplicationContext(TestConfiguration.class);
for (int i=0;i<5;i++){
TestBean testBean1=context.getBean("test1",TestBean.class);
System.out.println("testBean1="+testBean1);
}
System.out.println("==========================");
for (int i=0;i<5;i++){
TestBean testBean2=context.getBean("test2",TestBean.class);
System.out.println("testBean2="+testBean2);
}
}
}
结果
自定义作用域
新增作用域实现类
package com.example.springboot02.class15;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;
import org.springframework.lang.Nullable;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 15:23
* @Modified By: 自定义的双利模式作用域 需要实现Scope接口 注意:import org.springframework.beans.factory.config.Scope包中的
*/
public class MyScope implements Scope {
private Map<String,Object> map1=new ConcurrentHashMap<String,Object>();
private Map<String,Object> map2=new ConcurrentHashMap<String,Object>();
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
//如果map1不包含这个实例
if(!map1.containsKey(name)){
//我们就获取这个实例并保存
Object o=objectFactory.getObject();
map1.put(name,o);
return o;
}
if(!map2.containsKey(name)){
//我们就获取这个实例并保存
Object o=objectFactory.getObject();
map2.put(name,o);
return o;
}
int i=new Random().nextInt(2);
//随机返回一个实例
if(i==0){
return map1.get(name);
}else{
return map2.get(name);
}
}
@Nullable
@Override
public Object remove(String name) {
//如果map2包含这个实例 我们就要删除这个实例
if(map2.containsKey(name)){
Object o=map2.get(name);
map2.remove(name);
return o;
}
//如果map1包含这个实例 我们就要删除这个实例
if(map1.containsKey(name)){
Object o=map1.get(name);
map1.remove(name);
return o;
}
//如果都包含,我们就返回null
return null;
}
@Override
public void registerDestructionCallback(String name, Runnable runnable) {
}
@Nullable
@Override
public Object resolveContextualObject(String name) {
return null;
}
@Nullable
@Override
public String getConversationId() {
return null;
}
}
实例化对象
package com.example.springboot02.class15;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 18:44
* @Modified By:
*/
@Component("test2")
@Scope("myScope")
public class TestBean {
}
package com.example.springboot02.class15;
import org.springframework.beans.factory.config.CustomScopeConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 18:45
* @Modified By:
*/
@Configuration
@ComponentScan("com.example.springboot02.class15")
public class TestConfiguration {
@Bean(value = "test1")
@Scope("myScope")
public TestBean testBean(){
return new TestBean();
}
@Bean
public MyScope myScope(){
return new MyScope();
}
@Bean
public CustomScopeConfigurer customScopeConfigurer(){
CustomScopeConfigurer configurer=new CustomScopeConfigurer();
configurer.addScope("myScope",myScope());
return configurer;
}
}
测试类
package com.example.springboot02.class15;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 18:48
* @Modified By:
*/
public class Class15 {
@Test
public void test(){
ApplicationContext context=new AnnotationConfigApplicationContext(TestConfiguration.class);
for (int i=0;i<5;i++){
TestBean testBean1=context.getBean("test1",TestBean.class);
System.out.println("testBean1="+testBean1);
}
System.out.println("==========================");
for (int i=0;i<5;i++){
TestBean testBean2=context.getBean("test2",TestBean.class);
System.out.println("testBean2="+testBean2);
}
}
}
结果
双利模式,之会实例化两次 所以两个实例话对象的结果,也分别是2个对象
springIOC注解懒加载 使用@Lazy注解
pom
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
初始化类
package com.example.springboot02.class16;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 19:08
* @Modified By:
*/
@Component("testBean1")
@Lazy
public class TestBean {
public TestBean(){
System.out.println("testBean加载");
}
}
package com.example.springboot02.class16;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 19:08
* @Modified By:
*/
@Configuration
@ComponentScan("com.example.springboot02.class16")
public class TestConfiguration {
@Bean("testBean2")
@Lazy
public TestBean testBean(){
return new TestBean();
}
}
测试代码
package com.example.springboot02.class16;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 19:08
* @Modified By:
*/
public class class16 {
@Test
public void test(){
ApplicationContext context=new AnnotationConfigApplicationContext(TestConfiguration.class);
System.out.println("context 已经被初始化了");
TestBean testBean1=context.getBean("testBean1",TestBean.class);
System.out.println("testBean1="+testBean1);
System.out.println("==========================");
TestBean testBean2=context.getBean("testBean2",TestBean.class);
System.out.println("testBean2="+testBean2);
}
}
结果
SpringIoc注解实现 bean的初始化和销毁
第一种方式实现: InitializingBean,DisposableBean接口
pom
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
实例对象
package com.example.springboot02.class17;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 19:18
* @Modified By:
*/
@Component("testBean")
public class TestBean implements InitializingBean,DisposableBean{
@Override
public void destroy() throws Exception {
System.out.println("test.destroy");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("test.init");
}
}
package com.example.springboot02.class17;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 19:19
* @Modified By:
*/
@Configuration
@ComponentScan("com.example.springboot02.class17")
public class TestConfiguration {
// @Bean
// public TestBean testBean(){
// return new TestBean();
// }
}
测试类
package com.example.springboot02.class17;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 19:20
* @Modified By:
*/
public class Class17 {
@Test
public void test(){
AbstractApplicationContext context=new AnnotationConfigApplicationContext(TestConfiguration.class);
TestBean testBean=context.getBean("testBean",TestBean.class);
System.out.println("testBean="+testBean);
context.close();
}
}
输出结果
第二种方式
使用注解:PostConstruct、PreDestroy
package com.example.springboot02.class17;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* @Author: Administrator
* @Description: PostConstruct:bean初始化的处理
* PreDestroy:bean销毁的处理
* @Date: 2019-10-07 19:18
* @Modified By:
*/
@Component("testBean")
public class TestBean implements InitializingBean,DisposableBean{
@Override
public void destroy() throws Exception {
System.out.println("test.destroy");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("test.init");
}
@PostConstruct
public void onInit(){
System.out.println("testBean.onInit");
}
@PreDestroy
public void onDestory(){
System.out.println("testBean.onDesctory");
}
}
结果
第三种方式
package com.example.springboot02.class17;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* @Author: Administrator
* @Description: PostConstruct:bean初始化的处理
* PreDestroy:bean销毁的处理
* @Date: 2019-10-07 19:18
* @Modified By:
*/
@Component("testBean")
public class TestBean implements InitializingBean,DisposableBean{
@Override
public void destroy() throws Exception {
System.out.println("test.destroy");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("test.init");
}
@PostConstruct
public void onInit(){
System.out.println("testBean.onInit");
}
@PreDestroy
public void onDestory(){
System.out.println("testBean.onDesctory");
}
public void onInit1(){
System.out.println("testBean.onInit1");
}
public void onDestory1(){
System.out.println("testBean.onDesctory1");
}
}
实例化对象
package com.example.springboot02.class17;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @Author: Administrator
* @Description:
* @Date: 2019-10-07 19:19
* @Modified By:
*/
@Configuration
@ComponentScan("com.example.springboot02.class17")
public class TestConfiguration {
@Bean(initMethod = "onInit1",destroyMethod = "onDestory1")
public TestBean testBean(){
return new TestBean();
}
}
结果
三种方式均可以实现销毁和初始化方法