一、注解解释

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.Bean3]1433994678348: Bean3 Construtor

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

三、补充说明

1.@PostConstruct说明

被@PostConstruct修饰的方法会在服务器加载Servlet(bean)的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。

2.@PreDestroy说明

被@PreDestroy修饰的方法会在服务器卸载Servlet(bean)的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreDestroy修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前

另外,spring中Constructor、@Autowired、@PostConstruct的顺序

其实从依赖注入的字面意思就可以知道,要将对象p注入到对象a,那么首先就必须得生成对象a和对象p,才能执行注入。所以,如果一个类A中有个成员变量p被@Autowried注解,那么@Autowired注入是发生在A的构造方法执行完之后的。

如果想在生成对象时完成某些初始化操作,而偏偏这些初始化操作又依赖于依赖注入,那么久无法在构造函数中实现。为此,可以使用@PostConstruct注解一个方法来完成初始化,@PostConstruct注解的方法将会在依赖注入完成后被自动调用。

Constructor >> @Autowired >> @PostConstruct

举个例子:

    public Class AAA{
@Autowired
private BBB b; public AAA() {
System.out.println("此时b还未被注入: b = " + b);
}
@PostConstruct
private void init () {
System.out.println("@PostConstruct将在依赖注入完成后被自动调用: b = " + b);
}
}

spring注解之@PostConstruct在项目启动时执行指定方法的更多相关文章

  1. Java项目启动时执行指定方法的几种方式

    很多时候我们都会碰到需要在程序启动时去执行的方法,比如说去读取某个配置,预加载缓存,定时任务的初始化等.这里给出几种解决方案供大家参考. 1. 使用@PostConstruct注解 这个注解呢,可以在 ...

  2. java项目启动时执行指定方法

    想到的就是监听步骤如下: 1.配置web.xml <listener> <listener-class>com.listener.InitListener</listen ...

  3. 在web项目启动时执行某个方法

    在web项目中有很多时候需要在项目启动时就执行一些方法,而且只需要执行一次,比如:加载解析自定义的配置文件.初始化数据库信息等等,在项目启动时就直接执行一些方法,可以减少很多繁琐的操作. 在工作中遇到 ...

  4. Spring Boot学习--项目启动时执行指定service的指定方法

    Springboot给我们提供了两种“开机启动”某些方法的方式:ApplicationRunner和CommandLineRunner. 这两种方法提供的目的是为了满足,在项目启动的时候立刻执行某些方 ...

  5. Spring Boot学习--项目启动时执行特定方法

    Springboot给我们提供了两种"开机启动"某些方法的方式:ApplicationRunner和CommandLineRunner. 这两种方法提供的目的是为了满足,在项目启动 ...

  6. Spring项目启动时执行初始化方法

    一.applicationContext.xml配置bean <bean id="sensitiveWordInitUtil" class ="com.hx.daz ...

  7. spring boot 项目在启动时执行指定sql文件

    参考博客: https://www.jianshu.com/p/88125f1cf91c 1. 启动时执行 当有在项目启动时先执行指定的sql语句的需求时,可以在resources文件夹下添加需要执行 ...

  8. Spring在Web容器启动时执行初始化方法

    需求:在tomcat启动时开启一个定时任务. 想法:容器启动时执行方法,最容易想到的就是servlet中可以配置load-on-startup,设置一个正整数也就可以随容器一起启动. 问题:上面的方法 ...

  9. SpringBoot项目启动时执行初始化操作

    SpringBooot中的CommandLineRunner接口会在所有Spring Beans初始化之后,SpringApplication.run()之前执行. 1.添加pom引用 <?xm ...

随机推荐

  1. 借助CSS Shapes实现元素滚动自动环绕iPhone X的刘海

    CSS代码: .box { max-width: 414px; height: 480px; border: solid #000; margin: auto; overflow: auto; } . ...

  2. AtCoder Grand Contest 11~17 做题小记

    原文链接https://www.cnblogs.com/zhouzhendong/p/AtCoder-Grand-Contest-from-11-to-20.html UPD(2018-11-16): ...

  3. 3186Treats for the Cows(区间dp)

    题意:给一个数组v,每次可以取前面的或者后面的,第k次取的v[i]价值为v[i]*k,问总价值最大是多少. 区间dp. 区间dp可以不枚举len  直接枚举i和j即可  见代码 #include &l ...

  4. Kmeans:利用Kmeans实现对多个点进行自动分类—Jason niu

    import numpy as np def kmeans(X, k, maxIt): numPoints, numDim = X.shape dataSet = np.zeros((numPoint ...

  5. Team Queue POJ - 2259 (队列)

    Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...

  6. Codeforces 1118F1 Tree Cutting (Easy Version) (简单树形DP)

    <题目链接> 题目大意: 给定一棵树,树上的点有0,1,2三中情况,0代表该点无色.现在需要你将这棵树割掉一些边,使得割掉每条边分割成的两部分均最多只含有一种颜色的点,即分割后的两部分不能 ...

  7. POJ 1321-棋盘问题【DFS+递归】

    题目链接 题目大意: Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大 ...

  8. P1168 中位数

    P1168 中位数树状数组+二分答案.树状数组就是起一个高效查询比二分出来的数小的有几个. #include<iostream> #include<cstdio> #inclu ...

  9. D - GCD HDU - 1695 -模板-莫比乌斯容斥

    D - GCD HDU - 1695 思路: 都 除以 k 后转化为  1-b/k    1-d/k中找互质的对数,但是需要去重一下  (x,y)  (y,x) 这种情况. 这种情况出现 x  ,y ...

  10. Android:ViewGroup和View的Touch事件

    Android中ViewGroup和View中的Touch事件传递机制分析 关键字:GroupView:View:Touch事件 基础知识: onInterceptTouchEvent():在View ...