(該系列是觀看雷老師的教學視頻,加上自己上網查資料的總結)
使用@Configuration (配置註解)和@Bean註解將實列注入到ioc容器中
package springboot.configure;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springboot.apibean.Caraliu;
/**
* @author jeffchan 2020/03/20
*/
@Configuration
public class Configure {
@Lazy // 開啓懶加載
@Scope("singleton") // 單實例
@Bean(name = "caraliu0") // 指定注入的bean和bean的名稱
public Caraliu caraliu(){
return new Caraliu();
}
}
上述代碼會將Caraliu實列加入到IOC容器中管理,bean的名字默認爲caraliu
你可以通過改方法名來改注入實列id,當然也可以加註解的屬性@Bean(name = "caraliu0")
注意不同版本的spring,可能註解的屬性不一樣
在Caraliu類中加入兩個方法
public void init(){
System.out.println("init...");
}
public void destory(){
System.out.println("destory...");
}
然後通過屬性進行設置,指明初始化和銷燬的方法:
@Bean(name = "caraliu0",initMethod = "init",destroyMethod = "destory")
處了上述方式,還是讓bean實現兩個接口, InitializingBean, DisposableBean
重寫兩個方法:
@Override public void destroy() throws Exception { System.out.println("destroyx.."); } @Override public void afterPropertiesSet() throws Exception { System.out.println("initx.."); }
也可以實現初始化和銷燬
還可以用註解方式指定:
@PostConstruct public void initX(){ System.out.println("PostConstruct..."); }
但是調用時間: PostConstruct 》 接口實現的方式 》 @Bean的方式 銷燬的也一樣
注意針對單實例沒有設置懶加載的對象的時候,容器創建就會初始化,容器關閉的時候,會調用銷燬方法;如果是懶加載的話,那麼容器容器創建時不會初始化,在使用對象時纔會初始化,容器關閉的時候,同樣會調用銷燬方法;但是對於實列,容器創建的時候不會初始化,在使用對象的時候會初始化,初始化完後,關閉容器,不會調用銷燬方法。
bean的後置處理器:BeanPostProcessor
package springboot.configure;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* @author jeffchan 2020/03/22
*/
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
System.out.println("postProcessBeforeInitialization");
return o;
}
@Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
System.out.println("postProcessAfterInitialization");
return o;
}
}
會在bean的初始化前後調用這兩個方法。
以下看下源碼:
if (mbd == null || !mbd.isSynthetic()) {
//初始化之前
wrappedBean = this.applyBeanPostProcessorsBeforeInitialization(bean, beanName);
}
try {
//執行初始化方法
this.invokeInitMethods(beanName, wrappedBean, mbd);
} catch (Throwable var6) {
throw new BeanCreationException(mbd != null ? mbd.getResourceDescription() : null, beanName, "Invocation of init method failed", var6);
}
if (mbd == null || !mbd.isSynthetic()) {
//初始化之後
wrappedBean = this.applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
spring底層很多都用BeanPostProcessor來實現,比如屬性的注入,@BeanPostConstruct等都是。
上述的實列注入默認都是單實例的singleton,可以通過@Scope來指定實列的類型
多實列: prototype
以下兩種都是基於web應用的:
一個請求一個bean: request
一個運用一個bean: session
懶加載(懶加載是針對單實例來說的),上述的bean,如果不設置@Lazy,那麼啓動IOC容器的時候,就已經創建bean,當爲了節省資源時,可以
設置懶加載,設置懶加載後,bean不會馬上創建,而是當要使用相關的bean的時候,纔會去加載bean實列