javascript
Spring-基于注解的配置[03Bean作用范围和生命周期方法]
- Bean的作用范圍
- 實例
- Bean的生命周期方法
- 實例
Bean的作用范圍
通過注解配置的Bean和通過<bean>配置的Bean一樣,默認的作用范圍都是singleton。
Spring為注解配置提供了一個@Scope注解,可以通過它顯示指定Bean的作用范圍。
實例
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster
package com.xgj.ioc.configuration.lifeCycle;import javax.annotation.PostConstruct; import javax.annotation.PreDestroy;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component;@Scope("prototype") @Component public class Teacher {private Student student;/*** * * @Title:Teacher* * @Description:構造函數*/public Teacher() {super();System.out.println("Teacher is initing....");}@Autowiredpublic void setStudent(Student student) {this.student = student;} } package com.xgj.ioc.configuration.lifeCycle;import org.springframework.stereotype.Component;@Component public class Student {public Student() {super();System.out.println("Student is initing....");}}配置文件
<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- (1)聲明Context命名空間以及Schema文件 (2)掃描類包以及應用注解定義的bean --><context:component-scan base-package="com.xgj.ioc.configuration.lifeCycle"/></beans>測試類
package com.xgj.ioc.configuration.lifeCycle;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class ScopeTest {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/ioc/configuration/lifeCycle/beanLifeCycle.xml");System.out.println("isPrototype:" + ctx.isPrototype("teacher"));System.out.println("isSingleton:" + ctx.isSingleton("teacher"));} }運行結果:
Bean的生命周期方法
@Scope注解通過入參指定Bean的作用范圍。 在使用<bean>進行配置可以通過init-method和destory屬性指定Bean的初始化及容器銷毀前執行的方法。
Spring從2.5開始支持JSR-250中定義的@PostConstruct和PreDestory注解。 在Spring中這兩個注解相當于init-method和destroy-method屬性的功能。 不過在使用注解時,可以在一個bean 中定義多個@PostConstruct和@PreDestory方法。
實例
我們取消掉Teacher類的 @Scope(“prototype”) 注解 (因為對于singleton的Bean,容器管理,prototype由調用者管理,Spring不管理) ,增加 @PostConstruct 和PreDestory。
改造如下
package com.xgj.ioc.configuration.lifeCycle;import javax.annotation.PostConstruct; import javax.annotation.PreDestroy;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;//@Scope("prototype") @Component public class Teacher {private Student student;/*** * * @Title:Teacher* * @Description:構造函數*/public Teacher() {super();System.out.println("Teacher is initing....");}@Autowiredpublic void setStudent(Student student) {this.student = student;}@PostConstructpublic void init1() {System.out.println("init 1");}@PostConstructpublic void init2() {System.out.println("init 2");}@PreDestroypublic void destory1() {System.out.println("destory 1");}@PreDestroypublic void destory2() {System.out.println("destory 2");}}測試類
package com.xgj.ioc.configuration.lifeCycle;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class ScopeTest {public static void main(String[] args) {// 啟動容器ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/ioc/configuration/lifeCycle/beanLifeCycle.xml");System.out.println("isPrototype:" + ctx.isPrototype("teacher"));System.out.println("isSingleton:" + ctx.isSingleton("teacher"));// 關閉容器,對于singleton的Bean,容器管理,prototype由調用者管理,Spring不管理((ClassPathXmlApplicationContext) ctx).destroy();} }運行結果:
由此可以看出,Spring先調用類的構造函數實例化Bean,然后在執行@Autowired進行自動注入,然后分別執行標注了@PostConstruct的方法,然后在容器關閉時,分別執行了標注@PreDestroy的方法。
總結
以上是生活随笔為你收集整理的Spring-基于注解的配置[03Bean作用范围和生命周期方法]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring-基于Spring使用自定义
- 下一篇: Spring-方法注入lookup、方法