`

spring注解

阅读更多
可以说,在Java开发中离不开Spring。在Spring 2.5及以后的版本中,提供了注释和命名空间来简化Spring的配置。
本文就在实际应用中,把最常用的注释和配置做个简单的整理和介绍,也就是本人使用最多的一些功能,
其他更高级的功能可以参考Spring官方文档(http://www.springsource.org/documentation)或是通过Google一下。

一、@Autowired注释
以前给一个Bean配置属性时,Bean必须配置<property name="propName" ref="beanId"/>,然后在Java文件,还必须增加属性propname的getter和setter方法。
有了@Autowired注释后,我们可以简化配置文件和getters和setters方法。


1、注释属性
@Autowired
private BeanClassName propName;
当然,我们还必须告诉Spring容器,当它启动时,就去扫描所有的Bean,然后自动注入。
<!-- 对具有@Autowired注释的Bean进行注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
我们也可以注释构造函数和方法。
2、注释构造函数
@Autowired
public MainBean(PropBeanA propBeanA, PropBeanB propBeanB){
    this.propBeanA = propBeanA;
    this.PropBeanB = PropBeanB ;
}
3、注释方法
@Autowired
public void setPropBean(PropBean propBean) {
    this.propBean = propBean;
}
4、@Autowired的相关属性
在默认情况下使用@Autowired注释进行自动注入时,Spring容器中匹配的候选Bean数目必须有且仅有一个。当Springp容器找不到一个匹配的Bean时(可能是没有配置该Bean或是Bean的属性名写错了),Spring容器将抛出BeanCreationException异常,并指出必须至少拥有一个匹配的Bean。
(1)当Bean是可选的,或是不能确定Bean一定有,可以用@Autowired(required = false),这样在找不到匹配Bean 时也不报错,该属性默认为true。
(2)与上面的相反,当Bean有多个时,我们可以通过@Qualifier("beanId")还唯一确定一个所引用的Bean,与@Autowired配合使用,它也有三种使用的地方。

二、@Component注释
虽然我们可以通过@Autowired在Bean类中使用自动注入功能,但是Bean还是在XML文件中通过<bean>进行定义的,
通过@Component注释可以实现无需在配置文件中配置Bean的目的。

import org.springframework.stereotype.Component;
@Component
public class BeanClassNameA {
// ......
}
使用@Autowired
@Component
public class BeanClassNameB {
@Autowired
    private BeanClassNameA propBeanA;

// ......
}
因为没有配置,我们必须告诉Spring容器启用类扫描机制并自动注入了:
<context:annotation-config />
前面的AutowiredAnnotationBeanPostProcessor可以去掉了,这行的作用后面介绍。
(1)@Component有个可选参数,指定Bean的名称@Component("beanId")
(2)与@Component配合使用,可以通过@Scope指定Bean的作用范围,比如:@Scope("prototype")

三、其他注释
Spring除了提供@Component注释外,还定义了几个注释:@Repository、@Service 和 @Controller。
在目前的Spring 2.5中,这3个注释和@Component是等效的,但是从注释类的命名上,很容易看出这3个注释分别和持久层、业务层和控制层(Web 层)相对应。

虽然目前这3个注释和@Component相比没有什么新意,但Spring将在以后的版本中为它们添加特殊的功能。

所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用@Repository、@Service和 @Controller对分层中的类进行注释,而用@Component对那些比较中立的类进行注释。

四、<context:annotation-config />
<context:annotation-config />将隐式地向Spring容器注册 AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor。

五、<context:component-scan base-package="......" />
通过<context:component-scan base-package="com.packname.common" />,Spring会递归扫描类注释。
具体的说明可以参考:http://springindepth.com/book/annotation-based-bean-config-ioc-container-configuration.html

六、JavaEE应用@Controller
在使用SpringMVC中,可以通过@Controller简化配置
@Controller
@RequestMapping("/welcome.htm")
public class SomeNameController {
@RequestMapping(method = RequestMethod.GET)
    public void doGet(ModelMap model, WebRequest request) {
   // ...
}
}
关于@Controller,在以后的文章中介绍,敬请关注。

七、简化配置文件
对于属性,我们一般都是通过以下配置的:
<bean id="transactionManager"
   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource" />
</bean>
简化之后为:
<bean id="transactionManager"
   class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
   p:dataSource-ref="dataSource" />
使用方法介绍,我相信就没必要了,注意这两种方式可以一起使用的。

八、命名空间
在以上的<context:.... />和<bean id=".." p:name-ref="" />中,我们用到了context和p命名空间,因此必须在配置文件中申明,我的如下:
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:osgi="http://www.springframework.org/schema/osgi" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName">
XSD文件版本小技巧:不要增加版本(如直接使用spring-context.xsd),当然要加也是spring-context-2.5.xsd

九、最后
由于介绍内容比较多,感觉都比较空,还有一些没有介绍具体:
(1)JavaEE的注释,主要是@Controller;
(2)JPA注释,@Entity,@Table;
(3)JSR-250的注释,@Resource、@PostConstruct、@PreDestroy等。
以上两上以后文章再做具体介绍。

下面贴出我的Spring配置文件的内容。
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:osgi="http://www.springframework.org/schema/osgi" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName">

<!-- Activates annotation-based bean configuration -->
<context:component-scan base-package="com.alipay.common" />

<!-- DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
   destroy-method="close">
   <property name="driverClassName" value="com.mysql.jdbc.Driver" />
   <property name="url"
    value="jdbc:mysql://localhost:3306/work_shop?useUnicode=true&amp;characterEncoding=UTF-8" />
   <property name="username" value="root" />
   <property name="password" value="manager" />
</bean>

<!-- SqlMapClient -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
   <property name="configLocations">
    <list>
     <value>classpath:sqlmap/sqlmap-config.xml</value>
    </list>
   </property>
</bean>

<!-- SqlMapClientDAO -->
<bean id="sqlMapClientDAO" abstract="true" p:dataSource-ref="dataSource"
   p:sqlMapClient-ref="sqlMapClient" />

<!-- TransactionTemplate -->
<bean id="transactionManager"
   class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
   p:dataSource-ref="dataSource" />

<!-- TransactionManager -->
<bean id="mockTransactionTemplate"
   class="org.springframework.transaction.support.TransactionTemplate"
   p:transactionManager-ref="transactionManager" />

<!-- DAO -->
<bean id="siteUserDAO" class="com.alipay.dal.ibatis.IbatisSiteUserDAO"
   parent="sqlMapClientDAO" />
<!-- END OF DAO -->

<!-- MANAGER -->
<bean id="siteUserManager" class="com.alipay.biz.manager.impl.SiteUserManagerImpl" />
<!-- END OF MANAGER -->

<!-- FACADE-->

<!-- END OF FACADE-->

<!-- OSGI SERVICE-->

<!-- END OF OSGI SERVICE-->
</beans>

Spring注解@Component、@Repository、@Service、@Controller区别

Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller。在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释。

在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便。 Spring2.5为我们引入了组件自动扫描机制,他可以在类路径底下寻找标注了 @Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件时一样的。要使用自动扫描机制,我们需要打开以下配置信息:
Java代码

1. <?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-2.5.xsd   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-2.5.xsd" 
2. > 
3.  
4. <context:component-scan base-package=”com.eric.spring”>  
5. </beans>  
6. 其中base-package为需要扫描的包(含所有子包) @Service用于标注业务层组件,@Controller用于标注控制层组件(如struts中的action),@Repository用于标注数据访问组件,即DAO组件,而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。  
7. @Service public class VentorServiceImpl implements iVentorService {  
8. } @Repository public class VentorDaoImpl implements iVentorDao { 
9. } getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“aaaaa”)这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”) @Scope(“prototype”)来改变。可以使用以下方式指定初始化方法和销毁方法(方法名任意): @PostConstruct public void init() { 
10. } 
11. @PreDestroy public void destory() { 
12. }



注入方式:

把DAO实现类注入到service实现类中,把service的接口(注意不要是service的实现类)注入到action中,注

入时不要new 这个注入的类,因为spring会自动注入,如果手动再new的话会出现错误,然后属性加上

@Autowired后不需要getter()和setter()方法,Spring也会自动注入。至于更具体的内容,等对注入的方式更

加熟练后会做个完整的例子上来。

注解:

在 spring的配置文件里面只需要加上<context:annotation-config/> 和<context:component-scan base-package="需要实现注入的类所在包"/>,可以使用base-package="*"表示全部的类。  

<context:component-scan base-package=”com.eric.spring”>

其中base-package为需要扫描的包(含所有子包)

在接口前面标上@Autowired和@Qualifier注释使得接口可以被容器注入,当接口存在两个实现类的时候必须指定其中一个来注入,使用实现类首字母小写的字符串来注入,如:

    @Autowired     
  
    @Qualifier("chinese")      
  
    private Man man;   
否则可以省略,只写@Autowired   。

@Service服务层组件,用于标注业务层组件,表示定义一个bean,自动根据bean的类名实例化一个首写字母为小写的bean,例如Chinese实例化为chinese,如果需要自己改名字则:@Service("你自己改的bean名")。  

@Controller用于标注控制层组件(如struts中的action)

@Repository持久层组件,用于标注数据访问组件,即DAO组件

@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。


@Service
public class VentorServiceImpl implements iVentorService {
}

@Repository
public class VentorDaoImpl implements iVentorDao {
}

getBean 的默认名称是类名(头字母小写),如果想自定义,可以@Service(“aaaaa”) 这样来指定,这种

bean默认是单例的,如果想改变,可以使用@Service(“beanName”) @Scope(“prototype”)来改变。

可以使用以下方式指定初始化方法和销毁方法(方法名任意):

@PostConstruct

public void init() {

}

@PreDestroy

public void destory() {

}


spring @Qualifier


当候选 Bean 数目不为 1 时的应对方法

在默认情况下使用

@Autowired

注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个。当找不到一个匹配的 Bean 时,Spring 容器将抛出

BeanCreationException

异常,并指出必须至少拥有一个匹配的 Bean。我们可以来做一个实验:

清单 10. 候选 Bean 数目为 0 时



<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd "> <bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor"/> <bean id="boss" class="com.baobaotao.Boss"/> <!-- 将 office Bean 注释掉 --> <!-- <bean id="office" class="com.baobaotao.Office"> <property name="officeNo" value="001"/> </bean>--> <bean id="car" class="com.baobaotao.Car" scope="singleton"> <property name="brand" value=" 红旗 CA72"/> <property name="price" value="2000"/> </bean></beans>


由于

office

Bean 被注释掉了,所以 Spring 容器中将没有类型为

Office

的 Bean 了,而 Boss 的

office

属性标注了

@Autowired

,当启动 Spring 容器时,异常就产生了。

当不能确定 Spring 容器中一定拥有某个类的 Bean 时,可以在需要自动注入该类 Bean 的地方可以使用

@Autowired(required = false)

,这等于告诉 Spring:在找不到匹配 Bean 时也不报错。来看一下具体的例子:

清单 11. 使用 @Autowired(required = false)



package com.baobaotao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Required;public class Boss { private Car car; private Office office; @Autowired public void setCar(Car car) { this.car = car; } @Autowired(required = false) public void setOffice(Office office) { this.office = office; } …}


当然,一般情况下,使用

@Autowired

的地方都是需要注入 Bean 的,使用了自动注入而又允许不注入的情况一般仅会在开发期或测试期碰到(如为了快速启动 Spring 容器,仅引入一些模块的 Spring 配置文件),所以

@Autowired(required = false)

会很少用到。

和找不到一个类型匹配 Bean 相反的一个错误是:如果 Spring 容器中拥有多个候选 Bean,Spring 容器在启动时也会抛出

BeanCreationException

异常。来看下面的例子:

清单 12. 在 beans.xml 中配置两个 Office 类型的 Bean



… <bean id="office" class="com.baobaotao.Office"> <property name="officeNo" value="001"/></bean><bean id="office2" class="com.baobaotao.Office"> <property name="officeNo" value="001"/></bean>…


我们在 Spring 容器中配置了两个类型为

Office

类型的 Bean,当对 Boss 的

office

成员变量进行自动注入时,Spring 容器将无法确定到底要用哪一个 Bean,因此异常发生了。

Spring 允许我们通过

@Qualifier

注释指定注入 Bean 的名称,这样歧义就消除了,可以通过下面的方法解决异常:

清单 13. 使用 @Qualifier 注释指定注入 Bean 的名称



@Autowiredpublic void setOffice(@Qualifier("office")Office office) { this.office = office;}




@Qualifier("office")

中的

office

是 Bean 的名称,所以

@Autowired



@Qualifier

结合使用时,自动注入的策略就从 byType 转变成 byName 了。

@Autowired

可以对成员变量、方法以及构造函数进行注释,而

@Qualifier

的标注对象是成员变量、方法入参、构造函数入参。正是由于注释对象的不同,所以 Spring 不将

@Autowired



@Qualifier

统一成一个注释类。下面是对成员变量和构造函数入参进行注释的代码:

对成员变量进行注释:

清单 14. 对成员变量使用 @Qualifier 注释



public class Boss { @Autowired private Car car; @Autowired @Qualifier("office") private Office office; …}


对构造函数入参进行注释:

清单 15. 对构造函数变量使用 @Qualifier 注释



public class Boss { private Car car; private Office office; @Autowired public Boss(Car car , @Qualifier("office")Office office){ this.car = car; this.office = office ; }}




@Qualifier

只能和

@Autowired

结合使用,是对

@Autowired

有益的补充。一般来讲,

@Qualifier

对方法签名中入参进行注释会降低代码的可读性,而对成员变量注释则相对好一些。


// ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// UserService userService= (UserService)ctx.getBean("userService");

factory-method 动态传参数(转)


class ExampleBean {
      private String string;
      public ExampleBean(String string) {
        this.string = string;
      }
     
      public void write() {
        System.out.println("The text is: " + text);
      }
   
   }
   
   class ExampleBeanFactory {
     public static ExampleBean createExampleBean(String string) {
       return new ExampleBean(string);
     }
   }
   
   public class Main {
     public static void main(String[] args) {
       ApplicationContext context = new ClassPathXmlApplicationContext(
                       "context.xml");
       ExampleBean exampleBean =
               ExampleBean)context.getBean("exampleBean",
                 new Object[]{"bla bla"});
       exampleBean.write();
     }
   }
    <bean id="exampleBean" class="...ExampleBeanFactory"  scope="prototype"
            factory-method="createExampleBean">
        <constructor-arg value="default value"/>
    </bean>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics