spring类扫描注入-----类扫描的注解解析器

通过类扫描注入到容器中,这种方式,在实际开发中还是很常用的,可以看下自己的配置文件,就会发现,自己公司的项目,搞不好就是这么注入的。
起码,我发现我公司的项目就是这么干的。
下面来演示一下简单的例子:
此例子和上一篇的差别很微弱,相比较而言,就是在xml配置文件里面的配置又变得少了。
关于要注入到容器的bean,不用自己一个个的去写,省去了很多的重复的步骤。简化了操作。
当然我说这么多,你不看看我前面的几篇文章,不亲自实现一下,是不太明朗的。当然你要是了解这个的话,我就显得关公门前耍大刀啦。
附上,上一篇的链接,如下:

配置文件相对简单了一点的spiring注解使用的简单例子

然后再上这次的测试代码:

--------------------- 本文来自 李学凯 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qq_27093465/article/details/52710925?utm_source=copy 

 

 

  •  
    import org.springframework.context.ApplicationContext;
  •  
    import org.springframework.context.support.FileSystemXmlApplicationContext;
  •  
    import org.springframework.stereotype.Component;
  •  
     
  •  
    import javax.annotation.Resource;
  •  
     
  •  
    /**
  •  
    * @Component 等价于 <bean id="student" class="..Student">
  •  
    */
  •  
    @Component("sb")
  •  
    class Student {
  •  
    void say() {
  •  
    System.out.println("student");
  •  
    }
  •  
    }
  •  
     
  •  
    /**
  •  
    * @Component 等价于 <bean id="person" class="..Person">
  •  
    * @Component("p") 等价于 <bean id="p" class="..Person">
  •  
    */
  •  
    @Component("p")
  •  
    class Person {
  •  
    //Student类的@Component("sb")注解带有value值sb,所以bean的ID就相当于是sb
  •  
    //所以下面的@Resource(name = "sb")或者@Resource都是可以正确执行的。
  •  
    @Resource(name = "sb")
  •  
    private Student student;
  •  
     
  •  
    void say() {
  •  
    this.student.say();
  •  
    }
  •  
    }
  •  
     
  •  
    /**
  •  
    * Created by lxk on 2016/9/30
  •  
    */
  •  
    class AtInterfaceTest {
  •  
    public static void main(String[] args) {
  •  
    //ApplicationContext ctx = new ClassPathXmlApplicationContext("file:E:/fusion/intellij_work/TrunkNew/sss.xml");
  •  
    //ApplicationContext ctx = new FileSystemXmlApplicationContext("src/sss.xml");//这个时候sss.xml是在项目的根目录下的src文件夹下
  •  
    ApplicationContext ctx = new FileSystemXmlApplicationContext("sss.xml");//这个时候sss.xml是在项目的根目录下
  •  
    Person p = (Person) ctx.getBean("p");//Person类的@Component("p")带有value值,所以bean的ID就相当于改啦
  •  
    p.say();
  •  
    }
  •  
    }

然后是对应的配置文件,如下:

 

  • <?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-2.5.xsd">
  •  
     
  •  
    <context:component-scan base-package="com.xxx.x.model.s"/>
  •  
    </beans>

也能达到,测试的效果。

具体有如下总结:

 

  1.  
    原理:
  2.  
    * 类扫描的注解解析器包含了---依赖注入---的注解解析器
  3.  
    * 原理:
  4.  
    当启动spring容器的时候,
  5.  
    ApplicationContext context = new FileSystemXmlApplicationContext("sss.xml");
  6.  
    spring容器会加载配置文件,并且解析配置文件,就会解析到
  7.  
    1* 类扫描的注解解析器,会在base-package包及子包中扫描所有的类(包括内部类,因为的测试是把所有的class放在一个class里面搞的测试)
  8.  
    * 检查类上是否有@Compontent注解
  9.  
    * 如果有
  10.  
    * @Compontent是否有value属性
  11.  
    * 没有value属性
  12.  
    则会把这个注解所在的类的类名的第一个字母变成小写,其余的不变当做bean的id
  13.  
    * 如果有value属性
  14.  
    value属性的值就是bean的id
  15.  
    * 如果没有
  16.  
    do nothing
  17.  
     
  18.  
    2* 类扫描注解解析完以后,所有的在base-package包及子包下的带有@Compontent注解的类就被纳入spring管理了
  19.  
     
  20.  
    3* 在纳入spring管理的类中扫描各个属性,看属性是否有@Resource,再根据这个注解的规则进行操作。具体参考上一篇文章,在最上方有链接
  21.  
     
  22.  
    4* 扫描的次数:
  23.  
    * 根据base-package包及子包进行扫描
  24.  
    * 扫描纳入spring管理的所有的bean的属性
  25.  

--------------------- 本文来自 李学凯 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qq_27093465/article/details/52710925?utm_source=copy 

posted on 2018-09-27 10:54  四海骄阳  阅读(2857)  评论(1编辑  收藏  举报

导航