1.6.1 生命周期回調
實現接口:
InitializingBean?
DisposableBean?
可以使用afterPropertiesSet()?和destroy() 執行特定的功能。
JSR-250?規定使用@PostConstruct?and?@PreDestroy注解,但是這個不會與spring的特定接口結合。也就是不會走到Spring的特定接口。
Spring 使用 BeanPostProcessor?,
-初始化回調
org.springframework.beans.factory.InitializingBean
void afterPropertiesSet() throws Exception;
不推薦使用InitializingBean?的接口,因為會與Spring進行不必要的耦合。使用 @PostConstruct? 或者指定POJO指定方法。
xml使用 init-method, Java使用 intiMethod
<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
public class ExampleBean {
public void init() {
// do some initialization work
}
}
}
雖然與以下等效,但是不會與Spring耦合
<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
public class AnotherExampleBean implements InitializingBean {
public void afterPropertiesSet() {
// do some initialization work
}
}
}
-銷毀回調
org.springframework.beans.factory.DisposableBean
void destroy() throws Exception;
不建議使用DisposableBean?回調接口,應為會和Spring有不必要的耦合。
可以使用@PreDestroy?, 在XML中bean配置 destroy-method
<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>
在Java中, 使用Bean中的destroyMethod.
<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>
public class ExampleBean {
public void cleanup() {
// do some destruction work (like releasing pooled connections)
}
}
}
與以下等效:以下方式不好,與Spring 耦合
<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
public class AnotherExampleBean implements DisposableBean {
public void destroy() {
// do some destruction work (like releasing pooled connections)
}
}
}
-默認初始和銷毀方法
可以指定初始化的方法:
<beans default-init-method="init">
<bean id="blogService" class="com.foo.DefaultBlogService">
<property name="blogDao" ref="blogDao" />
</bean>
</beans>
代碼中新增這個方法:
public class DefaultBlogService implements BlogService {
private BlogDao blogDao;
public void setBlogDao(BlogDao blogDao) {
this.blogDao = blogDao;
}
// this is (unsurprisingly) the initialization callback method
public void init() {
if (this.blogDao == null) {
throw new IllegalStateException("The [blogDao] property must be set.");
}
}
}
}
同樣銷毀可以使用 default-destroy-method 的配置
對同一個bean的多個生命周期機制,可以使用不同的初始化方法,順序如下
1- @PostConstruct 的注釋
2- afterPropertiesSet() 定義在InitializingBean? 接口的
3. 定制的 init() 方法
銷毀的順序也是類似:
1. @PreDestroy 注釋
2. destroy()? 方法 繼承自DisposableBean?
3.定制的destroy() 方法。
在依賴項開始之前開始,在依賴項結束之后結束。
SmartLifecycle接口,可以得到對象的階段。
Spring web 的ApplicationContext已經處理了關閉Spring IoC 容器。
非web應用程式需要處理。
registerShutdownHook()
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public final class Boot {
public static void main(final String[] args) throws Exception {
ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
// add a shutdown hook for the above context...
ctx.registerShutdownHook();
// app runs here...
// main method exits, hook is called prior to the app shutting down...
}
}
}
1.6.2 ApplicationContextAware? 和 BeanNameAware
從ApplicationContextAware接口繼承,當創建一個ApplicationContext是,提供了以下設置方式:
public interface ApplicationContextAware {
void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}
}
BeanNameAware 類似狀況
public interface BeanNameAware {
void setBeanName(String name) throws BeansException;
}
}
1.6.3 其他Aware接口
Name
Injected Dependency
Explained in…?
ApplicationContextAware
Declaring?ApplicationContext
ApplicationContextAware and BeanNameAware
ApplicationEventPublisherAware
Event publisher of the enclosing?ApplicationContext
Additional capabilities of the ApplicationContext
BeanClassLoaderAware
Class loader used to load the bean classes.
Instantiating beans
BeanFactoryAware
Declaring?BeanFactory
ApplicationContextAware and BeanNameAware
BeanNameAware
Name of the declaring bean
ApplicationContextAware and BeanNameAware
BootstrapContextAware
Resource adapter?BootstrapContext?the container runs in. Typically available only in JCA aware?ApplicationContexts
JCA CCI
LoadTimeWeaverAware
Defined?weaver?for processing class definition at load time
Load-time weaving with AspectJ in the Spring Framework
MessageSourceAware
Configured strategy for resolving messages (with support for parametrization and internationalization)
Additional capabilities of the ApplicationContext
NotificationPublisherAware
Spring JMX notification publisher
Notifications
ResourceLoaderAware
Configured loader for low-level access to resources
Resources
ServletConfigAware
Current?ServletConfig?the container runs in. Valid only in a web-aware SpringApplicationContext
Spring MVC
ServletContextAware
Current?ServletContext?the container runs in. Valid only in a web-aware SpringApplicationContext
Spring MVC
Aware的用法沒有follow IoC的風格。
- 空白目錄
- 0.環境準備
- 0.1基于maven的工程創建
- 1.控制反轉容器
- 1.1 Spring控制反轉容器和beans介紹
- 1.2 容器概覽
- 1.3 Bean概覽
- 1.4 依賴
- 1.5 Bean的范圍
- 1.6 客制bean的特性
- 1.7 Bean定義的繼承
- 1.8 容器擴展點
- 1.9 基于注解的容器配置
- 1.10 類路徑掃描及組件管理
- 1.11 使用JSR 330標準的注解
- 1.12 基于Java的容器配置
- 1.12.1 基本概念: @Bean 和 @Configuration
- 1.13 環境抽象化
- 1.14 注冊一個LoadTimeWeaver
- 1.15 ApplicationContext的附加功能
- 1.16 BeanFactory
- 2. 資源
- 3. 驗證,數據綁定和類型轉換
- 4. Spring表達式語言(SpEL)
- 5. Spring面向方面的切面編程
- 6. Spring AOP 接口
- 7. 空安全
- 8. 數據緩沖和編碼
- 9. 附錄