应用场景:当你需要往Bean里注入一个其父类中定义的属性,而你又无法复写父类的属性或属性的setter方法时

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
private SessionFactory mySessionFacotry;
@Resource
public void setMySessionFacotry(SessionFactory sessionFacotry) {
this.mySessionFacotry = sessionFacotry;
}
@PostConstruct
public void injectSessionFactory() {
super.setSessionFactory(mySessionFacotry);
}
...
}

这里通过@PostConstruct,为UserDaoImpl的父类里定义的一个sessionFactory私有属性,注入了我们自己定义的sessionFactory(父类的setSessionFactory方法为final,不可复写),之后我们就可以通过调用 super.getSessionFactory()来访问该属性了。

Java源码:

//Copyright Sun Microsystems Inc. 2004 - 2005.

package javax.annotation;

import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*; /**
* The PostConstruct annotation is used on a method that needs to be executed
* after dependency injection is done to perform any initialization. This
* method MUST be invoked before the class is put into service. This
* annotation MUST be supported on all classes that support dependency
* injection. The method annotated with PostConstruct MUST be invoked even
* if the class does not request any resources to be injected. Only one
* method can be annotated with this annotation. The method on which the
* PostConstruct annotation is applied MUST fulfill all of the following
* criteria -
- The method MUST NOT have any parameters except in the case of EJB
* interceptors in which case it takes an InvocationC ontext object as
* defined by the EJB specification.
* - The return type of the method MUST be void.
* - The method MUST NOT throw a checked exception.
* - The method on which PostConstruct is applied MAY be public, protected,
* package private or private.
* - The method MUST NOT be static except for the application client.
* - The method MAY be final.
* - If the method throws an unchecked exception the class MUST NOT be put into
* service except in the case of EJBs where the EJB can handle exceptions and
* even recover from them.
* @since Common Annotations 1.0
* @see javax.annotation.PreDestroy
* @see javax.annotation.Resource
*/
@Documented
@Retention (RUNTIME)
@Target(METHOD)
public @interface PostConstruct {
}

PostConstruct注解的更多相关文章

  1. @PostConstruct 注解

    @PostConstruct 注解 /* * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. ...

  2. @PostConstruct注解小结

    1.在具体Bean的实例化过程中,@PostConstruct注解的方法,会在构造方法之后,init方法之前进行调用2.在项目中@PostConstruct主要应用场景是在初始化Servlet时加载一 ...

  3. springboot用@Autowired和@PostConstruct注解把config配置读取到bean变成静态方法

    springboot用@Autowired和@PostConstruct注解把config配置读取到bean变成静态方法 @SpringBootApplication public class Sen ...

  4. 【spring源码分析】spring和@PostConstruct注解

    @PostConstruct注解好多人以为是Spring提供的.其实是Java自己的注解. Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法.被@Pos ...

  5. @PostConstruct和@PostConstruct 注解 及ehcache 缓存 执行过程 小记

    @PostConstruct 和@PostConstruct 注解 从Java EE 5规范开始,Servlet中增加了两个影响Servlet生命周期的注解(Annotion):@PostConstr ...

  6. java的@PostConstruct注解

    javax.annotation 注释类型 PostConstruct @Documented @Retention(value=RUNTIME) @Target(value=METHOD) publ ...

  7. Spring@PostConstruct注解和构造方法的调用顺序

    先看下@PostConstruct的注解 * The PostConstruct annotation is used on a method that needs to be executed * ...

  8. @PostConstruct注解原理解析

    所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 正文 @PostConstruct注解使用简介 在了解一个东西的原理之前,我们得初步的懂得如何 ...

  9. 【String注解驱动开发】你了解@PostConstruct注解和@PreDestroy注解吗?

    写在前面 在之前的文章中,我们介绍了如何使用@Bean注解指定初始化和销毁的方法,小伙伴们可以参见<[Spring注解驱动开发]如何使用@Bean注解指定初始化和销毁的方法?看这一篇就够了!!& ...

  10. 【Java注解】@PostConstruct 注解相关

    不多逼逼,直接看注解上面的文档, @PostConsturct PostConstruct注释用于需要执行的方法在依赖注入完成后执行任何初始化.这个方法必须在类投入服务之前调用. 这个所有支持依赖关系 ...

随机推荐

  1. React 相关资料

    learncodeacademy/react-js-tutorials MobX

  2. [ An Ac a Day ^_^ ] FZU 2030 括号问题 搜索

    FZU一直交不上去 先写在这吧 #include<stdio.h> #include<iostream> #include<algorithm> #include& ...

  3. Arch最小化安装LXDE桌面环境

    安装最小化的LXDE桌面环境: pacman -S lxde-common 安装LXDE Session: pacman -S lxsession 不安装这个没法登录进桌面环境 安装LXDE面板: p ...

  4. pc app 桌面打包

    进入 http://nwjs.io/  下载 创建web项目,在项目根目录 创建文件package.json并填写 1 2 3 4 5 6 7 {   "name": " ...

  5. python 列表、字典的方法

    # 列表最后新增元素 li = [11, 22, 33] print(li) li.append(44) # 对原列表最后增加一个元素 print(li) 执行如下: [11, 22, 33][11, ...

  6. Android JNI的使用浅析

    介绍JNI的好文章: http://blog.csdn.net/yuanzeyao/article/details/42418977 JNI技术对于多java开发的朋友相信并不陌生,即(java na ...

  7. JS全选

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  8. mac中Eclipse的快捷键

    查看某个类:command + shift +T 快速查看源代码中方法: command + o 选中某个类,command + t:查看此类的父类和子类 如果要导入一个类所在的包名,可以选中这个类, ...

  9. 使用NSURLProtocol实现UIWebView的离线缓存

    http://blog.csdn.net/youcanping2008/article/details/9240487

  10. Jena文档《An Introduction to RDF and the Jena RDF API》的译文

    前言本文是一篇对W3C的资源描述框架(RDF)和 Jena(一个Java的RDF API)的教程性介绍. 本文是为那些不熟悉RDF的, 以及那些通过建立原形可以达到最好学习效果的, 或是因为其他原因希 ...