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=false)
      private 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

Spring 学习——Spring常用注解——@Component、@Scope、@Repository、@Service、@Controller、@Required、@Autowired、@Qualifier、@Configuration、@ImportResource、@Value的更多相关文章

  1. Java程序员常用的@Component、@Repository、@Controller、@Service系列【案例demo3】

    Java程序员常用的@Component.@Repository.@Controller.@Service系列[案例demo3]   很多程序员通过在类上使用@Repository.@Componen ...

  2. Spring学习之常用注解(转)

    使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...

  3. spring 以及 spring mvc 中常用注解整理

    spring 以及 spring mvc 中常用注解整理 @RequestMapping(映射路径) @Autowired(注入 bean 对象) 例如: @Autowired private Bas ...

  4. SpringAnnotation注解之@Component,@Repository,@Service,@Controller

    @Component:组件,表示此写上了此注解的bean,作为一个组件存在于容器中.这样的话别的地方就可以使用@Resource这个注解来把这个组件作为一个资源来使用了.初始化bean的名字为类名首字 ...

  5. Spring 和 SpringMVC 常用注解和配置(@Autowired、@Resource、@Component、@Repository、@Service、@Controller的区别)

    Spring 常用注解 总结内容 一.Spring部分 1.声明bean的注解 2.注入bean的注解 3.java配置类相关注解 4.切面(AOP)相关注解 5.事务注解 6.@Bean的属性支持 ...

  6. Spring中的常用注解

    Spring中的常用注解 1.@Controller 标识一个该类是Spring MVC controller处理器,用来创建处理http请求的对象.

  7. springmvc学习笔记(常用注解)

    springmvc学习笔记(常用注解) 1. @Controller @Controller注解用于表示一个类的实例是页面控制器(后面都将称为控制器). 使用@Controller注解定义的控制器有如 ...

  8. SpringMVC注解@Component、@Repository、@Service、@Controller区别

    SpringMVC中四个基本注解: @Component.@Repository   @Service.@Controller 看字面含义,很容易却别出其中三个: @Controller   控制层, ...

  9. Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier、@scope

    以下内容摘自部分网友的,并加上了自己的理解 @Service用于标注业务层组件(我们通常定义的service层就用这个) @Controller用于标注控制层组件(如struts中的action.Sp ...

随机推荐

  1. Python学习笔记之装饰器原理

    def decorator(fn): def wrapper(): print("询价") fn() print("购买成功!") return wrapper ...

  2. 假如java类里的成员变量是自身的对象

    假如java类里的成员变量是自身的对象,则新建该类对象时内存中怎么分配空间,我感觉似乎死循环了. 不过我想的肯定是错的,因为很多类的成员变量是自身对象,并且绝对无错,举个例子: Class A{ pr ...

  3. DX9 DirectX键盘控制程序 代码

    // @time: 2012.3.26 // @author: jadeshu // des: DirectX键盘控制程序 #include <Windows.h> #include &l ...

  4. python3(socket 实现udp,tcp套接字编程)

    #coding=utf-8 #客户端程序TCP 连接 import socket s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connec ...

  5. Hadoop常用命令总结

    一.前述 分享一篇hadoop的常用命令的总结,将常用的Hadoop命令总结如下. 二.具体 1.启动hadoop所有进程start-all.sh等价于start-dfs.sh + start-yar ...

  6. 20165215 2017-2018-2 《Java程序设计》第7周学习总结

    20165215 2017-2018-2 <Java程序设计>第七周学习总结 教材学习内容总结 chapter11 下载安装MySQL服务器 启动MySQL数据库服务器 在bin子目录中, ...

  7. SQL 语法速记

    ----------------------------------DML(数据操作语言)---------------------------------- -- 一.INSERT VALUES语句 ...

  8. php Allocator Jemalloc TCMalloc那个内存分配器比较好?

    php Allocator Jemalloc TCMalloc那个内存分配器比较好? php一键安装脚本可以选择是否安装内存优化 You have 3 options for your Memory ...

  9. str int list tuple dict 一些实操

    #字符串的 一些实操 a='what' b=' are ' c=' you ' print(a+b+c) #字符串拼接 m =a.split('+') #以什么分割 (代码a='w+ha+t' 输出[ ...

  10. Unity3d 5.x搭载VS2013使用

    1.   安装unity vs.首先我们打开我们下载的unity vs.然后就会看见里面有3个文件,我们双击UnityVS 2013-1.8.1.msi.进行安装,在其过程狂点击下一步就可以,直到点击 ...