Spring4学习回顾之路09-基于注解的方式配置bean

一:基于注解配置Bean

  首先介绍下组件扫描(component scanning): Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件。

包括:

  -@Component:基本注解,标识一个受Spring管理的组件

  -@Respository:标识持久层组件

  -@Service:标识服务层/业务层组件

  -@Controller:标识表现层/控制层组件

实际上,上述四个注解,除了名字不同之外,功能都是一样的,换一句话说,能用@Respository也能用其他三个组件,只是换个名称而已!

对于扫描到的组件,Spring有默认的命名策略,“使用非限定类名,第一个字母小写”,也可以在注解中通过value属性值标识组件名称。

在组件类上使用了上述注解后,还需要在Spring的配置文件中开启组件扫描“<context:component-scan>”,比如:

<context:component-scan base-package="com.lql.srping04"></context:component-scan>

base-package:指定一个需要扫描的基类包,Spring容器将会扫描这个基类包里面以及其子包的所有类,如需要扫描多个包,可以使用逗号隔开。

案例:建立一个空的类Student.java,加上注解@Component

package com.lql.srping04;

import org.springframework.stereotype.Component;

/**
 * @author: lql
 * @date: 2019.10.28
 * Description:
 */
@Component
public class Student {
   
}

 

 测试类:(配置文件必须开始组件扫描)

package com.lql.srping04;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author: lql
 * @date: 2019.10.28
 * Description:
 */
public class StudetnTest {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("spring04.xml");
        Student student = app.getBean("student", Student.class);
        System.out.println(student);
    }
}

 

 打印结果:com.lql.srping04.Student@1563da5

说明是没有问题的!当组件扫描的包过多,只需要指定单独的包的时候,需要在“<context:component-scan>”使用resource-pattern:如:

    <context:component-scan base-package="com.lql.srping04" resource-pattern="demo/*.class"></context:component-scan>

 resource-pattern就是指定扫描的资源。当然除此之外还有子节点“<context:include-filter>”和子节点“<context:exclude-filter>”:

<context:include-filter>:表示要包含的目标类

<context:exclude-filter>:表示要排除的目标类

<context:component-scan>下可以拥有若干个<context:include-filter>和干个<context:exclude-filter>子节点。

<context:include-filter>和<context:exclude-filter>支持多种类型的过滤表达式:  -annotation:  示例:com.lql.XxxAnnotation,所有标注了XxxAnnotation的类,该类型采用目标类是否标注了某个注解进行过滤

  -assinable:   示例:com.lql.XxxService,所有继承或扩展了XxxService的类,该类型采用目标类是否继承或扩展某个特定类进行过滤

  -aspectj:    示例:com.lql..*Service+,所有类名以SErvice结束的类以及继承或扩展它们的类,该类型采用AspectJ表达式进行过滤

  -regex:    示例:com.\lql\.anno\.*,所有com.lql.anno包下的类,该类型采用正则表达式根据类的类名进行过滤

  -custom:  示例:com.lql.XxxTypeFilter,采用XxxTypeFilter通过代码的方式定义过滤规则,该类型必须实现org.springframework.core.type.TypeFilter接口

常用的则是annotationassinable;比如:(注意的是,如果使用了include-filter,必须将use-default-filters设置为false,取消默认的过滤,采用自己的过滤方式)

使用annotation

<context:component-scan base-package="com.lql.srping04" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"></context:include-filter>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"></context:include-filter>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
    </context:component-scan>

 

使用assinable

    <context:component-scan base-package="com.lql.srping04">
        <context:exclude-filter type="assignable" expression="com.lql.srping04.Student"></context:exclude-filter>
    </context:component-scan>

 效果是一样的.

二:基于注解装配Bean的属性

  <context:component-scan>元素还会自动注册AutowiredAnnotationBeanPostPRocessor实例,该实例可以自动装配具有@Autowired,@Resource,@inject注解的属性!

使用@autowired自动装配Bean:@Autowired注解自动装配具有兼容类型的单个Bean属性

  构造器,普通字段(即使是非public),一切具有参数的方法都可以应用@autowired注解!默认情况下,所有使用@Autowired注解的属性都需要被设置,当Spring找不到匹配的Bean装配属性时就会抛出异常,若某一属性允许不被设置,可以设置@Autowired注解的required的属性为false。比如:

   @Autowired(required = false)
    private Student student;

如果IOC容器里有多个相同的bean,如果使用@autowired的话会报错,("org.springframework.beans.factory.NoUniqueBeanDefinitionException");这时有两种方式

 ①:指定bean的名称与属性名一直,比如组件上@Service(value = “student”),这个“student”和属性的student对应:

 ②:使用@Qualifier注解指定要使用的那个bean;如下

   @Autowired
    @Qualifier("student")
    private Student student;

除@Autowired之外,还可以使用@Resource或者@Inject自动装配Bean,@Inject和@Autowired注解一样也是按类型匹配注入的Bean,但是没有reqired属性,@Resource注解要求提供一个Bean名称的属性,若该属性为空,则自动采用标注处的变量或者方法名作为Bean的名称。所以建议使用@autowired!   

@Autowired的工作机制

  首先使用byType的方式进行装配,如果能唯一匹配,则进行装配;如果匹配到多个兼容类型的Bean时,还会使用byName的方式进行唯一确定,如果能唯一确定则进行装配,如果不能唯一确定则装配失败。

posted @ 2019-10-28 17:28  DC红茶  阅读(277)  评论(0编辑  收藏  举报