spring-零配置支持

1、搜索bean类,使用注解标注spring bean。

     @Component:标注一个普通的spring bean类

     @Controller:标注一个控制器组件类(Java EE组件)

     @Service:标注一个业务逻辑组件类(Java EE组件)

     @Repository:标注一个DAO组件类(Java EE组件)

     普通bean在使用@Component注解后,还需要在配置文件中配置这些bean的搜索路径。

     引入命名空间:xmlns:context="http://www.springframework.org/schema/context"

     引入元素:

            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd

      指定搜索bean注解路径:

<context:component-scan 
        base-package="com.lfy.bean"/>

       举个例子:

<?xml version="1.0" encoding="GBK"?>
<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-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    <!-- 自动扫描指定包及其子包下的所有Bean类 -->
    <context:component-scan base-package="com.lfy.bean"/>
</beans>

       基于注解模式下,bean实例的名称默认是bean类的首字母小写,其他都不变。当然spring也支持在添加注解的时候指定bean实例的名称,如@Component("chinese")。

       除此之外,还可以通过<component-scan.../>元素添加<include-filter.../>或<exclude-filter.../>子元素来指定spring bean类,只要指定路径下的java类满足指定的规则,无论其是否使用了注解,spring一样会将其当做bean类来处理。使用<include-filter.../>、<exclude-filter>元素时要求指定一下两个属性:

           1》type:指定过滤器类型。spring内建的过滤器类型有:

                     annotation:Annotation过滤器,该过滤器需要指定一个Annotation名,如com.lfy.bean.MyAnnotation。

                     assignable:类名过滤器,该过滤器直接指定一个Java类。

                     regex:正则表达式过滤器,该过滤器指定一个正则表达式,匹配该正则表达式的Java类都将满足该规则。

                     aspectj:AspectJ过滤器。

           2》expression:指定过滤器所需要的表达式。

2、指定bean的作用域

     默认是single通。可以使用使用@Scope,也可以在配置文件中指定scope-resolver属性,则自定义的作用域解析器需要自定义,自定义的解析器需要实现ScopeMetadataResolver接口,并提供自定义的作用域解析策略。

     举个例子:

<beans ...>
        <context:component-scan base-package="com.lfy.bean" scope-resolver="="com.lfy.util.MyScopeResolver"/>
    </beans>

3、使用@Resource配置依赖

    @Resource有一个name属性,该属性相当于XML Schema模式中ref属性标签的作用。

//此时直接使用的是Java EE规范的Field属性字段注入
@Resource(name="stoneAxe")
private Axe axe;

    或

//此时将作为参数传入setter方法
@Resource(name="stoneAxe")
public void setAxe(Axe axe){
   this.axe=axe;
}

    或者name属性省略

4、使用@PostConstruct和@PreDestroy定制生命周期行为

     前面我们使用的生命周期相关的有:

          init-method:指定bean的初始化方法--spring容器会在bean的依赖关系注入完成后回调该方法。

          destroy-method:指定bean销毁之前的方法--spring容器会在bean销毁之前回调该方法。

      注解方面与之对应的有

          @PostConstruct:对应init-method

          @PreDestroy:对应destroy-method

5、@DependsOn强制初始化其他bean

     可以修饰bean类或方法。指定一个字符串数组作为参数。

@dependsOn({"steelAxe","abc"})
@Component
public class Chinese{
   ...
}

6、@Lazy是否取消预初始化

     使用该注解可以指定一个boolean型的value属性,该属性决定是否预初始化bean

@dependsOn({"steelAxe","abc"})
@Lazy(true)
@Component
public class Chinese{
   ...
}

7、@Autowired自动装配,@Qualifier精确装配

猜你喜欢

转载自www.cnblogs.com/ZeroMZ/p/11333210.html
今日推荐