- 浏览: 3443464 次
- 性别:
- 来自: 珠海
-
文章分类
- 全部博客 (1633)
- Java (250)
- Android&HTML5 (111)
- Struts (10)
- Spring (236)
- Hibernate&MyBatis (115)
- SSH (49)
- jQuery插件收集 (55)
- Javascript (145)
- PHP (77)
- REST&WebService (18)
- BIRT (27)
- .NET (7)
- Database (105)
- 设计模式 (16)
- 自动化和测试 (19)
- Maven&Ant (43)
- 工作流 (36)
- 开源应用 (156)
- 其他 (16)
- 前台&美工 (119)
- 工作积累 (0)
- OS&Docker (83)
- Python&爬虫 (28)
- 工具软件 (157)
- 问题收集 (61)
- OFbiz (6)
- noSQL (12)
最新评论
-
HEZR曾嶸:
你好博主,这个不是很理解,能解释一下嘛//左边+1,上边+1, ...
java 两字符串相似度计算算法 -
天使建站:
写得不错,可以看这里,和这里的这篇文章一起看,有 ...
jquery 遍历对象、数组、集合 -
xue88ming:
很有用,谢谢
@PathVariable映射出现错误: Name for argument type -
jnjeC:
厉害,困扰了我很久
MyBatis排序时使用order by 动态参数时需要注意,用$而不是# -
TopLongMan:
非常好,很实用啊。。
PostgreSQL递归查询实现树状结构查询
http://blog.csdn.net/topwqp/article/details/8681497
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种:
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
第二种是:通过 在xml中定义init-method 和 destory-method方法
第三种是: 通过bean实现InitializingBean和 DisposableBean接口
下面演示通过 @PostConstruct 和 @PreDestory
1:定义相关的实现类:
2:定义相关的配置文件:
其中<context:annotation-config />告诉spring 容器采用注解配置:扫描注解配置;
测试类:
测试结果:
I'm init method using @PostConstrut....123
I'm destory method using @PreDestroy.....123
其中也可以通过申明加载org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
类来告诉Spring容器采用的 常用 注解配置的方式:
只需要修改配置文件为:
同样可以得到以上测试的输出结果。
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种:
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
第二种是:通过 在xml中定义init-method 和 destory-method方法
第三种是: 通过bean实现InitializingBean和 DisposableBean接口
下面演示通过 @PostConstruct 和 @PreDestory
1:定义相关的实现类:
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:定义相关的配置文件:
<?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 容器采用注解配置:扫描注解配置;
测试类:
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容器采用的 常用 注解配置的方式:
只需要修改配置文件为:
<?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 Boot 属性配置
2016-06-24 11:04 1198Spring Boot 属性配置和使用 http://blog ... -
Spring Boot 集成MyBatis
2016-06-24 10:55 2049Spring Boot 集成MyBatis http://bl ... -
Spring MVC防重复提交
2016-06-17 15:47 1664http://my.oschina.net/zyqjustin ... -
Spring容器加载完之后执行特定任务
2016-06-17 15:36 2308http://my.oschina.net/simpleton ... -
使用spring-session和shiro来代理session的配置
2016-06-16 11:21 12080使用spring-session和redis来代理sessio ... -
JSTL 的 if else : 有 c:if 没有 else 的处理
2016-06-14 09:52 1349http://blog.csdn.net/xiyuan1999 ... -
spring mvc 请求转发和重定向
2016-06-14 09:48 1421http://blog.csdn.net/jackpk/art ... -
mvc:view-controller
2016-05-18 10:26 1099http://blog.csdn.net/lzwglory/a ... -
spring配置事物的方式:注解和aop配置
2016-05-14 00:26 4122参考: Spring AOP中pointcut express ... -
分布式任务调度组件 Uncode-Schedule
2016-05-13 14:47 2301http://www.oschina.net/p/uncode ... -
Mybatis分库分表扩展插件
2016-05-12 15:47 1640http://fangjialong.iteye.com/bl ... -
spring+mybatis+atomikos 实现JTA事务
2016-05-11 22:00 5544sping配置多个数据源 不同用户操作不同数据库 http:/ ... -
Spring中使用注解 @Scheduled执行定时任务
2016-05-10 09:39 1582原文:http://dwf07223.blog.51cto.c ... -
Spring中配置Websocket
2016-05-05 16:55 1293spring+websocket整合(springMVC+sp ... -
redis 集群中Session解决方案之Spring Session
2016-05-04 08:54 1337集群中Session解决方案之Spring Session h ... -
使用Spring-data进行Redis操作
2016-05-04 08:54 4822使用Spring-data进行Redis操作 http://z ... -
Spring4新特性——集成Bean Validation 1.1(JSR-349)到SpringMVC
2016-05-03 13:35 1076Spring4新特性——集成Bean Validation 1 ... -
SpringMVC介绍之Validation
2016-05-03 13:10 1008SpringMVC介绍之Validation http://h ... -
spring 注解方式下使用commons-validator 验证表单
2016-05-03 11:08 3093原文: http://www.programgo.com/ar ... -
Spring MVC学习详解
2016-04-28 09:13 1019原文 http://blog.csdn.net/alsocod ...
相关推荐
然而,没有这些文件的具体内容,我们无法深入讨论Infinispan如何利用`@PostConstruct` 和 `@PreDestroy` 进行初始化和销毁操作。 在实际开发中,了解和正确使用这些注解对于确保服务的稳定性和资源的有效管理至关...
本文将深入探讨如何在Spring容器初始化Bean和销毁Bean前后执行自定义的操作,以便于进行日志记录、资源清理等任务。 首先,我们需要了解Spring中Bean的生命周期。Bean的生命周期大致分为以下阶段: 1. 实例化:...
在Spring框架中,我们可以使用多种方式来控制Bean的初始化和销毁,以下就Spring Bean的初始化和销毁进行详细的讲解。 一、Bean的初始化 在Spring框架中,我们可以使用多种方式来初始化Bean,下面我们将介绍两种...
`@POSTConstruct` 和 `@PreDestroy` 注解允许我们在Bean初始化完成后执行特定的操作,并在Bean销毁之前执行清理工作。本示例详细介绍了如何在Spring中使用这两个注解来定制Bean的生命周期行为。 首先,让我们了解...
下面将详细介绍如何通过不同方式定义Spring Bean的初始化和销毁回调方法。 **初始化回调方法** 1. **@PostConstruct注解** 这个Java标准注解用于标记一个方法,该方法将在对象完全构造后但在业务逻辑执行前被调用...
`@PostConstruct`和`@PreDestroy`注解分别标识初始化和销毁方法,Spring会在Bean实例化后调用`@PostConstruct`方法,在销毁前调用`@PreDestroy`方法。 8. **生命周期回调** 除了初始化和销毁方法,Spring还提供了...
"Spring初始化和销毁的实现方法" Spring框架中,Bean的初始化和销毁是非常重要的两个生命...Spring 中提供了多种实现 Bean 初始化和销毁的方法,开发者可以根据实际情况选择合适的方法来实现 Bean 的生命周期管理。
4. **启动后处理器(BeanPostProcessor)**:这些是特殊类型的Bean,它们在所有常规Bean实例化之后,但初始化之前进行处理。它们的实例化顺序由`@Order`注解决定,或者通过实现`Ordered`接口来控制。 5. **...
同样,如果实现了`org.springframework.beans.factory.DisposableBean`接口,Spring会在销毁Bean时调用`destroy()`方法。 ```java public class PersonService implements InitializingBean, DisposableBean { ....
在上面的例子中,当Spring容器创建`myService` Bean时,会调用`init`方法进行初始化,而在Bean被销毁时调用`destroy`方法进行资源清理。 在转向注解配置之前,我们需要理解Spring的@Component、@Service、@...
Spring 提供了多种不同的方式来初始化和销毁 Bean,这些方式包括 init-method/destroy-method、InitializingBean/DisposableBean、@PostConstruct/@PreDestroy 和 ContextStartedEvent/ContextClosedEvent 等。...
1. **初始化阶段**:在这个阶段,Spring会创建Bean的实例,这可以通过`@PostConstruct`注解的方法来标记初始化代码。此外,如果Bean实现了`InitializingBean`接口,Spring会调用`afterPropertiesSet()`方法。另外,...
在Spring框架中,Bean的生命周期管理是其核心功能之一,它涉及到Bean的创建、初始化、使用和销毁等各个阶段。以下是对"bean的生命周期1"的详细解释: 1. **指定初始化和销毁方法**: Spring允许我们为Bean定义初始...
`@PostConstruct`标注的方法会在bean完全初始化之后、实例可供其他bean使用之前调用,而`@PreDestroy`则会在bean即将被销毁时调用。 通过使用这些注解,我们可以减少XML配置,使代码更简洁,同时也使得依赖关系更加...
通过实现`postProcessBeforeInitialization()`和`postProcessAfterInitialization()`方法,可以在Bean初始化前后进行额外的操作。 在Spring容器中,Bean的生命周期管理也支持通过`BeanDefinition`进行定制。例如,...
如果一个 Bean 实现了 InitializingBean 接口,Spring 会在实例化后调用 afterPropertiesSet 方法进行初始化。另外,也可以使用 @PostConstruct 注解标记初始化方法,同样会在 Bean 实例化后执行。 6. **...
3.@PostConstruct 和 @PreDestroy 注解 Spring 也支持在方法上使用这些注解来标识初始化和销毁方法。`@PostConstruct` 标记的方法会在所有依赖注入完成后调用,而 `@PreDestroy` 标记的方法会在容器关闭前...
在这里,我们使用 @PostConstruct 和 @PreDestroy 注解来指定组件的初始化和销毁方法,这样可以在组件的生命周期中执行特定的操作。 @Component、@Repository、@Service、@Controller 等注解是 Spring 框架中常用的...
4. **容器回调**:在Bean初始化之后,Spring容器可能会调用自己的回调方法,如`BeanPostProcessor`接口实现的`postProcessBeforeInitialization()`和`postProcessAfterInitialization()`方法,允许自定义逻辑进一步...