Spring的init-method 与afterPropertiesSet
2014-07-17 08:03
507 查看
Spring的init-method 与afterPropertiesSet
博客分类:spring
SpringBeanJMSWeblogic
init-method 与afterPropertiesSet 都是在初始化bean的时候执行,执行顺序是afterPropertiesSet 先执行,init-method 后执行,afterPropertiesSet 必须实现 InitializingBean接口
今天遇到一问题,在配置文件中配置了init-method
<bean id="financeAgent" class="com.sumavision.ecommerce.plateform.core.monitor.FinanceAgent"
init-method="initialize" destroy-method="destroy">
<property name="jmsTemplate" ref="jmsTemplate" />
<property name="serviceMap">
<map
value-type="com.sumavision.ecommerce.plateform.core.monitor.FinanceMonitorService">
<entry key="financeManageService"
value-ref="financeManageService" />
<entry key="financeTransactService"
value-ref="financeTransactService" />
</map>
</property>
</bean>
<!-- Jndi -->
<bean id="jndiTemplate"
class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">
weblogic.jndi.WLInitialContextFactory
</prop>
<prop key="java.naming.provider.url">
t3://192.166.68.44:7001
</prop>
<prop key="java.naming.factory.url.pkgs">
weblogic.jndi.factories
</prop>
</props>
</property>
</bean>
<!-- jms sender -->
<bean id="jmsConnectionFactory"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName" value="ConnectionFactory" />
</bean>
<bean id="jmsQueue"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate"></property>
<property name="jndiName" value="Queue"></property>
</bean>
<!-- jms template -->
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory"></property>
<property name="defaultDestination" ref="jmsQueue"></property>
</bean>
在加载的时候不能执行,找了半天最后发现是因为
<beans default-lazy-init="true"></beans> 配置了延迟初始化,但是有的bean的init-method是可以运行的,具体需要再看下这个
<bean id="tradeAuditService"
class="com.sumavision.ecommerce.plateform.core.audit.impl.TradeAuditServiceImpl"
init-method="initialize">
<property name="auditRuleDAO" ref="AuditRuleDAO"></property>
<property name="negativelyTradeDAO" ref="NegativelyTradeDAO"></property>
<property name="securityServiceManager"
ref="securityServiceManager">
</property>
</bean>
<bean id="tradeAuditServiceProxy" parent="baseTransactionProxy">
<property name="target" ref="tradeAuditService"></property>
<property name="transactionAttributes">
<props>
<prop key="process">
PROPAGATION_REQUIRED,ISOLATION_SERIALIZABLE
</prop>
</props>
</property>
</bean>
这个就起作用了,
通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
分类: Spring2013-03-1616:48 10109人阅读 评论(1) 收藏 举报
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种:
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
第二种是:通过 在xml中定义init-method 和 destory-method方法
第三种是: 通过bean实现InitializingBean和 DisposableBean接口
下面演示通过 @PostConstruct 和 @PreDestory
1:定义相关的实现类:
[java] view
plaincopy
package com.myapp.core.annotation.init;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class PersonService {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@PostConstruct
public void init(){
System.out.println("I'm init method using @PostConstrut...."+message);
}
@PreDestroy
public void dostory(){
System.out.println("I'm destory method using @PreDestroy....."+message);
}
}
2:定义相关的配置文件:
[html] view
plaincopy
<?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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- <context:component-scan base-package="com.myapp.core.jsr330"/> -->
<context:annotation-config />
<bean id="personService" class="com.myapp.core.annotation.init.PersonService">
<property name="message" value="123"></property>
</bean>
</beans>
其中<context:annotation-config />告诉spring 容器采用注解配置:扫描注解配置;
测试类:
[java] view
plaincopy
package com.myapp.core.annotation.init;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("resource/annotation.xml");
PersonService personService = (PersonService)context.getBean("personService");
personService.dostory();
}
}
测试结果:
I'm init method using @PostConstrut....123
I'm destory method using @PreDestroy.....123
其中也可以通过申明加载org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
类来告诉Spring容器采用的 常用 注解配置的方式:
只需要修改配置文件为:
[html] view
plaincopy
<?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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- <context:component-scan base-package="com.myapp.core.jsr330"/> -->
<!-- <context:annotation-config /> -->
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean id="personService" class="com.myapp.core.annotation.init.PersonService">
<property name="message" value="123"></property>
</bean>
</beans>
同样可以得到以上测试的输出结果。
相关文章推荐
- spring bean中构造函数,afterPropertiesSet和init-method的执行顺序
- spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的区别联系
- 【Spring源码】从源码实现的角度解析spring的InitializingBean的afterPropertiesSet方法和init-method区别和应用
- spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系
- spring中afterPropertiesSet方法与init-method配置描述
- Spring InitializingBean的afterPropertiesSet与init-method、@PostConstruct
- spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系
- 写代码简单比较init-method,afterPropertiesSet和BeanPostProcessor
- spring @postConstruct init-method constructor和afterSetProperties() 执行顺序
- 源码解析:init-method、@PostConstruct、afterPropertiesSet孰先孰后
- init-method 与afterPropertiesSet
- 简单比较init-method,afterPropertiesSet和BeanPostProcessor
- 简单比较init-method,afterPropertiesSet和BeanPostProcessor
- 源码解析:init-method、@PostConstruct、afterPropertiesSet孰先孰后
- 【转】比较init-method,afterPropertiesSet和BeanPostProcessor
- 写代码简单比较init-method,afterPropertiesSet和BeanPostProcessor
- 用spring的InitializingBean的afterPropertiesSet来初始化
- jedis +springdata spring-data-redis 1.1.1 to 1.2.0 afterPropertiesSet signature exception
- Spring afterPropertiesSet方法读取系统配置文件实例
- Spring InitializingBean的afterPropertiesSet方法