Spring的注解

一、几个常用的注解

      对Spring的注解标签刚刚接触,所以就找了几个常用的,记录下,感觉注解用了之后,会在*.xml文件中大大减少配置量。以前我们每个Bean都需到配置文件中配置关联下。spring2.5后,引入了完整的annotation配置注解,使得我们的程序配置更简单更容易维护。

        @Component;@Controller;@Service ;@Repository
annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spring容器管理的类。即就是该类已经拉入到spring的管理中了。而@Controller@Service ,@Repository@Component的细化,这三个注解比@Component带有更多的语义,它们分别对应了控制层、服务层、持久层的类。

 @Repository标签是用来给持久层的类定义一个名字,让Spring根据这个名字关联到这个类。

例如:
@Repository("userDao")

1 <strong>public class UserDaoImpl  implements UserDao{
2  
3 }</strong>

声明了UserDaoImpl  在Spring容器中叫userDao这个名字。

@Service是用于服务层的IServiceImpl类文件,功能与@Repository类似。


另外标签:@Autowired 用来注入。

例如:

1 @Autowired
2 private UserDao userDao;

这样就注入进去了,相当于我们new了个实现类,我们就无需写setter方法了。


我们还得有配置文件进行配置:

01 <?xml version="1.0" encoding="UTF-8"?>
02 <beans xmlns="http://www.springframework.org/schema/beans"
03        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04        xmlns:aop="http://www.springframework.org/schema/aop"
05        xmlns:tx="http://www.springframework.org/schema/tx"
06        xmlns:context="http://www.springframework.org/schema/context"
07       xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
08      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
09      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
10      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
11  
12     <context:annotation-config/>
13     <context:component-scan base-package="com.zxr.manager">
14         <context:include-filter type="regex" expression=".*DaoImpl"/>
15         <context:include-filter type="regex" expression=".*ServiceImpl"/>
16     </context:component-scan>
17 </beans>

这样就把com.zxr.manager包下的所有.*DaoImpl,.*ServiceImpl都注册关联到Spring容器中去了。



二、各种注解方式

1.@Autowired注解(不推荐使用,建议使用@Resource)

@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。@Autowired的标注位置不同,它们都会在Spring在初始化这个bean时,自动装配这个属性。要使@Autowired能够工作,还需要在配置文件中加入以下

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


2. @Qualifier注解

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

1). 可能存在多个UserDao实例

Java代码

1 @Autowired
2 @Qualifier("userServiceImpl")
3 public IUserService userService;

或者

1 @Autowired
2 public void setUserDao(@Qualifier("userDao") UserDao userDao) {
3 this.userDao = userDao;
4 }

这样,Spring会找到id为userServiceImpl和userDao的bean进行装配。

2). 可能不存在UserDao实例

Java代码

1 @Autowired(required = false)
2 public IUserService userService;


3. @Resource注解

JSR-250标准注解,推荐使用它来代替Spring专有的@Autowired注解。@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入罢了。@Resource有两个属性是比较重要的,分别是name和type,Spring将 @Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。要使@Autowired能够工作,还需要在配置文件中加入以下:

1 <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />


@Resource装配顺序:

a.如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常

b.如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常

c.如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常

d.如果既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);如果没有匹配,则回退为一个原始类型(UserDao)进行匹配,如果匹配则自动装配;


4. @PostConstruct(JSR-250)注解

在方法上加上注解@PostConstruct,这个方法就会在Bean初始化之后被Spring容器执行(注:Bean初始化包括,实例化Bean,并装配Bean的属性(依赖注入))。它的一个典型的应用场景是,当你需要往Bean里注入一个其父类中定义的属性,而你又无法复写父类的属性或属性的setter方法时,如:

Java代码

01 public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
02 private SessionFactory mySessionFacotry;
03 @Resource
04 public void setMySessionFacotry(SessionFactory sessionFacotry)
05 {
06 this.mySessionFacotry = sessionFacotry;
07 }
08 @PostConstruct
09 public void injectSessionFactory()
10 {
11 super.setSessionFactory(mySessionFacotry);
12 }
13 }

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


5. @PreDestroy(JSR-250)注解

在方法上加上注解@PreDestroy,这个方法就会在Bean初始化之后被Spring容器执行。其用法同@PostConstruct。和@PostConstruct 区别在于:@PostConstruct注释的方法将在类实例化后调用,而标注了 @PreDestroy 的方法将在类销毁之前调用。


6. @Component注解 (不推荐使用)

只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了。Spring还提供了更加细化的注解形式:@Repository、@Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。目前版本(2.5)中,这些注解与@Component的语义是一样的,完全通用,在Spring以后的版本中可能会给它们追加更多的语义。所以,我们推荐使用@Repository、@Service、@Controller来替代@Component。


7.@Scope注解

在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过@Scope注解来完成这项工作:

Java代码

1 @Scope("session")
2 @Component()
3 public class UserSessionBean implements Serializable{
4 ... ...
5 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章