Spring的“零配置”支持,用annotation替代xml

曾经java和xml是密不可分的,跨平台的java和跨平台的xml配合的天衣无缝,然而spring中零配置的出现,使用annotation替代xml的配置方式也逐渐开始使用,人们抛弃了xml的配置方式。

首要问题是spring需要自动搜索java下的类,并将这些类注册成bean实例。此处使用annotation进行标注。

1. 基本Bean类搜索

项目价格
@component标注普通的spring bean类
@controller标注一个控制器组件类
@service标注一个业务逻辑组件类
@repository标注一个dao组件类

注:@component需要定义一个普通改的bean类,而其他三个标注的类作为特殊的java EE组件,可与工具进行更好的接触,或者与切面进行关联。这三个在以后的发展中可能会赋予更多的语义,因此在标注时尽量根据类的功能标注这三个标签。

采用annotation方法来进行对象注入,需要在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.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!-- 自动扫描所有及其包下所有的Bean类 -->
    <context:component-scan base-package="testSpring"/>
</beans>

可以看出xml中配置中使用了component-scan…进行配置,此外include-filter,exclude-filter等过滤器也可以进行过滤扫描等功能。

@Component   //方法1:该接口放在了testSpring下,作为加载类型进行标记
public interface Axe {
    public String chop();
}

@Component("axe") //方法2:也可以采用该方法进行标记。

实例名称默认为类名的第一个字母变成小写,其他不变。也可以采用上面方法进行标记定义实例名。

public class BeanTest {

    public static void main(String[] args){
    ApplicationContext ctx= new                 ClassPathXmlApplicationContext("beans.xml");
    System.out.println("-----"+java.util.Arrays.toString(ctx.getBeanDefinitionNames()));
    }
}

2.设置Bean的作用域

可以通过scope来制定Bean实例的作用域,没有指定scope属性的作用域默认是singleton

如下例:

@Component
@Scope("prototype")
public class Chinese {
}

也可以采用通过xml文件中的方式进行配置。

<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
...
    <context:component-scan base-package="testSpring"
     scope-resolver="org.crazyit.app.util.MyscopeResolver"/>
</beans>

3.使用@Resource配置依赖
@resource位于java.annotation包下,是来自javaEE规范的一个Annotation。改annotation中有name属性,来指定加载的实例。它不仅可以修饰setter方法,也可以直接修饰field,也可以省略name属性。
若省略name属性,当修饰field时,根据类型匹配,当修饰setter时,则默认去掉setter前面的set后,将第一个字母小写。即setAxe,实例名为axe。

@Component
@Scope("prototype")
public class Chinese {

    @Resource(name="steelAxe")//修饰field
    private Axe axe;

    //@Resource(name="steelAxe") 修饰set方法
    public void setAxe(Axe axe) {
        this.axe = axe;
    }
3.使用@postConstruct和@PreDestroy定制生命周期行为
init-method 指定初始化方法,发生在依赖注入完成后
destroy-method 指定销毁之前的方法,发生在容器销毁Bean之前

spring正常在xml中有配置生命周期前后的功能行为。Annotation也配置了@postContruct和@PreDestroy使用。

例:

@Component
@Scope("prototype")
public class Chinese {

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

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

    @PostConstruct  //发生在依赖注入之后
    public void init(){

    }

    @PreDestroy  //发生在bean销毁之前
    public void destroy(){

    }
}

4.Spring3.0新增的Annotation
@DependOn :加载该对象之前先加载哪些类
如:@DependOn({“steel”,”abc”})
@Lazy: 使bean延迟加载
如:@Lazy(true)

5.自动装配和精确装配

@Autowired,他可以标注setter方法、普通方法、Field和构造器。

    @Autowired
    public void setAxe(Axe axe) {
        this.axe = axe;
    }

@Autowired默认是byType方式装配,若修饰setter时,采用去挑set后第一个字母小写的方式作为实例名。此外,@Autowired也可以修饰多个参数的普通方法。以及构造器和Field。还可以修饰对象数组和泛型等等。

@Qualifier通常可用于修饰Field。他目标精确的指定装配的实例。此外,他还可以修饰形参。如下例子所示:

public class Chinese {

    @Qualifier("steelAxe")
    private Axe axe;

    public void setAxe(@Qualifier("steelAxe") Axe axe) {
        this.axe = axe;
    }
}

以上就是Annotation的基本使用,要熟练掌握,还需要更加了解spring的相关知识。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值