`
最王座
  • 浏览: 137165 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring注解之@PostConstruct

阅读更多

一、注解解释

Spring的@PostConstruct注解在方法上,表示此方法是在Spring实例化该Bean之后马上执行此方法,之后才会去实例化其他Bean,并且一个Bean中@PostConstruct注解的方法可以有多个。

 

二、示例代码

1. 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.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">

	<!-- 引入属性文件 -->
	<context:property-placeholder location="classpath:config.properties" />

	<!-- 自动扫描dao和service包(自动注入) -->
	<context:component-scan base-package="com.wbf.bean" />

</beans>

 2. Bean1.java

 

 

package com.wbf.bean;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

@Service("bean1")
public class Bean1 {
	
	private static final Logger log = Logger.getLogger(Bean1.class);
	
	public Bean1() {
		log.info(System.currentTimeMillis() + ": Bean1 Constructor");
	}
	
	@PostConstruct
	public void test() {
		log.info(System.currentTimeMillis() + ": bean1-->test()");
		Bean2.uniqueInstance().test();
		
	}
	
	@PostConstruct
	public void print() {
		log.info(System.currentTimeMillis() + ": bean1-->print()");
	}
}

 3. Bean2.java

 

 

package com.wbf.bean;

import org.apache.log4j.Logger;

public class Bean2 {
	
	private static final Logger log = Logger.getLogger(Bean2.class);
	
	private static Bean2 instance = uniqueInstance();
	
	public static Bean2 uniqueInstance() {
		if (instance == null)
			instance = new Bean2();
		
		return instance;
	}
	
	public Bean2() {
		log.info(System.currentTimeMillis() + ": Bean2 Construtor");
	}
	
	public void test() {
		log.info(System.currentTimeMillis() + ": bean2-->test()");
	}
}

4.  Bean3.java

 

 

package com.wbf.bean;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

@Service("bean3")
public class Bean3 {
	
	private static final Logger log = Logger.getLogger(Bean3.class);
	
	public Bean3() {
		log.info(System.currentTimeMillis() + ": Bean3 Construtor");
	}
	
}

 5. SpringTest.java

 

 

package com.wbf.bean;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
	
	@Test
	public void test() {
		//加载/解析spring.xml, 得到BeanFactory, 实例化所有IOC容器中的Bean
		//在实例化每一个Bean之后,如果当前Bean包含@PostConstruct注解的方法则会马上执行该方法,之后才会去实例化其他的Bean
		//每一个Bean中可以有多个包含@PostConstruct注解的方法
		ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"classpath:spring.xml"});
	}
	
}

 运行结果:

 

 

[org.springframework.context.support.ClassPathXmlApplicationContext]Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@b0f2b2: startup date [Thu Jun 11 11:51:17 CST 2015]; root of context hierarchy
[org.springframework.beans.factory.xml.XmlBeanDefinitionReader]Loading XML bean definitions from class path resource [spring.xml]
[org.springframework.beans.factory.config.PropertyPlaceholderConfigurer]Loading properties file from class path resource [config.properties]
[org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor]JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
[org.springframework.beans.factory.support.DefaultListableBeanFactory]Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b8dc93: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,bean1,bean3,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
[com.wbf.bean.Bean1]1433994678340: Bean1 Constructor
[com.wbf.bean.Bean1]1433994678347: bean1-->print()
[com.wbf.bean.Bean1]1433994678347: bean1-->test()
[com.wbf.bean.Bean2]1433994678348: Bean2 Construtor
[com.wbf.bean.Bean2]1433994678348: bean2-->test()
[com.wbf.bean.Bean3]1433994678348: Bean3 Construtor

从运行结果可以看出Spring在实例化Bean1之后马上执行了它的@PostConstruct注解的方法print()和test(),之后才去实例化Bean3。其中Bean2没有被Spring IOC容器管理。

 

三、补充说明

@PostConstruct注解Bean中的某些方法,可以用在服务器启动时的做一些初始化工作。

 

 

分享到:
评论

相关推荐

    @PostConstruct注解用来获取springbean对象.txt

    获取springbean对象

    Spring框架中 @Autowired 和 @Resource 注解的区别

    在 spring 框架中,除了使用其特有的注解外,使用基于 JSR-250 的注解,它包括 @PostConstruct, @PreDestroy 和 @Resource 注释。  首先,咱们简单了解 @PostConstruct 和 @PreDestroy 注释:  为了定义一个 bean...

    源码深入解析spring 的初始化方法 initMethod (标有注解的@postConstruct的方法)–极度细致!

    @postConstruct 所标注的方法 内部是靠的spring提供的两个后置处理器(InitDestroyAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor)共同 协调分布处理完成的。 这2点也是网上绝大部人没讲明白的...

    Spring注解 - 52注解 - 原稿笔记

    注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...

    Spring定时任务中@PostConstruct被多次执行异常的分析与解决

    主要给大家介绍了关于Spring定时任务中@PostConstruct被多次执行异常的分析与解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

    Spring bean初始化及销毁你必须要掌握的回调方法.docx

    1、通过java提供的@PostConstruct注解; 2、通过实现spring提供的InitializingBean接口,并重写其afterPropertiesSet方法; 3、通过spring的xml bean配置或bean注解指定初始化方法,如下面实例的initMethod方法通过@...

    javax.annotation.jar文件

    spring 注解初始化,初始化开始@postconstruct 对象销毁@predestroy javax.annotation.jar文件

    spring 相关jar包

    Spring需要的相关jar包。包括spring.jar 、commons-logging.jar 如果使用了且卖弄编程(AOP)还需要下列jar...如果使用了JSR-250中的注解,如@PostConstruct/@PreDestroy 还需要下列jar文件 common-annotations.jar

    spring MVC 初始启动blocking queue

    spring MVC 初始启动concurrent blocking queue,通过@PostConstruct 注解实现,详情参考我的博客

    mina新手教程源码 mina+springboot+idea最简单的案例。

    * 1、添加@Controller注解和 @PostConstruct注解,代表启动springboot项目时也调用该类下的该方法, * 启动springboot项目,会启动两个服务两个端口,一个是http服务,一个是mina服务。两个服务互不影响 * 2、...

    spring.doc

    3.6.4 @PostConstruct 28 3.6.5 @PreDestroy 28 注解注入拓展: 28 3.7扫描注入 30 注解扫描拓展: 32 Mvc用注解写: 34 Spring容器IOC和di的整个启动过程: 38 3.8 spring中的继承 38 拓展spring为类中的属性赋值:...

    开源框架面试专题及答案.pdf

    Spring Bean 的生命周期 &gt; Spring Bean 的生命周期简单易懂。在一个 bean 实例被初始化时,需要执行一系列的初 始化操作以达到可用的状态。同样的,当一个 bean 不在被调用...&gt; @PostConstruct 和@PreDestroy 注解方式

    Spring的学习笔记

    五、 @Scope、@PostConstruct、@PreDestroy 19 六、 注解对应的jar包 19 第七课:AOP(面向切面编程) 19 一、 AOP概念 19 二、 利用动态代理实现面向切面编程 20 第八课:Spring AOP配置选项 21 一、 AOP配置...

    Maven.Spring-BeansLearnerLab

    目的-演示使用Bean注册依赖注入国际奥委会集装箱AnnotationConfigApplicationContext 注解@Bean @DependsOn @Autowired @PostConstruct @Config @SpringBootTest @Qualifier发展笔记在完成Part 0.0之前,请勿克隆此...

    Spring中如何动态注入Bean实例教程

    主要给大家介绍了关于Spring中如何动态注入Bean的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

    动态的装配bean,注入到Controller中

    有时候根据需求会动态的装配bean,注入到Controller中,现在提供两种方式: 一、xml文件配置方式,ApplicationContext.xml 二、@PostConstruct注解方式

    spring2.5 学习笔记

    五、 @Scope、@PostConstruct、@PreDestroy 19 六、 注解对应的jar包 19 第七课:AOP(面向切面编程) 19 一、 AOP概念 19 二、 利用动态代理实现面向切面编程 20 第八课:Spring AOP配置选项 21 一、 AOP配置...

    thinking-in-spring:学春天

    顺序:@PostConstruct&gt; InitializingBean#afterPropertiesSet方法&gt; initMethod Bean的预设初始化 延迟初始化和非延迟对象的差异:应用之上启动前后BeanInitialDemo Bean的销毁@PreDestroy&gt; Dispo

    Spring中文帮助文档

    3.11.5. @PostConstruct 与 @PreDestroy 3.12. 对受管组件的Classpath扫描 3.12.1. @Component和更多典型化注解 3.12.2. 自动检测组件 3.12.3. 使用过滤器自定义扫描 3.12.4. 自动检测组件的命名 3.12.5. 为...

    Spring API

    3.11.5. @PostConstruct 与 @PreDestroy 3.12. 对受管组件的Classpath扫描 3.12.1. @Component和更多典型化注解 3.12.2. 自动检测组件 3.12.3. 使用过滤器自定义扫描 3.12.4. 自动检测组件的命名 3.12.5. 为...

Global site tag (gtag.js) - Google Analytics