Spring学习笔记之依赖的注解(2)

时间:2022-06-22
本文章向大家介绍Spring学习笔记之依赖的注解(2),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.0 注解,不能单独存在,是Java中的一种类型 1.1 写注解 1.2 注解反射 2.0 spring的注解 spring的 @Controller@Component@Service//更多典型化注解,但是@Controller@Service建议使用 @service(“personService”)可以代替set get 方法,@Resource(name=personDao) @Autowired//按照类型匹配 @Qualifier(“student”)两者结合相当于id匹配 相当于java的@Resource(name=”student”) 3.0 DI(依赖注入)的注解@Resource、@PostConstruct applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <bean id="person" class="come.demo.spring.di.annotation.Person">
    </bean>
    <bean id="student" class="come.demo.spring.di.annotation.Student"></bean>
    <context:annotation-config></context:annotation-config>
</beans>

Person.java

public class Person {
    /**
     * @Autowired
       @Qualifier("student")
       ==
       @Resource(name="student")
     */
    @Resource(name="student")
    private Student student;

    @PostConstruct  //在构造器之后
    public void init(){
        System.out.println("init");
    }

    @PreDestroy //在spring容器销毁之前
    public void destroy(){

    }

    public void say(){
        this.student.say();
    }
}

Student.java public class Student { public void say(){ System.out.println(“student”); } }

PersonTest.java

public class PersonTest {
    /**
     * 1、启动spring容器
     * 2、把spring配置文件中的bean实例化(person,student)
     * 3、当spring容器解析配置文件
     *     <context:annotation-config></context:annotation-config>
     *    spring容器会在纳入spring管理的bean的范围内查找哪些类的属性上是否加有@Resource注解
     * 4、如果在属性上找到@Resource注解
     *      如果@Resource的注解的name属性的值为""
     *           则把@Resource所在的属性的名称和spring容器中的id作匹配
     *                  如果匹配成功,则赋值
     *                  如果匹配不成功,则会按照类型进行匹配
     *                      如果匹配成功,则赋值,匹配不成功,报错
     *      如果@Resource的注解的name属性的值不为""
     *           则解析@Resource注解name属性的值,把值和spring容器中的ID进行匹配
     *               如果匹配成功,则赋值
     *               如果匹配不成功,则报错
     *               
     * 说明:
     *     注解代码越来越简单,效率越来越低
     *     注解只能应用于引用类型
     */
    @Test
    public void testDIAnnotation(){
        ApplicationContext context = 
                new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person)context.getBean("person");
        person.say();
    }
}
4.0 scan-类扫描的注解(减少配置代码,却降低了性能)
    4.1 配置文件
    <context:component-scan
        base-package="com.demo.spring.scan.annotation"></context:component-scan
    4.2 原理:
    0.0 把一个类放到spring容器中,该类也称为component
    1.0 启动spring容器
    2.0 当spring容器解析配置文件时,会去base-package指定的包及子包中扫描所有的类。
    3.0 把类放到spring容器中
        看哪些类上面是否加有@Component注解
        如果该类上面有@Component注解
            检查该类的value属性是否为““
                如果为”“,则会把该类注解所在的类的类名以这样的方式
                @Component
                public class Person{
                }
                ==
                <bean id = "person" class="..person"
        如果该类的value不为空,则以这样的形式:
        @Component("aa")
        public class Person{}
        ==
        <bean id="aa" class="..Person"
    4.0 扫描spring容器中所有的bean,进行@Resource规制

5.0 小结

    1、IOC把一个类放到容器里边,spring容器给他创建对象
    1.1 spring创建对象的三种方式
        1 默认构造函数 2.静态工程3.实例工程
        默认情况下把一个类放到spring容器里面,默认是单例。
        什么时候创建单例对象?
            默认情况下spring容器启动时候创建单例对象。
            但是把lazy-init="true"的话,是在contextgetbean时候才创建对象。
            如果scope="property"即多例,在contextgetbean时候才创建对象。
        初始化(init,只需要在配置文件中init-method,spring容器在调用完构造器立即自动调用)
        销毁(destroy,在spring容器销毁时候才内部调用)

    2、DI给属性赋值
        2.1赋值方法
        利用set方法可以给属性赋值还可以利用构造器,也可以用注解。进行注入。
        基本属性、引用类型、集合进行装配。
        @Resource用于给一个属性进行注入。
        类扫描<context:component-scan减少配置文件代码。

6.0 spring注解的继承 xml的继承需要,parent:spring容器中的继承 而注解的继承配置文件不需要parent。