Spring DI 注解

本文深入探讨了如何在JavaBean中利用注解实现与Spring框架的集成,详细介绍了创建JavaBean、配置Spring容器、使用注解解析器、在类中通过注解注入bean等关键步骤。此外,文章还特别强调了注解的使用规则,包括基本类型与引用类型的区分、@Resource注解的具体应用与分析整个过程,旨在帮助开发者掌握这一高效且灵活的开发方式。

1、建JavaBean

    比如 Person.java  和 Student.java


2、在 applicationContext-di-annotation.xml 中,导入“注解解析器”

		<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">
			<!-- 
				注解解析器
			 -->
			<context:annotation-config></context:annotation-config>
			<bean  id="person_annotation"  class=""></bean>
			<bean  id="student_annotation"  class=""></bean>

		</beans>

    3、在 Person.java 中

        public  class  Person{
            private  Long  pid;
            private  String pname;
            
            // 用注解生成 student 的 bean
            @Resource(name="student_annotation")
            private  Strudent  student;

            private  Set<>  sets;

            private  List<>  lists;

            private  Map<>   map;

            private  Properties  properties;
            
            // 此处不要再封装,用注解赋值

            public  void  showStudent(){
                this.student.show();
            }
        }

              结果:创建 student 对象,执行show() 方法

4、建测试 AnnotationTest.ava

		@Test
		public  void  testPerson(){
			Person  person = (Person)context.getBean("person_annotation");
			person.showStudent();
		}

    5、“基本类型”不能使用注解,只有“引用类型”可以


    6、 @Resource 注解的使用规则:
        6.1、在spring的配置文件中导入命名空间
            xmlns:context="http://www.springframework.org/schema/context"
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd"

       6.2、导入注解解析器
            <context:annotation-config></context:annotation-config>

        6.3、在 spring 配置文件中把 bean 引入进来
        6.4、在一个类的属性上加
            @Resource(name="student_annotation")
            private  Strudent  student;

            ·该注解可以用于“属性”或“方法”上
            ·该注解有一个属性 name,默认值为 “”
        
        6.5、分析整个过程:
            1)启动 spring 容器时, spring 加载了配置文件
            2)在 spring 配置文件中,只要遇到 bean 的配置,就会为该 bean 创建对象。
            3)在纳入 spring 容器的范围内查找所有的bean,看哪些bean 的属性或者方法上有 @Resource
            4)找到 @Resource 注解后,判断该注解 name 的属性是否为空(name 没有写),
                  如果没有写 name 属性,则会让属性名称的值和 spring中ID的值做匹配,如果匹配成功,则赋值。
                                                            如果不成功,则按照类型进行匹配,匹配不成功,则报错。
                  如果有name 属性,则会按照 name 属性的值和 spring 的bean中ID进行匹配,匹配成功,则赋值,不成功则报错。





评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值