Spring注解

来源:互联网 发布:算法工程师的数学工具 编辑:程序博客网 时间:2024/06/07 03:35
@Resource 注解被用来激活一个命名资源(named resource)的依赖注入,在JavaEE应用程序中,该注解被典型地转换为绑定于JNDI context中的一个对象。 Spring确实支持使用@Resource通过JNDI lookup来解析对象,默认地,拥有与@Resource注解所提供名字相匹配的“bean name(bean名字)”的Spring管理对象会被注入。 在下面的例子中,Spring会向加了注解的setter方法传递bean名为“dataSource”的Spring管理对象的引用。


@Autowire和@Resource的区别
@Autowire默认按照类型装配,默认情况下它要求依赖对象必须存在如果允许为null,可以设置它required属性为false,如果我们想使用按照名称装配,可以结合@Qualifier注解一起使用; 

@Resource默认按照名称装配,当找不到与名称匹配的bean才会按照类型装配,可以通过name属性指定,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象. 
注意:如果没有指定name属性,并且按照默认的名称仍然找不到依赖的对象时候,会回退到按照类型装配,但一旦指定了name属性,就只能按照名称装配了. 
建议使用@Resource。 


spring中autowire的用法  

Autowire模式就是在spring的声明文件里用作进行对象间的关联关系自动绑定的,就是在 spring beanfactory内的一个bean对其bean的引用可以自动进行,而不一定用ref=的方式显式声明。在reference的3.3.6节有详细 的介绍,autowire主要有5种模式: 



1 no 

不使用Autowire,引用关系显示声明,spring的reference也 建议不用autoware,因为这会破坏模块关系的可读性,原文如下: 

Note: as has already been mentioned, for larger applications, it is discouraged to use autowiring because it 

removes the transparency and the structure from your collaborating classes. 



2 byName 

用名称关联,如果指定了这种模式,如 

   <bean id="userManagerTarget" class="com.mdcchina.jianghu.logic.UserManager" autowire="byName"> 

       <property name="baseDAO"/> 

   </bean> 

这样对于bean userManagerTarget的属性baseDAO,spring就会自动去引用同名的bean,也就是上面的声明和下面是等价的: 

   <bean id="userManagerTarget" class="com.mdcchina.jianghu.logic.UserManager" autowire="no"> 

       <property name="baseDAO"> 

          <ref local="baseDAO"/> 

       </property> 

   </bean> 



3 byType 

和前面的byName类似的,就是这个属性会在整个beanFactory定义里 找和这个属性一样的bean自动关联上,如果有2个或更多这个类型的bean在beanFactory的定义里,就直接抛异常了,如果没有,就什么都不发 生,这个属性就是null,所以这个只适用与这个属性的类型有且只有一个同类的bean在spring里定义 



4 constructor 

这个的意思我没有确定的把握,不过感觉用途也不会大,好像是用构造函数新建一个属 性类型的bean并关联上,reference原文是: 

This is analogous to byType, but applies to constructor arguments. If there isn't exactly one 

bean of the constructor argument type in the bean factory, a fatal error is raised. 



5 autodetect 

这个的意思好像是说自动决定用byType还是constructor方式,原文 如下: 

Chooses constructor or byType through introspection of the bean class. If a default 

constructor is found, byType gets applied. 


@Service
用于标注业务层组件

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

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

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


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

@Service用于标注业务层组件

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

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

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

[java] view plain copy
  1. @Service  
  2. public class VentorServiceImpl implements iVentorService {     
  3. }  
  4. @Repository  
  5. public class VentorDaoImpl implements iVentorDao {   
  6. }  

 

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

代码

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>   
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.                 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.         http://www.springframework.org/schema/context  
  8.         http://www.springframework.org/schema/context/spring-context-2.5.xsd">   
  9.      
  10.     <context:component-scan base-package=”com.eric.spring”>     
  11. </beans>   

1.component-scan标签默认情况下自动扫描指定路径下的包(含所有子包),将带有@Component、@Repository、@Service、@Controller标签的类自动注册到spring容器。对标记了 Spring's @Required、@Autowired、JSR250's @PostConstruct、@PreDestroy、@Resource、JAX-WS's @WebServiceRef、EJB3's @EJB、JPA's @PersistenceContext、@PersistenceUnit等注解的类进行对应的操作使注解生效(包含了annotation-config标签的作用)。

 

getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“aaaaa”)这样来指定。
这种bean默认是“singleton”的,如果想改变,可以使用@Scope(“prototype”)来改变。

可以使用以下方式指定初始化方法和销毁方法:

[java] view plain copy
  1. @PostConstruct  
  2. public void init() {   
  3. }   
  4. @PreDestroy  
  5. public void destory() {   
  6. }   


 

注入方式:

把DAO实现类注入到action的service接口(注意不要是service的实现类)中,注入时不要new 这个注入的类,因为spring会自动注入,如果手动再new的话会出现错误,
然后属性加上@Autowired后不需要getter()和setter()方法,Spring也会自动注入。   

在接口前面标上@Autowired注释使得接口可以被容器注入,如:

[java] view plain copy
  1. @Autowired  
  2. @Qualifier("chinese")  
  3. private Man man;   

当接口存在两个实现类的时候必须使用@Qualifier指定注入哪个实现类,否则可以省略,只写@Autowired。
待续。。。。


原创粉丝点击