Spring 学习——Spring常用注解——@Component、@Scope、@Repository、@Service、@Controller、@Required、@Autowired、@Qualifier、@Configuration、@ImportResource、@Value

Bean管理注解实现

  • Classpath扫描与组件管理
  • 类的自动检测与注册Bean
  • 类的注解@Component、@Service等作用是将这个实例自动装配到Bean容器中管理
  • 而类似于@Autowired、@Required等注解则是将所代表的实例Bean1注册到需要这个实例的另一个Bean2中,在Bean2初始化时使其属性Bean1值不为null,他们并不能使Bean装配到Bean容器中。使用这些注解时,其代表的实例是要已经装配到Bean容器中的,否则会报错。
  • <context:component-scan >
    • 自动扫描类的注解:将使用注解的类注册到IOC容器中
      <context:component-scan base-package="com.jing.spring.annotation"></context:component-scan>
  • @Component(value="    ")
    • 是所有注解的父注解,用于不确定类是具体应该使用哪种类型注解时使用。可以代表任何一种类型注解。
  • @Scope
    • Bean的作用域
    • @Scope(value="singleton")
    • @Scope("value=prototype")
  • @Repository
    • 注解数据DAO层
  • @Service
    • 注解Service层
  • @Controller
  • @Required
    • 适用于Bean的setter方法,表示Bean初始化时,Bean的属性必须被填充。
  • @Autowired(!!!)
    • @Autowired注解只是将Bean注入到具体属性或参数中,它不具备将Bean装配到Bean容器中的功能。
    • 适用于Bean的setter方法,表示Bean初始化时,Bean的属性必须被填充。(功能同@Required)
      private BeanTest beanTest;
      
      @Autowired
      private void setBeanTest ( BeanTest beanTest){
            this.beanTest =beanTest;
      }
    • 适用于构造器
      class BeanAnnotation{
          private BeanTest beanTest;
      
          @Autowired
          public BeanTest  ( BeanTest beanTest){
              this.beanTest =beanTest;
          }
      }
    • 适用于属性
      class BeanAnnotation{
      
          @Autowired
          private BeanTest beanTest;
      
          public BeanTest  ( BeanTest beanTest){
              this.beanTest =beanTest;
          }
      }    
    • 在使用@Autowired时,如果找不到合适的Bean将会报错。我们可以使用以下方法避免这个问题,代表这个Bean不是必要的
      class BeanAnnotation{
      
          @Autowired(required=falseprivate BeanTest beanTest;
      
          public BeanTest  ( BeanTest beanTest){
              this.beanTest =beanTest;
          }
      }    
    • 当使用@Autowired(required=true)时,代表这个Bean是必须已经装配到Bean容器中,每个类只可以在一个构造器上使用。
    • 当一个类中,有多个setter需要标记为必须装配时,可以使用@Required代替@Autowired(required=true
    • 注解常用的接口,比如BeanFactory、Application、ResourceLoader、MessageSource,用于在类中直接获取实例。
      public class TestAutowired{
           
           @Autowired   
           private ApplicationContext application;   
      
           public TestAutowired{
                application.getBean("");
           }
      }
    • 注解给需要该类型的数组,使其不为null,如List、Set
      public class TestAutowired{
           
          //AutowiredBeanI为接口,实现这个接口的类都将注入到list中
           @Autowired   
           private List<AutowiredBeanI>  list;   
      }
    • 注解给需要该类型的Map,使其不为null
      public class TestAutowired{
           
          //AutowiredBeanI为接口,实现这个接口的类都将注入到map中
           @Autowired   
           private Map<String,AutowiredBeanI>  map;   
      }
    • Next
  • @Qualifier
    • @Qualifier注解是将具体Bean注入到属性或参数中,它不具备将Bean装配到Bean容器中的功能。使用这个注解的前提是这个Bean已经装配到Bean容器中,否则会报错。
    • @Qualifier可以指定具体名称的Bean,将Bean注入到属性或参数中,如下代码:TestQualifierI接口有多个实现类,如果不通过@Qualifier指定具体Bean,这几个实现类的Bean将都会注入到List中;指定具体Bean名称后,只会注入指定的Bean。
      @Component(value = "qualifierEntry")
      public class QualifierEntry {
      
          @Autowired
          @Qualifier(value = "qualifierOne")
          private List<TestQualifierI> qualifierIS;
      
          public void print(){
              for (TestQualifierI qualifier:qualifierIS) {
                  qualifier.say();
              }
          }
      }
    • 可以通过@Autowired和@Qualifier(value=" ")的方式指定Bean名字将Bean注入。但是这种方式一般不建议使用,可以使用JSR-250@Resource注解替代,这个注解是通过使用特定的名称来定义和识别特定的目标(这个匹配过程与Bean声明的类型无关)。
  • @Configuration、@Bean、@Scope
    • @Configuration标记类可以作为配置文档来使用,和配置文件产生关联
    • 在使用@Configuration标记的类中,使用@Qualifier(value="  ")时,@Qualifier不能够注入其他类的实例,只可以引用本类中使用@Bean装配的实例。
    • @Bean用于配置和初始化一个用于IOC容器管理的新实例,它可以用于标识具有特殊数据内容的实例,将这个实例存储在IOC中,方便读取。
    • @Bean标记的类默认为singleton,可以和@Scope搭配使用,标记改变类的作用域
    • @Bean只能应用于标记类的方法,这个类的作用是产生新的实例,并由@Bean标记交由IOC容器管理,和@Configuration注解一起使用。
      package com.jing.spring.annotation;
      
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;

      @Configuration public class Configure { @Bean(value = {"configure2","configure1"},initMethod = "init",destroyMethod = "destroy") public BeanConfigureI beanCon(){ return new BeanConfigureImpl(); } }
  • @ImportResource、@Value
    • @ImportResource加载本地资源文件:xml,使用@Value将properties中得变量赋值到Java属性中,配置使用
      <!--   spring-config.xml       -->
      <?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" >

      <context:property-placeholder location="classpath:/config.properties"></context:property-placeholder>
      </beans>
      #config.properties
      url:127.0.0.1
      name:jingguoliang
      package com.jing.spring.annotation;
      
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.context.annotation.ImportResource;
      
      @Configuration
      @ImportResource(locations = {"classpath:spring-config.xml"})
      public class Configure {
      
          @Value(value = "url")
          private String url;
          @Value(value = "name")
          private String name;
      
          @Bean(value = {"configResource"})
          public ValueAndReImportResource getConfigResource(){
              ValueAndReImportResource valueAndReImportResource = new ValueAndReImportResource(url,name);
      
              return valueAndReImportResource;
          }
      }
    • Next
原文地址:https://www.cnblogs.com/zuiyue_jing/p/9990961.html