初识注解

来源:互联网 发布:网页小游戏源码 编辑:程序博客网 时间:2024/06/16 16:38

  一、手动装配      

在spring中我们知道注入依赖对象,可以使用spring配置文件中的property标签通过get set方法以及构造器来注入依赖对象,这样的方式被称为手工注入。通常一个系统包括很多bean,然后一个bean中有很多依赖对象,那么这样的配置文件就显得很臃肿。所以提供了一个手动装配依赖对象的方式-----注解。

        下面通过例子来介绍两个简单的注解,@Resource 和 @Autowired。这两个注解的作用都是把依赖对象自动注入。      @Autowired注解是通过类型来装配。@Resource默认是按名称来装配,如果根据名称找不到,然后

就会通过类型去装配。

1)spring配置文件

<?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                                   //注解需要的schema文件
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
          
          <context:annotation-config/>             //打开注解功能
          
          <bean id="personDaoxxxx" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
          <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
          <!-- 
          <constructor-arg index="0" type="cn.itcast.dao.PersonDao" ref="personDao"/>
          <constructor-arg index="1" value="111"/>
          -->
          </bean>
</beans>

红色的命名空间是需要引入的schema文件,<context:annotation-config/>   需要context的schema文件, 这段话是开启注解的标志。

2)

import javax.annotation.Resource;    //该注解是jdk定义的,跟spring无耦合


import cn.itcast.dao.PersonDao;
import cn.itcast.service.PersonService;


public class PersonServiceBean implements PersonService {
@Resource private PersonDao personDao;                    //先通过personDao来装配,如果找不到personDao,就通过PersonDao类型来装配(找到匹配的类型                                                                                                                                 cn.itcast.dao.impl.PersonDaoBean),@Resource也可以写成 @Resource(name="personDaoxxxx"),明确要                                                                                                               注入的bean的名称
private String name;
public PersonServiceBean(){}

public PersonServiceBean(PersonDao personDao, String name) {
this.personDao = personDao;
this.name = name;
}


public void save(){
//System.out.println(name);
personDao.add();
}
}


 3)      另外,还可以通过属性的set方法添加 @Resource注解来装配依赖对象。

@Resource

public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}


    注意:注解本身干不了任何事情,它和xml一样都只起到配置的作用。每一个注解都有一个注解处理器,用来处理该注解。如AutowiredAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor。


二、自动装配

<?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">
          
          <context:annotation-config/>
         
          <bean id="personDaoxxxx" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
          <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"autowire="byType">
          </bean>
</beans>

        如byType、byName 等就是自动装配,但是自动装配会出现很多不确定情况,不建议使用

0 0