Spring - 注解配置Bean

来源:互联网 发布:程序员三宝github 编辑:程序博客网 时间:2024/05/17 09:27

前面几篇均是使用xml配置bean,如果有上百个bean,这是不可想象的!!
故而,请使用注解配置bean !!!

【1】注解类别

@Component : 基本注解, 标识了一个受 Spring 管理的组件
@Repository : 标识持久层组件
@Service : 标识服务层(业务层)组件
@Controller : 标识表现层组件

Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件. 对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称

【2】context:component-scan

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

base-package 属性指定一个需要扫描的基类包,Spring 容器将会扫描这个基类包里及其子包中的所有类.
当需要扫描多个包时, 可以使用逗号分隔.

如果仅希望扫描特定的类而非基包下的所有类,可使用 resource-pattern 属性过滤特定的类 。

示例:

<context:component-scan base-package="com.web.annotation" resource-pattern="repository/*.class" use-default-filters="true"><context:include-filter> 子节点表示要包含的目标类<context:exclude-filter> 子节点表示要排除在外的目标类<context:component-scan> 下可以拥有若干个 <context:include-filter> 和 <context:exclude-filter> 子节点
      • 一个复杂的<context:component-scan/>
如下配置将只扫描repository下的包,resource-pattern="repository/*.class"默认fileter(use-default-filters="true")将扫描所有注解组件,若想使用 include-filter等,则将其改为false.    <context:component-scan base-package="com.web.annotation" resource-pattern="repository/*.class"     use-default-filters="true">        <!-- 只包含Repository注解 ,其他不行--><!--        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>-->        <!-- 不包含Repository注解 ,其他可以--><!--        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>-->        <!-- 只包含Repository接口及其实现类 ,其他不行--><!--        <context:include-filter type="assignable" expression="com.web.annotation.repository.UserRepository"/>-->        <!-- 不包含Repository接口及其实现类 ,其他可以--><!--        <context:include-filter type="assignable" expression="com.web.annotation.repository.UserRepository"/>-->    </context:component-scan>

【3】代码中引用其他bean

简单示例如下:

    @Resource(name="userRepository")    private UserRepository repository;    public void add(){        System.out.println("UserService add...");        repository.save();    }

引用其他bean有如下几种方式:

@Autowired 和 @Resource 、@Inject

【4】使用 @Autowired 自动装配 Bean

@Autowired 注解自动装配具有兼容类型的单个 Bean属性,构造器, 普通字段(即使是非 public), 一切具有参数的方法都可以应用@Authwired 注解

默认按类型匹配,若一个类型多个实现,将会自动查询注解定义的名字,先匹配。若实现类注解都没有定义名字,将会抛出异常!!

1)默认情况下, 所有使用 @Authwired 注解的属性都需要被设置。    当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false2)默认情况下, 当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作。    此时可以在 @Qualifier 注解里提供 Bean 的名称. Spring 允许对方法的入参标注 @Qualifiter 已指定注入 Bean 的名称。3@Authwired 注解也可以应用在数组类型的属性上, 此时 Spring 将会把所有匹配的 Bean 进行自动装配。4@Authwired 注解也可以应用在集合属性上, 此时 Spring 读取该集合的类型信息, 然后自动装配所有与之兼容的 Bean5@Authwired 注解用在 java.util.Map 上时, 若该 Map 的键值为 String, 那么 Spring 将自动装配与之 Map 值类型兼容的 Bean, 此时 Bean 的名称作为键值。

【5】使用 @Resource 自动装配 Bean

@Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为 Bean 的名称

我是最喜欢使用这个注解的,写上字段值@Resource(value="userService"),简单方便又省心。不用担心一个类型多个实现,而且不用额外加@Qualifier 去进行精确匹配。

【6】注解获取bean,并使用方法

如:

@Repository(value="bookShopDAO")public class BookShopDAOImpl implements BookShopDAO {}
      • 类型为实现类型
    @Resource(name="bookShopDAO")    BookShopDAOImpl bookShopDAO;

将可以使用 BookShopDAOImpl 的所有方法 !!

      • 类型为接口类型
    @Resource(name="bookShopDAO")    BookShopDAO bookShopDAO;    //此时用到了多态

将只能使用 BookShopDAO中定义 的所有方法 !!,不能使用实现类自定义的方法

0 0
原创粉丝点击