zoukankan      html  css  js  c++  java
  • Spring--通过注解来配置bean

    Spring通过注解配置bean
      基于注解配置bean
      基于注解来配置bean的属性

    在classpath中扫描组件
      组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件。
      特定的组件包括:
        -@Component:基本注解,标识了一个受Spring管理的组件
        -@Responsitory:标识持久层组件
        -@Service:标识服务层(业务层)组件
        -@Controller:标识表现层组件
      对于扫描到的组件,Spring有默认的命名策略:使用非限定类名,第一个字母小写。也可以在注解中通过value属性值标识组件的名称。
      当在组件类上使用了特定的注解之后,还需要在Spring的配置文件中声明<context:component-scan>:
        base-package属性指定一个需要扫描的基类包,Spring容器将会扫描这个基类包里及其子包中的所有类
        当需要扫描多个包时,可以使用逗号分隔
        如果仅希望扫描特定的类而非基包下的所有类,可使用resource-pattern属性过滤特定的类,示例:
        <context:component-sacn base-package="com.yl.spring.beans" resource-pattern="autowire/*.class"/>
        <context:include-filter>子节点表示要包含的目标类
        <context:exclude-filter>子节点表示要排除在外的目标类
        <context:component-sacn>下可以拥有若干个<context:include-filter>和<context:exclude-filter>子节点


    <context:include-filter>和<context:exclude-filter>子节点支持多种类型的过滤表达式:

    类别 示例 说明
    annotation  com.yl.XxxAnnotation 所有标注了XxxAnnotation的类,该类型采用目标类是否标注了某个注解进行过滤
    assinable  com.yl.XxxService 所有继承或扩展XxxService的类,该类型采用了目标类是否继承或扩展某个特定类进行过滤
    aspectj  com.yl.*Service 所有类名义Service结束的类及继承或扩展它们的类,该类型采用AspectJ表达式进行过滤
    regex  com.yl.anno.* 所有com.yl.anno包下的类。该类型采用正则表达式,根据类的类名进行过滤
    custom  com.yl.XxxTypeFilter  采用XxxTypeFilter通过代码的方式定义过滤原则。该类必须实现org.springframework.core.type.TypeFilter接口


    组件装配
    <context:component-scan>元素还会自动注册AutowiredAnnotationBeanPostProcessor实例,该实例可以自动装配具有@Autowired和@Resource、和@Inject注解的属性

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

    TestObject.java

    1 package com.yl.annotation;
    2 
    3 import org.springframework.stereotype.Component;
    4 
    5 @Component
    6 public class TestObject {
    7     
    8 }

    UserRepository.java接口

    1 package com.yl.annotation.repository;
    2 
    3 public interface UserRepository {
    4     public void save();
    5     
    6 }

    UserRepositoryImpl.java

     1 package com.yl.annotation.repository;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.stereotype.Repository;
     5 
     6 import com.yl.annotation.TestObject;
     7 
     8 @Repository
     9 //@Repository("userRepository")
    10 public class UserRepositoryImpl implements UserRepository {
    11     
    12     @Autowired(required=false)
    13     private TestObject testObject;
    14     
    15     @Override
    16     public void save() {
    17         System.out.println("UserRepository save...");
    18         System.out.println(testObject);
    19     }
    20 
    21 }

    UserJdbcRepository.java

     1 package com.yl.annotation.repository;
     2 
     3 import org.springframework.stereotype.Repository;
     4 
     5 @Repository
     6 public class UserJdbcRepository implements UserRepository {
     7 
     8     @Override
     9     public void save() {
    10         System.out.println("UserJdbcRepository save...");
    11     }
    12 
    13 }

    UserService.java

     1 package com.yl.annotation.service;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.beans.factory.annotation.Qualifier;
     5 import org.springframework.stereotype.Service;
     6 
     7 import com.yl.annotation.repository.UserRepository;
     8 
     9 @Service
    10 public class UserService {
    11     @Autowired
    12     @Qualifier("userJdbcRepository")
    13     private UserRepository userRepository;
    14     
    15     /*@Autowired
    16     @Qualifier("userJdbcRepository")
    17     public void setUserRepository(UserRepository userRepository) {
    18         this.userRepository = userRepository;
    19     }*/
    20     
    21     public void add() {
    22         System.out.println("UserService add...");
    23         userRepository.save();
    24     }
    25 }

    UserController.java

     1 package com.yl.annotation.controller;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.stereotype.Controller;
     5 
     6 import com.yl.annotation.service.UserService;
     7 
     8 @Controller
     9 public class UserController {
    10     @Autowired
    11     private UserService userService;
    12     
    13     public void execute() {
    14         System.out.println("UserController execute...");
    15         userService.add();
    16     }
    17 }

    beans-annotation.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     6         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
     7 
     8     <!-- 指定Spring IOC容器扫描的包 -->
     9     <!-- 可以通过resource-pattern指定扫描的资源 -->
    10     <!-- <context:component-scan 
    11         base-package="com.yl.annotation"
    12         resource-pattern="repository/*.class"></context:component-scan> -->
    13     
    14     <!-- context:exclude-filter 子节点指定排除哪些指定表达式的组件 -->    
    15     <!-- context:include-filter 子节点指定包含哪些指定表达式的组件, 该子节点需要use-default-filters配合使用 -->    
    16     <context:component-scan 
    17         base-package="com.yl.annotation" >
    18         <!-- use-default-filters="false"> -->
    19         <!-- <context:exclude-filter type="annotation" 
    20             expression="org.springframework.stereotype.Repository"/> -->
    21         
    22         <!-- <context:include-filter type="annotation" 
    23             expression="org.springframework.stereotype.Repository"/> -->
    24             
    25         <!-- <context:exclude-filter type="assignable" 
    26             expression="com.yl.annotation.repository.UserRepository"/> -->
    27         
    28         <!-- <context:include-filter type="annotation" 
    29             expression="com.yl.annotation.repository.UserRepository"/> -->
    30     </context:component-scan>
    31 </beans>

    使用@Resource或@Inject自动装配bean
      Spring还支持@Resource和@Inject注解,这两个注解和@Autowired注解的功用类似
      @Resource注解要求提供一个bean名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为bean的名称
      @Inject和@Autowired注解一样也是按类型注入的bean,但是没有required属性
      建议使用@Autowired注解

  • 相关阅读:
    记一次centos 服务器中毒事件
    heatmap
    this
    购物车
    带标签的无缝轮播
    无缝轮播
    图片滚动百分百宽度
    选择排序
    冒泡排序
    tab标签页
  • 原文地址:https://www.cnblogs.com/dreamfree/p/4090746.html
Copyright © 2011-2022 走看看