Spring IoC常用注解

一、Spring IoC容器
Spring IoC(Inversion of Control,控制反转)是Spring Framework的最核心的部分。
所谓控制反转,是指通过使用IoC容器对象依赖关系的管理被反转了,也就是说,对象之间的依赖关系由IoC容器进行管理,并且由Ioc容器通过依赖注入(DI,Dependency Injection)的方式来完成对象的注入。
 
在使用Spring的开发中,我们需要将所有的类在Spring的IoC容器中进行注册,告诉Spring你是什么,你需要什么,然后IoC容器会在你需要哪个对象时为你创建这个对象以及该对象依赖的其他对象实例(这就是依赖注入)。这些类的创建、销毁都会交由IoC容器来管理,而不再由引用它的对象维护。将以前的“对象-对象”的依赖模式转变为了“对象-IoC容器-对象”的依赖模式。
 
Spring提供了两种注册IoC容器的方式:XML方式和注解的方式。
 
二、Spring IoC的XML配置
1、添加Spring基本依赖包
aopalliance-1.0.jar
commons-logging-1.1.1.jar
spring-aop-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
2、添加Spring配置文件applicationContext.xml 

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="userDao" class="com.boya.spring.ioc.UserDao" />
    <bean id="exampleBean" class="com.boya.spring.ioc.ExampleBean">
        <property name="name" value="boya" />
        <property name="userDao" ref="userDao" />
    bean>
beans>
3、分别创建User类和ExampleBean类
User类: 
public class UserDao {
    public String getName() {
        return "boya";
    }
}
ExampleBean类: 
public class ExampleBean {
    private String name;
    private UserDao userDao;
    
    public void print(){
        System.out.println("Name is :"+name);
    }
    
    public void userPrint(){
        System.out.println("User name is :"+userDao.getName());
    }
    
    //省略getter、setter方法
}
4、IoC容器测试 
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ExampleBean exampleBean = context.getBean("exampleBean", ExampleBean.class);
exampleBean.print();
exampleBean.userPrint();
    输出:
Name is :boya
User name is :boya
 
三、Spring IoC注解
Spring 的依赖配置方式与 Spring 框架的内核自身是松耦合设计的。然而,直到 Spring 3.0 以前,使用 XML 进行依赖配置几乎是唯一的选择。Spring 3.0 的出现改变了这一状况,它提供了一系列的针对依赖注入的注解,这使得 Spring IoC 在 XML 文件之外多了一种可行的选择。下面介绍如何使用这些注解进行依赖配置的管理。
 
我将注解分为两类,第一类用于属性装配,第二类用于类的注册。

属性装配使用的注解一般有两种,分别是Autowired、Resource。
@Autowired
1、@Autowired默认按照类型匹配的方式(byType)进行注入
3、@Autowired注解可以用于成员变量、setter方法、构造器函数等
4、使用@Autowired注解须有且仅有一个与之匹配的Bean,当找不到匹配的 Bean 或者存在多个匹配的Bean时,Spring 容器将抛出异常
5、Spring 允许我们通过 @Qualifier 注释指定注入 Bean 的名称。@Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了
 
@Resource
1、@Resource 的作用相当于 @Autowired,只不过 @Autowired 按 byType 自动注入,@Resource 默认按 byName 自动注入罢了
2、@Resource 有两个属性,分别是 name 和 type,Spring 将 @Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。所以如果使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。

需要将某个类在IoC容器注册时,可以使用@Component、@Repository、@Service和 @Controller 
@Component
1、@Component是所有受Spring管理组件的通用形式,而@Repository、@Service和 @Controller则是@Component的细化, 用来表示更具体的用例(分别对应了持久化层、服务层和表现层)
2、使用@Component注解定义的Bean,默认的名称(id)是小写开头的非限定类名。如UserDao类定义的Bean名称就是userDao。你也可以指定Bean的名称: @Component("abc")

下面,我们把上面的示例改为注解配置的形式。
首先,需要修改配置文件applicationContext.xml,如下: 

<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.0.xsd
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="com.boya.spring.ioc" />
beans>
的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类使用的注解都会被处理。 
2、在类中添加注解
UserDao类: 
@Repository
public class UserDao {
    public String getName() {
        return "boya";
    }
}
ExampleBean类: 
@Service
public class ExampleBean {
    @Resource
    @Value("boya")
    private String name;
    @Resource
    private UserDao userDao;
    
    public void print(){
        System.out.println("Name is :"+name);
    }
    
    public void userPrint(){
        System.out.println("User name is :"+userDao.getName());
    }
}
前面介绍了,用于装配属性的注解@Autowired和@Service是可以用于成员变量的,所以当我们在成员变量设置这两个注解后,setter方法就可以去除了。
 
3、IoC容器测试 
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ExampleBean exampleBean = context.getBean("exampleBean", ExampleBean.class);
exampleBean.print();
exampleBean.userPrint();
输出结果:
Name is :boya
User name is :boya

 

注解实现Bean配置主要用来进行如依赖注入、生命周期回调方法定义等,不能消除XML文件中的Bean元数据定义,且基于XML配置中的依赖注入的数据将覆盖基于注解配置中的依赖注入的数据
注册注解处理器

 方式一:bean

[html]  view plain copy
  1. <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  

 方式二: 命名空间

 将隐式地向Spring 容器注册AutowiredAnnotationBeanPostProcessor 、CommonAnnotationBeanPostProcessor 、 PersistenceAnnotationBeanPostProcessor 以及RequiredAnnotationBeanPostProcessor 这4 个BeanPostProcessor 。

 方式三: 命名空间

如果要使注解工作,则必须配置component-scan ,实际上不需要再配置annotation-config。

[html]  view plain copy
  1. <context:component-scan base-package="com.spring.ioc5">  
  2.         

你可能感兴趣的:(spring)