spring随手笔记2:初始化方法】的更多相关文章

1:Spring 容器中的 Bean 是有生命周期的,spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作.下面是常用的三种指定特定操作的方法: 通过实现InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法: 通过<bean> 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法: 在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化…
1.init-method="init" public class HelloWorldServiceImpl implements HelloWorldService { private String msg; public void init(){ this.msg="hellword"; } public void setMsg(String msg) { this.msg = msg; } public String getMsg() { return ms…
1. public class HelloWorld { private String msg; public void setMsg(String msg) { this.msg = msg; } public String getMsg() { return msg; } public void detory(){ this.msg=""; } } <bean id="HelloWord" class="com.ltf.captha.servic…
Multiple lifecycle mechanisms configured for the same bean, with different initialization methods, are called as follows: Methods annotated with @PostConstruct    afterPropertiesSet() as defined by the InitializingBean callback interface     A custom…
1.local属性 引用在同一个xml的bean           只能引用bean的id <bean id="HelloWord" class="com.ltf.captha.serviceImpl.HelloWorld" destroy-method="destory"> <property name="date"> <ref local="date"/> <…
<bean id="Hello" class="com.ltf.captha.serviceImpl.HelloWorldServiceImpl">        <!-- 通过构造函数进行注入 -->        <constructor-arg index="0">            <value>helloworld</value>        </construct…
开发框架:spingMVC+myBatis 解决方案:给web容器添加一个Listener类,在容器启动的时候执行Listener的“初始化”方法,在这个初始化方法中执行查询数据库的所有操作,然后将数据库中的信息缓存起来 问题:上面的方法很好,但问题是如何去查询数据库,由于使用了spring的IOC特性,查询数据库的service控制dao层,dao层访问数据库,而Listener类只是在系统启动的时候会执行初始化方法,但是“service”对象没有被spring管理,也就是说没有service…
需求:在tomcat启动时开启一个定时任务. 想法:容器启动时执行方法,最容易想到的就是servlet中可以配置load-on-startup,设置一个正整数也就可以随容器一起启动. 问题:上面的方法很好,但是由于定时任务需要去操作数据库,而项目采用了spring的依赖注入来管理对象,而servlet并不受Spring的管理.若此时在servlet中注入Spring管理的对象,则会报错:javax.naming.NameNotFoundException: Name com.test.InitS…
一.通过@Bean指定初始化和销毁方法 在以往的xml中,我们是这样配置的 <bean id="exampleInitBean" class="examples.ExampleBean" init-method="init" destroy-method="cleanup"/> 那如果采用注解 的方式该如何配置呢? 首先我们创建一个Car, public class Car { public Car(){ Syst…
一.applicationContext.xml配置bean <bean id="sensitiveWordInitUtil" class ="com.hx.dazibo.front.util.SensitiveWordInitUtil" scope="singleton" init-method="initKeyWord"> <property name="xmlFile"> &l…