您的位置:首页 > 编程语言 > Java开发

Spring AOP常用注解解析

2017-08-07 10:56 766 查看
Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
本文介绍了使用Spring注解注入属性的方法。使用注解以前,注入属性通过类以及配置文件来实现。现在,注入属性可以通过引入@Autowired注解,或者@Resource,@Qualifier,@PostConstruct,@PreDestroy等注解来实现。

1.1. 使用注解以前我们是怎样注入属性的

类的实现:

1
public class UserManagerImpl implements UserManager {
2
private UserDao userDao;
3
public void setUserDao(UserDao userDao) {
4
this.userDao = userDao;
5
}
6
...
7
}
配置文件:

1
<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl">
2
<property name="userDao" ref="userDao" />
3
</bean>
4
<bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">
5
<property name="sessionFactory" ref="mySessionFactory" />
6
</bean>
1.2. 引入@Autowired注解(不推荐使用,建议使用@Resource)

类的实现(对成员变量进行标注)

1
public class UserManagerImpl implements UserManager {
2
@Autowired
3
private UserDao userDao;
4
...
5
}
或者(对方法进行标注)

1
public class UserManagerImpl implements UserManager {
2
private UserDao userDao;
3
@Autowired
4
public void setUserDao(UserDao userDao) {
5
this.userDao = userDao;
6
}
7
...
8
}
配置文件

1
<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl" />
2
<bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">
3
<property name="sessionFactory" ref="mySessionFactory" />
4
</bean>
@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。以上两种不同实现方式中,@Autowired的标注位置不同,它们都会在Spring在初始化userManagerImpl这个bean时,自动装配userDao这个
4000
属性,区别是:第一种实现中,Spring会直接将UserDao类型的唯一一个bean赋值给userDao这个成员变量;第二种实现中,Spring会调用 setUserDao方法来将UserDao类型的唯一一个bean装配到userDao这个属性。

1.3. 让@Autowired工作起来

要使@Autowired能够工作,还需要在配置文件中加入以下代码

1
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
1.4. @Qualifier

@Autowired是根据类型进行自动装配的。在上面的例子中,如果当Spring上下文中存在不止一个UserDao类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存在UserDao类型的bean,也会抛出 BeanCreationException异常。我们可以使用@Qualifier配合@Autowired来解决这些问题。

a. 可能存在多个UserDao实例

1
@Autowired
2
public void setUserDao(@Qualifier("userDao") UserDao userDao) {
3
this.userDao = userDao;
4
}
这样,Spring会找到id为userDao的bean进行装配。

b. 可能不存在UserDao实例

1
@Autowired(required = false)
2
public void setUserDao(UserDao userDao) {
3
this.userDao = userDao;
4
}
1.5. @Resource(JSR-250标准注解,推荐使用它来代替Spring专有的@Autowired注解)

Spring 不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource、@PostConstruct以及@PreDestroy。

@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分别是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。

@Resource装配顺序
1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);如果没有匹配,则回退为一个原始类型(UserDao)进行匹配,如果匹配则自动装配;

1.6. @PostConstruct(JSR-250)

在方法上加上注解@PostConstruct,这个方法就会在Bean初始化之后被Spring容器执行(注:Bean初始化包括,实例化Bean,并装配Bean的属性(依赖注入))。

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

01
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
02
private SessionFactory mySessionFacotry;
03
@Resource
04
public void setMySessionFacotry(SessionFactory sessionFacotry) {
05
this.mySessionFacotry = sessionFacotry;
06
}
07
@PostConstruct
08
public void injectSessionFactory() {
09
super.setSessionFactory(mySessionFacotry);
10
}
11
...
12
}
这里通过@PostConstruct,为UserDaoImpl的父类里定义的一个sessionFactory私有属性,注入了我们自己定义的sessionFactory(父类的setSessionFactory方法为final,不可复写),之后我们就可以通过调用 super.getSessionFactory()来访问该属性了。

1.7. @PreDestroy(JSR-250)

在方法上加上注解@PreDestroy,这个方法就会在Bean初始化之后被Spring容器执行。由于我们当前还没有需要用到它的场景,这里不不去演示。其用法同@PostConstruct。

1.8. 使用< context:annotation-config />简化配置

Spring2.1添加了一个新的context的Schema命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。

AutowiredAnnotationBeanPostProcessor和 CommonAnnotationBeanPostProcessor就是处理这些注释元数据的处理器。但是直接在Spring配置文件中定义这些 Bean显得比较笨拙。Spring为我们提供了一种方便的注册这些BeanPostProcessor的方式,这就是< context:annotation-config />:

1
<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"
2
xsi:schemaLocation="http://www.springframework.org/schema/beans
3 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 4 http://www.springframework.org/schema/context 5 http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 6
< context:annotation-config />
7
</beans>
<context:annotationconfig />将隐式地向Spring容器注册AutowiredAnnotationBeanPostProcessor、 CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor以及 RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor。

原文:http://www.blogjava.net/hellxoul/archive/2011/11/21/364419.html
================================================================================================================================================================================================================================================================

注解@PostConstruct与@PreDestroy详解及实例

标签:java ee 2016-08-10 18:04 3714人阅读 评论(0) 收藏 举报分类: Java基础(16)作者同类文章X 版权声明:本文为博主原创文章,转载请写明出处。目录(?)[+]简介API使用说明Servlet执行流程图实例项目应用注意事项

简介

Java EE5 引入了@PostConstruct和@PreDestroy这两个作用于Servlet生命周期的注解,实现Bean初始化之前和销毁之前的自定义操作。此文主要说明@PostConstruct。

API使用说明

以下为@PostConstruct的API使用说明:PostConstruct 注释用于在依赖关系注入完成之后需要执行的方法上,以执行任何初始化。此方法必须在将类放入服务之前调用。支持依赖关系注入的所有类都必须支持此注释。即使类没有请求注入任何资源,用 PostConstruct 注释的方法也必须被调用。只有一个方法可以用此注释进行注释。应用 PostConstruct 注释的方法必须遵守以下所有标准:该方法不得有任何参数,除非是在 EJB 拦截器 (interceptor) 的情况下,根据 EJB 规范的定义,在这种情况下它将带有一个 InvocationContext 对象 ;该方法的返回类型必须为 void;该方法不得抛出已检查异常;应用 PostConstruct 的方法可以是 public、protected、package private 或 private;除了应用程序客户端之外,该方法不能是 static;该方法可以是 final;如果该方法抛出未检查异常,那么不得将类放入服务中,除非是能够处理异常并可从中恢复的 EJB。总结为一下几点:只有一个方法可以使用此注释进行注解;被注解方法不得有任何参数;被注解方法返回值为void;被注解方法不得抛出已检查异常;被注解方法需是非静态方法;此方法只会被执行一次;

Servlet执行流程图

两个注解加入只会,Servlet执行流程图:在具体Bean的实例化过程中,@PostConstruct注释的方法,会在构造方法之后,init方法之前进行调用。

实例

基于spring boot编写的可执行方法见github:https://github.com/HappySecondBrother/exampleUserService方法(提供缓存数据):
package com.secbro.service;import org.springframework.stereotype.Service;import java.util.ArrayList;import java.util.List;/*** @author 二师兄* @date 2016/8/10*/@Servicepublic class UserService {public List<String> getUser(){List<String> list = new ArrayList<>();list.add("张三");list.add("李四");return list;}}
BusinessService方法,通过@PostConstruct调用UserService:
package com.secbro.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import javax.annotation.PostConstruct;import java.util.List;/*** @author 二师兄* @date 2016/8/10*/@Servicepublic class BusinessService {@Autowiredprivate UserService userService;private List<String> list = null;/*** 构造方法执行之后,调用此方法*/@PostConstructpublic void init(){System.out.println("@PostConstruct方法被调用");// 实例化类之前缓存获得用户信息List<String> list = userService.getUser();this.list = list;if(list != null && !list.isEmpty()){for(String user : list){Sy12eefstem.out.println("用户:" + user);}}}public BusinessService(){System.out.println("构造方法被调用");}public List<String> getList() {return list;}public void setList(List<String> list) {this.list = list;}}
执行结果:
构造方法被调用@PostConstruct方法被调用用户:张三用户:李四

项目应用

在项目中@PostConstruct主要应用场景是在初始化Servlet时加载一些缓存数据等。

注意事项

使用此注解时会影响到服务的启动时间。服务器在启动时会扫描WEB-INF/classes的所有文件和WEB-INF/lib下的所有jar包。================================================================================================================================================================================================================================================================Java开发之@PostConstruct和@PreConstruct注解      从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct。这两个注解被用来修饰一个非静态的void()方法.而且这个方法不能有抛出异常声明。使用方式,例如:
1@PostConstruct   //方式12public void someMethod(){3    ...4}56public @PostConstruct void someMethod(){   //方式27    ...8}
1.@PostConstruct说明     被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。2.@PreConstruct说明     被@PreConstruct修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreConstruct修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。(详见下面的程序实践)3.程序实践web.xml
1 <!-- @PostConstruct和@PreDestroy注解 -->2   <servlet>3<servlet-name>AnnotationServlet</servlet-name>4<servlet-class>com.servlet.AnnotationServlet</servlet-class>5   </servlet>6 <servlet-mapping>7<servlet-name>AnnotationServlet</servlet-name>8<url-pattern>/servlet/AnnotationServlet</url-pattern>9   </servlet-mapping>
AnnotationServlet
 1 package com.servlet;23 import java.io.IOException;4 import java.io.PrintWriter;5 import java.sql.Time;6 import java.text.SimpleDateFormat;7 import java.util.Date;89 import javax.annotation.PostConstruct;10 import javax.annotation.PreDestroy;11 import javax.servlet.ServletException;12 import javax.servlet.http.HttpServlet;13 import javax.servlet.http.HttpServletRequest;14 import javax.servlet.http.HttpServletResponse;1516 public class AnnotationServlet extends HttpServlet {17SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");//设置日期格式,精确到毫秒1819public AnnotationServlet(){20    System.out.println("时间:"+df.format(new Date())+"执行构造函数...");21}2223public void destroy() {24    this.log("时间:"+df.format(new Date())+"执行destroy()方法...");25    //super.destroy(); // Just puts "destroy" string in log26    // Put your code here27}2829@PostConstruct30public void someMethod(){31    //this.log("执行@PostConstruct修饰的someMethod()方法...");//注意:这样会出错32    System.out.println("时间:"+df.format(new Date())+"执行@PostConstruct修饰的someMethod()方法...");33}3435@PreDestroy36public void otherMethod(){37    System.out.println("时间:"+df.format(new Date())+"执行@PreDestroy修饰的otherMethod()方法...");38}3940public void doGet(HttpServletRequest request, HttpServletResponse response)41   throws ServletException, IOException {42    this.log("时间:"+df.format(new Date())+"执行doGet()方法...");43}4445public void init() throws ServletException {46    // Put your code here47    this.log("时间:"+df.format(new Date())+"执行init()方法...");48}4950protected void service(HttpServletRequest request, HttpServletResponse response)51 throws ServletException, IOException{52    this.log("时间:"+df.format(new Date())+"执行service()方法...");53    super.service(request, response);54}5556 }
运行结果:4.注意事项     注解多少会影响服务器的启动速度。服务器在启动的时候,会遍历Web应用的WEB-INF/classes下的所有class文件与WEB-INF/lib下的所有jar文件,以检查哪些类使用了注解。如果程序中没有使用任何注解,可以在web.xml中设置<web-app>的metadatacomplete属性为true来关掉服务器启动时的例行检查。           支持注解的服务器需要支持到Servlet2.5及以上规范,所以Tomcat要6.0.X及以上版本才行。================================================================================================================================================================================================================================================================

通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

2013-03-16 16:48 147894人阅读 评论(9) 收藏 举报分类: Spring(15)作者同类文章X 版权声明:本文为博主原创文章,未经博主允许不得转载。关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种:第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作第二种是:通过 在xml中定义init-method 和  destory-method方法第三种是: 通过bean实现InitializingBean和 DisposableBean接口下面演示通过  @PostConstruct 和 @PreDestory1:定义相关的实现类:[java] view plain copy print?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);      }        }  
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;}@PostConstructpublic void  init(){System.out.println("I'm  init  method  using  @PostConstrut...."+message);}@PreDestroypublic void  dostory(){System.out.println("I'm  destory method  using  @PreDestroy....."+message);}}
2:定义相关的配置文件:[html] view plain copy print?<?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>  
<?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 容器采用注解配置:扫描注解配置;测试类:[java] view plain copy print?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();      }    }  
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....123I'm  destory method  using  @PreDestroy.....123其中也可以通过申明加载org.springframework.context.annotation.CommonAnnotationBeanPostProcessor类来告诉Spring容器采用的 常用 注解配置的方式:只需要修改配置文件为:[html] view plain copy print?<?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>  
<?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>
同样可以得到以上测试的输出结果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: