JSR330注解和Spring注解对比:
JSR330中的 @Inject 代替了 Spring 注解中的@AutoWired
JSR330中的@Named代替了Spring 注解中的@Component
spring 3.0 支持JSR330标准; JSR330标准和spring annotation都是通过自动扫描的方式集成各个组件,用JSR330需要下列maven配置:
- <dependency>
- <groupId>javax.inject</groupId>
- <artifactId>javax.inject</artifactId>
- <version>1</version>
- </dependency>
一:首先是利用Spring annotation 构建项目
1:首先配置容器组件自动扫描在xml文件中,确定扫描的类
- <?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.annotation"/>
- </beans>
2:定义相关的类
dao层
- package com.myapp.core.annotation;
- import org.springframework.stereotype.Repository;
- @Repository
- public class PersonDao {
- public void save(){
- System.out.println("Person Dao save method is invoked");
- }
- }
- <span style="font-size:10px;">package com.myapp.core.annotation;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- @Service
- public class PersonService {
- @Autowired
- private PersonDao personDao;
- public void setPersonDao(PersonDao personDao){
- this.personDao = personDao;
- }
- public void save(){
- System.out.println("Person Service save method is invoked");
- personDao.save();
- }
- }
- </span>
以上类中用到的@Repository 用于生命是dao层,@Service声明是service层, 组件扫描就是扫描这些类,如果没有了这些声明,这些类讲不能得到扫描,就无法注入。所以声明是必须的。声明只是为了扫描这个类用,如果为了验证这个道理,可以把PersonDao中的 @Repository 改为 @Service程序依然正常运行,如果去掉就无法注入,会报错。
测试类:
- <span style="font-size:10px;">package com.myapp.core.annotation;
- 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.save();
- }
- }
- </span>
测试结果:
Person Service save method is invoked
Person Dao save method is invoked
Person Dao save method is invoked
以上是通过 Spring annotation进行注入;
二:利用 JSR330 进行注入:
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-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"/>
- </beans>
上面改动只是改变了Spring container 的 扫描的包
2:配置相应的类:
- package com.myapp.core.jsr330;
- import javax.inject.Named;
- @Named
- public class PersonDao {
- public void save(){
- System.out.println("Person Dao save method is invoked");
- }
- }
- package com.myapp.core.jsr330;
- import javax.inject.Inject;
- import javax.inject.Named;
- @Named
- public class PersonService {
- @Inject
- private PersonDao personDao;
- public void setPersonDao(PersonDao personDao){
- this.personDao = personDao;
- }
- public void save(){
- System.out.println("Person Service save method is invoked");
- personDao.save();
- }
- }
- package com.myapp.core.jsr330;
- 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.save();
- }
- }
测试结果:
Person Service save method is invoked
Person Dao save method is invoked
Person Dao save method is invoked
能够得到同样的结果:
summary:
JSR330注解和Spring注解对比:
JSR330中的 @Inject 代替了 Spring 注解中的@AutoWired