JSR330注解和Spring注解对比

来源:互联网 发布:分视角情感分析算法 编辑:程序博客网 时间:2024/06/10 23:57
JSR330注解和Spring注解对比:

JSR330中的 @Inject  代替了 Spring  注解中的@AutoWired

JSR330中的@Named代替了Spring 注解中的@Component


spring  3.0 支持JSR330标准; JSR330标准和spring  annotation都是通过自动扫描的方式集成各个组件,用JSR330需要下列maven配置:


[html] view plain copy
  1. <dependency>  
  2.     <groupId>javax.inject</groupId>  
  3.     <artifactId>javax.inject</artifactId>  
  4.     <version>1</version>  
  5.  </dependency>  


一:首先是利用Spring  annotation 构建项目

1:首先配置容器组件自动扫描在xml文件中,确定扫描的类

[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-3.1.xsd  
  7. http://www.springframework.org/schema/context  
  8. http://www.springframework.org/schema/context/spring-context-3.1.xsd">  
  9.   
  10. <context:component-scan  base-package="com.myapp.core.annotation"/>  
  11.   
  12. </beans>  

2:定义相关的类

dao层
[java] view plain copy
  1. package com.myapp.core.annotation;  
  2.   
  3. import org.springframework.stereotype.Repository;  
  4.   
  5. @Repository  
  6. public class PersonDao {  
  7.     public void  save(){  
  8.         System.out.println("Person Dao save method  is invoked");  
  9.           
  10.     }  
  11. }  
service层
[java] view plain copy
  1. <span style="font-size:10px;">package com.myapp.core.annotation;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Service;  
  5.   
  6. @Service  
  7. public class PersonService {  
  8.   
  9.   @Autowired      
  10.   private  PersonDao  personDao;  
  11.     
  12.   public   void setPersonDao(PersonDao personDao){  
  13.       this.personDao = personDao;  
  14.   }  
  15.     
  16.   public  void   save(){  
  17.       System.out.println("Person Service  save  method is invoked");  
  18.       personDao.save();  
  19.   }  
  20. }  
  21. </span>  

以上类中用到的@Repository 用于生命是dao层,@Service声明是service层, 组件扫描就是扫描这些类,如果没有了这些声明,这些类讲不能得到扫描,就无法注入。所以声明是必须的。声明只是为了扫描这个类用,如果为了验证这个道理,可以把PersonDao中的 @Repository 改为 @Service程序依然正常运行,如果去掉就无法注入,会报错。
测试类:
[java] view plain copy
  1. <span style="font-size:10px;">package com.myapp.core.annotation;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class MainTest {  
  7.   public static void main(String[] args) {  
  8.       
  9.       ApplicationContext  context = new  ClassPathXmlApplicationContext("resource/annotation.xml");  
  10.      
  11.        PersonService  personService = (PersonService) context.getBean("personService");  
  12.          
  13.        personService.save();  
  14.   }  
  15. }  
  16. </span>  


测试结果:
Person Service  save  method is invoked
Person Dao save method  is invoked
以上是通过 Spring annotation进行注入;

二:利用 JSR330 进行注入:

1:配置容器扫描类:

[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-3.1.xsd  
  7. http://www.springframework.org/schema/context  
  8. http://www.springframework.org/schema/context/spring-context-3.1.xsd">  
  9.   
  10. <context:component-scan  base-package="com.myapp.core.jsr330"/>  
  11.   
  12. </beans>  

上面改动只是改变了Spring  container 的 扫描的包

2:配置相应的类:

[java] view plain copy
  1. package com.myapp.core.jsr330;  
  2.   
  3. import javax.inject.Named;  
  4.   
  5. @Named  
  6. public class PersonDao {  
  7.     public void  save(){  
  8.         System.out.println("Person Dao save method  is invoked");  
  9.           
  10.     }  
  11. }  

[java] view plain copy
  1. package com.myapp.core.jsr330;  
  2.   
  3. import javax.inject.Inject;  
  4.   
  5. import javax.inject.Named;  
  6.   
  7. @Named  
  8. public class PersonService {  
  9.   
  10.   @Inject     
  11.   private  PersonDao  personDao;  
  12.     
  13.   public   void setPersonDao(PersonDao personDao){  
  14.       this.personDao = personDao;  
  15.   }  
  16.     
  17.   public  void   save(){  
  18.       System.out.println("Person Service  save  method is invoked");  
  19.       personDao.save();  
  20.   }  
  21. }  

测试类相同:
[java] view plain copy
  1. package com.myapp.core.jsr330;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4.   
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7.   
  8. public class MainTest {  
  9.       public static void main(String[] args) {  
  10.           
  11.           ApplicationContext  context = new  ClassPathXmlApplicationContext("resource/annotation.xml");  
  12.          
  13.            PersonService  personService = (PersonService) context.getBean("personService");  
  14.              
  15.            personService.save();  
  16.       }  
  17.     }  

测试结果:
Person Service  save  method is invoked
Person Dao save method  is invoked
能够得到同样的结果:

summary:

JSR330注解和Spring注解对比:

JSR330中的 @Inject  代替了 Spring  注解中的@AutoWired

JSR330中的@Named代替了Spring 注解中的@Component
0 0
原创粉丝点击