Spring笔记——零配置(注解)

1.搜索Bean类

Spring通过使用一些特殊的Annotation来标注Bean类,Spring提供了如下几个Annotation来标注Spring Bean:
@Component:标注一个普通的Spring Bean类
@Controller:标注一个控制器组件类
@Service:标注一个业务逻辑组件类
@Repository:标注一个DAO组件类

如果我们需要定义一个普通的Spring Bean,则直接使用@Component标注即可。但如果用@Repository、@Controller或@Service来标注这些Bean类,这些Bean类将被作为特殊的Java EE组件对待,也许能更好的被工具处理,或与切面进行关联。@Repository、@Controller和@Service也许还能携带更多的语义,因此,如果需要在Java EE应用中使用这些标注时,尽量考虑使用@Repository、@Controller和@Service来代替通用的@Component标注。
指定了某些类可作为Spring Bean类使用后,最后还需要让Spring搜索指定路径,此时需要在Spring配置文件中导入context Schema,并指定一个简单的搜索路径。

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- 自动扫描指定包及其子包下的所有Bean类 -->
    <context:component-scan base-package="main.java.service.impl" />
</beans>

在这种基于Annotation的方式下,Spring采用约定的方式来为这些Bean实例指定名称,这些Bean实例的名称默认是Bean类的首字母小写,其他部分不变。

除此之外,我们还可以通过为

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- 自动扫描指定包及其子包下的所有Bean类 -->
    <context:component-scan base-package="main.java.service.impl">
        <!-- 只包含以Axe结尾的类 -->
        <context:include-filter type="regex" expression=".*Axe" />
        <!-- 但是StoneAxe类不包含其中 -->
        <context:exclude-filter type="assignable" expression="main.java.service.impl.StoneAxe"/>
    </context:component-scan>
</beans>

2.指定Bean的作用域

当使用注解方式来管理Bean类时,可使用@Scope来标注Bean的作用域,只要在该Annotation中提供作用域的名称即可。例如,我们可以定义如下Java类:

@Scope("prototype")
@Component
public class SteelAxe implements Axe {
    ......
}

3.使用@Resource配置依赖

@Resource位于java.annotation包下,是来自Java EE规范的一个Annotation,Spring直接借鉴了该Annotation,通过使用该Annotation为目标制定协作者Bean。

@Resource有一个name属性,在默认情况下,Spring将这个值解释为需要被注入的Bean实例的名字。换句话说,使用@Resource与

package main.java.service.impl;

import javax.annotation.Resource;

import main.java.service.Axe;
import main.java.service.Person;

import org.springframework.stereotype.Component;

@Component
public class Chinese implements Person {

    private Axe axe;

    @Resource(name = "stoneAxe")
    public void setAxe(Axe axe) {
        this.axe = axe;
    }

    @Override
    public void useAxe() {
        System.out.println(this.axe.chop());
    }
}

该Annotation指定将stoneAxe注入该setAxe()方法,也就是将容器中的stoneAxe Bean作为setAxe()的参数传入。

@Resource不仅可以修饰setter方法,也可以直接修饰Field,使用@Resource时还可以省略name属性。如果使用@Resource修饰Field将会更加简单,此时连setter方法都可以不要。例如:

package main.java.service.impl;

import javax.annotation.Resource;

import main.java.service.Axe;
import main.java.service.Person;

import org.springframework.stereotype.Component;

@Component
public class Chinese implements Person {

    @Resource(name = "stoneAxe")
    private Axe axe;

    @Override
    public void useAxe() {
        System.out.println(this.axe.chop());
    }
}

☞ 当使用@Resource修饰setter方法时,如果省略name属性,则name属性默认是该setter方法去掉前面的set子串、首字母小写后得到的字符串。
☞ 当使用@Resource修饰Field时,如果省略name属性,则name属性默认与该Filed同名;如果不存在,则会查找实现其接口的Bean类

4.使用@PostConstruct和@PreDestory定制生命周期行为

@PostConstruct和@PreDestory同样位于java.annotation包下,也是来自Java EE规范的两个Annotation,Spring直接借鉴了它们,用于定制Spring容器中Bean的生命周期行为。
@PostConstruct与

5.Spring3.0新增的Annotation

Spring3.0新增了两个Annotation:@DependsOn和@Lazy,其中用于强制初始化其他Bean,而则用于指定该Bean是否取消预初始化

6.自动装配和精确装配

Spring提供了@Autowired注解来指定自动装配,使用@Autowired可以标注setter方法、普通方法、Field和构造器等

@Autowired标注setter方法时,默认采用的是byType的自动装配策略。
@Autowired标注Field时,默认采用的是byType策略,如果实例多余一个,则会抛出异常
@Autowired甚至可以用于修饰数组类型的Field,如下代码所示:

@Component
public class Chinese implements Person {

    @Autowired
    private Axe[] axes;

    @Override
    public void useAxe() {
        for (Axe axe : axes) {
            System.out.println(axe.chop());
        }
    }
}

被@Autowired修饰的axes属性是Axe[]数组,在这种情况下,Spring会自动搜索容器中所有的Axe实例,并以这些axe实例作为数组元素来创建数组。
与此类似的是,@Autowired也可以标注集合Field,或标注形参类型是集合的方法,Spring对这种集合属性、集合形参的处理与前面数组类型的处理是完全相同的,对于这种集合类型的参数而言,程序代码中必须使用泛型。

@Qualifier注解
@Autowired总是采用byteType的自动装配策略,在这种策略下,符合自动装配类型的候选Bean实例常常有多个,这个时候就可能引起异常了;为了实现精确的自动装配,Spring提供了@Qualifier注解,通过使用@Qualifier,允许根据Bean标识来指定自动装配。例如:

@Component
public class Chinese implements Person {

    @Autowired
    @Qualifier("stoneAxe")
    private Axe axe;

    @Override
    public void useAxe() {
        System.out.println(this.axe.chop());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值