Spring通过注解的方式配置Bean

组件扫描:Spring能够从classpath下自动扫描、侦测和实例化具有特定注解的组件

特定组件包括:

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

—@Respository:标识持久层组件

—@Service:标识业务层(服务层)组件

—@Controller:标识表现层组件

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


当在组件上标注了特定的注解之后,还需要在Spring的配置文件中声明:<context:component-scan>,、

1、base-package属性指定一个需要扫描的基础类包,Spring容器会扫描这个基础类包及其子包中的所有类,、

2、当需要扫描多个包时,可以使用逗号分隔

3、当需要扫描某个特定的类,而非所有,可以使用resource-pattern过滤特定的类(自己需要的类,就是过滤进来)

4、context:include-filter子节点标识包含的目标类,context:exclude-filter子节点标识排除哪些类<context:component-scan>下可以包含若干个这样的子节点

controller:

package annotation.controller;

import org.springframework.stereotype.Controller;

/**
 * @author chenpeng
 * @date 2018/6/2 15:21
 */
@Controller
public class UserController {
    public void execute(){
        System.out.println("UserController execute....");
    }
}

service:

package annotation.service;

import org.springframework.stereotype.Service;

/**
 * @author chenpeng
 * @date 2018/6/2 15:20
 */
@Service
public class UserService {
    public void add(){
        System.out.println("UserService add.....");
    }
}

respository:

package annotation.respository;

import org.springframework.stereotype.Repository;

/**
 * @author chenpeng
 * @date 2018/6/2 15:19
 */
@Repository
public class UserImpl implements User {

    public void save() {
        System.out.println("save......");
    }
}

配置文件:

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

    <!--
    指定SpringIOC容器扫描的包
    可以通过resource-pattern扫描指定的类
    -->
    <!--<context:component-scan base-package="annotation"
    resource-pattern=""/>-->


    <!--
    context:include-filter:子节点指定包含哪些表达式的组件,需要配合use-default-filters="false"使用
    context:exclude-filter:子节点指定排除哪些表达式的组件,不需要配合use-default-filters使用
    -->
    <context:component-scan base-package="annotation" use-default-filters="false">
        <!--注解的方式-->
        <!--<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>


        <!--接口的方式-->
        <context:exclude-filter type="assignable" expression="annotation.service.UserService"/>
    </context:component-scan>
</beans>


各个层之间的关联关系

实际上<context:component-scan>元素会自动注册AutowiredAnnotationBeanPostProcessor实例,改实例可以自动装配

@Autowired和@Resource、@Inject注解的属性


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

—构造器、普通字段,一切只有参数的方法都可以应用@Autowired注解

—默认情况下,所有使用@Autowired注解的属性都需要被设置,当Spring找不到匹配的Bean装配属性时,会抛出异常。

    若某一属性允许不被设置,可以为@Autowired注解的required属性设置为false


当有有多个类型相同的Bean被装配的时候,若要不冲突:

1、在类的注解中加入特定的名字

2、注入的时候可以@Autowired和@Qualifier一起使用指定要注入的bean


除了@Autowired之外,还可以使用@Resource或@Inject来自动装配,他们的功能和@Autowired相似,建议使用@Autowired


原文地址:https://www.cnblogs.com/huangzhe1515023110/p/9276058.html