用spring的InitializingBean的afterPropertiesSet来初始化
2016-05-03 10:23
447 查看
org.springframework.beans.factory包下有一个接口是InitializingBean 只有一个方法:
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware).
* <p>This method allows the bean instance to perform initialization only
* possible when all bean properties have been set and to throw an
* exception in the event of misconfiguration.
* @throws Exception in the event of misconfiguration (such
* as failure to set an essential property) or if initialization fails.
*/
void afterPropertiesSet() throws Exception;
这个方法将在所有的属性被初始化后调用。
但是会在init前调用。
但是主要的是如果是延迟加载的话,则马上执行。
所以可以在类上加上注解:
import org.springframework.context.annotation.Lazy;
@Lazy(false)
这样spring容器初始化的时候afterPropertiesSet就会被调用。
只需要实现InitializingBean接口就行。
如果代码在这里初始化的话,半天都找不到。。
转载:/article/5219201.html
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware).
* <p>This method allows the bean instance to perform initialization only
* possible when all bean properties have been set and to throw an
* exception in the event of misconfiguration.
* @throws Exception in the event of misconfiguration (such
* as failure to set an essential property) or if initialization fails.
*/
void afterPropertiesSet() throws Exception;
这个方法将在所有的属性被初始化后调用。
但是会在init前调用。
但是主要的是如果是延迟加载的话,则马上执行。
所以可以在类上加上注解:
import org.springframework.context.annotation.Lazy;
@Lazy(false)
这样spring容器初始化的时候afterPropertiesSet就会被调用。
只需要实现InitializingBean接口就行。
如果代码在这里初始化的话,半天都找不到。。
转载:/article/5219201.html
相关文章推荐
- Java final static abstract关键字介绍
- Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨
- JAVA线程的通讯
- java synchronized关键字的用法
- try ... catch 抓不到异常
- Java静态代理和动态代理
- 布隆过滤器(Bloom Filter)Java实现
- Java 简单的BFS爬虫
- [Java视频笔记]day14
- Java 的多线程访问常见异常--fast-lost (快速失败 )
- JAVA数据库连接的另一种实现及简单的数据插入及显示
- Spring--Bean scope
- Eclipse 项目红色叹号:Build Path Problem
- Java 多线程知识
- Spring简单属性注入--不常用!!!
- java中的构造函数
- JAVA的名词释义
- java分为 三类 ME,SE,EE
- Ubuntu 16.04 中的 Eclipse Mars 菜单栏选项失效
- java连接MySQL数据库 json数据前后端交互