Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2794875
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: 系统运维

2012-03-11 13:28:35

1、在java代码中使用@Autowired或@Resource注解方式进行装配。但我们需要在xml配置文件中配置以下信息:
       xmlns:xsi=""
       xmlns:context=""
       xsi:schemaLocation="
           /spring-beans-2.5.xsd
           
           /spring-context-2.5.xsd">
         
这个配置隐式注册了多个对注释进行解析处理的处理器:AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor 注: @Resource注解在spring安装目录的lib\j2ee\common-annotations.jar

2、在java代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:@Autowired 默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
    @Autowired 
    private PersonDao  personDao;//用于字段上
    @Autowired
    public void setOrderDao(OrderDao orderDao) {//用于属性的setter方法上
        this.orderDao = orderDao;
    }
@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:
    @Autowired  @Qualifier("personDaoBean")
    private PersonDao  personDao;

@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上,但它默认按名称装配。名称可以通过@Resource的name属性指定,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。
    @Resource(name=“personDaoBean”)在配置文件上id="personDaoBean"
    private PersonDao  personDao;//用于字段上

注意:如果没有指定name属性,并且按照默认的名称仍然找不到依赖对象时, @Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。

推荐用@Resource 它是属于javax.anonotion.Resource,在jdk6已经存在,是j2ee里不依赖于spring
用注解连set方法都不用写
 

  1. package cn.itcast.service.impl;

  2. import javax.annotation.Resource;
  3. import cn.itcast.dao.PersonDao;
  4. import cn.itcast.service.PersonService;

  5. public class PersonServiceBean implements PersonService {
  6.     @Resource(name="personDaoxxxx")
  7.     private PersonDao personDao;
  8.     private String name;    
  9.     public PersonServiceBean(){}

  10.     public void save(){
  11.         //System.out.println(name);
  12.         personDao.add();
  13.     }
  14. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns=""
  3.        xmlns:xsi=""
  4.        xmlns:context=""
  5.        xsi:schemaLocation="
  6.            /spring-beans-2.5.xsd
  7.             /spring-context-2.5.xsd">
  8.           
  9.           <context:annotation-config/>
  10.           
  11.           <bean id="personDaoxxxx" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
  12.          <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/>
  13. </beans>

  1. public class SpringTest {

  2.     @BeforeClass
  3.     public static void setUpBeforeClass() throws Exception {
  4.     }

  5.     @Test public void instanceSpring(){
  6.         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  7.         PersonService personService = (PersonService)ctx.getBean("personService");
  8.         personService.save();
  9.         //ctx.close();
  10.         
  11.     }
  12. }

阅读(550) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~