直接用service实现两个接口:
package com.myapp.core.annotation.init; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class PersonService implements InitializingBean,DisposableBean{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @Override public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("I'm init method using implements InitializingBean interface...."+message); } @Override public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("I'm init method using implements DisposableBean interface...."+message); } }
spring xml :
<?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> --> <bean id="personService" class="com.myapp.core.annotation.init.PersonService"> <property name="message" value="123"></property> </bean> </beans>
测试类:
package com.myapp.core.annotation.init; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("resource/annotation.xml"); PersonService personService = (PersonService)context.getBean("personService"); context.registerShutdownHook(); } }
相关推荐
2、通过实现spring提供的InitializingBean接口,并重写其afterPropertiesSet方法; 3、通过spring的xml bean配置或bean注解指定初始化方法,如下面实例的initMethod方法通过@bean注解指定。 销毁的时候实现的方法 1...
spring配置对象实例化后执行的方法,两种实现比对,通过InitializingBean接口,并实现arterPropertiesSet()方法;或者通过applicationContext.xml配置init-method属性
主要介绍了Spring中的InitializingBean接口的使用,需要的朋友可以参考下
Spring Bean 的生命周期 ...> InitializingBean 和 DisposableBean 回调接口 > 针对特殊行为的其他 Aware 接口 > Bean 配置文件中的 Custom init()方法和 destroy()方法 > @PostConstruct 和@PreDestroy 注解方式
(6)如果BeanPostProcessor和Bean关联,则Spring将调用该接口的预初始化方法postProcessBeforeInitialzation()对Bean进行加工操作,这个非常重要,Spring的AOP就是用它实现的。 (7)如果Bean实现了...
它会根据配置文件或注解创建、初始化、使用和销毁Bean对象。同时,Spring还提供了扩展点(如BeanPostProcessor和InitializingBean接口),允许开发人员在Bean生命周期的不同阶段进行自定义操作。 AOP(面向切面编程...
NULL 博文链接:https://yuui89828923aaa.iteye.com/blog/812569
NULL 博文链接:https://latex.iteye.com/blog/900664
1. Bean的定义,给出配置元数据,可通过xml、java类、注解三种方式 2. Bean的初始化,调用InitializingBean 接口定义的after
将会逐步扩大完善Spring Boot版本:2.1.7,兼容的Spring Cloud版本为Greenwich,版本对照参考概要如何配置拦截器:拦截器,过滤器,@ RestControllerAdvice bean的初始化:InitializingBean接口,@ conditionXXX注解...
spring+InitializingBean+quartz+定时器-附件资源
春天学习笔记 Bean初始化 BeanDefinitionBuilder 新的GenericBeanDefinition Bean的命名(非强制性) ...延迟初始化和非延迟对象的差异:应用之上启动前后BeanInitialDemo Bean的销毁@PreDestroy> Dispo
.1. netty-sekiro 原文 感谢作者。以下是对原作者内容的学习提取 ...服务器端是用Springboot管理各种组件,在Springboot bean(InitializingBean)初始化 afterPropertiesSet完成会启动两个nio服务,分别监听5600
1. IP限流,每分钟可以访问三次,与学号绑定,通过Redis实现 ```java Integer sno = StudentIDUtils.getStudentIDFromMap(); Integer count = (Integer) redisService.get("ip-", String.valueOf(sno)); if ...
bean),即使用普通的“new”运算符创建 bean,然后 spring 进一步控制初始化 bean,替换 @Value 占位符 bean 属性上的值,调用 afterPropertiesSet InitializingBean 类型,注入@Autowired 或@Inject 属性等。...
FormApp:基于Eclipse的项目,通过表单提交来演示JSP和Servlet交互 modules-application:基于Maven的模块应用程序,可在后台将3个模块降级 spring-annotation-based-autowiring:基于Spring注释的自动装配bean ...
//初始化namedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource()); //初始化pagingSqlBuilder pagingSqlBuilder = new PagingSqlBuilder(...