zoukankan      html  css  js  c++  java
  • spring4——IOC之基于注解的依赖注入(DI )

    spring容器对于Bean的创建和对象属性的依赖注入提供了注解的支持,让我们在开发中能够更加便捷的实现对象的创建和对象属性的依赖注入。

    一,对于Bean的创建spring容器提供了以下四个注解的支持:

    1、@Component

    2、@Repository  dao层实现类的注解

    3、@Service    service层实现类的注解

    4、@Controller    controller层实现类的注解

    以上四个注解在普通使用中是等效的,但在web项目中为了区分三层架构中不同层之间Bean的创建,为了避免注解使用的混乱,使用后三个注解进行区分


    二,对于Bean属性的依赖注入分为两类,一类是对于属性是String类型或者基本数据类型Spring容器提供了@Value这个注解,另一类是对于属性是对象的提供了@Autowired和@Resource这两个注解。

    其中,@Autowired这个注解是spring框架自带的注解,而@Resource(javax.annotation.Resource)这个注解是javax扩展包中注解规范的一种,而spring对这一注解提供了支持。

    下面我们通过实验来说明注解对于bean创建和bean属性依赖注入的实现。

    首先要在配置文件中配置注解扫描的驱动。

    <context:annotation-config/>
    <context:component-scan base-package="com.opensource"/>

    这里提一句,如果配置了注解扫描包的范围,也就是第二行,那么<context:annotation-config/>可以不用配置,因为配置扫描包的范围后,注解的驱动也就有了。
    实验一,bean的创建,因为spring容器对于bean创建的四个注解是等效,这里我们使用@Component这个注解

    Student类:
    @Component
    public class Student {
    
        public Student(){
            System.out.println("spring容器调用Student类的无参构造器");
        }
    
    
    

    测试类:
    public class MyTest {
        public static void main(String[] args) {
            
            ApplicationContext ac = new ClassPathXmlApplicationContext("spring-bean.xml");
    
        }
    
    }
    
    
    

    实验结果:


     实验二:bean属性为String类型及基本数据类型的的依赖注入

    student类:

    @Component(value = "student")
    public class Student {
        
        @Value("张三")
        private String name;
        @Value("23")
        private int age;
        
        public String getName() {
            return name;
        }
    
        public int getAge() {
            return age;
        }
    }

    在这里 @Component(value = "student") value指定的是bean的id,另外对于注解方式实现的依赖注入,bean的属性无需再提供setter方法。

    测试类:

    public class MyTest {
        public static void main(String[] args) {
            
            ApplicationContext ac = new ClassPathXmlApplicationContext("spring-bean.xml");
            
            Student student = (Student)ac.getBean("student");
            System.out.println(student.getName());
            System.out.println(student.getAge());
        }
    
    }

    实验结果:

    实验三:bean属性为java对象的依赖注入。

    对于bean属性为java对象的依赖注入,分为两种一种是byName类型的,一种是byType类型的。而使用的注解分别是@Autowired@Resource,对于byType类型的两者的使用没有区别,但是对于byName类型的@Autowired要使用联合注解@Qualifier,所以说使用@Resource较为简单。这里我们在研究对于bean属性为java对象的依赖注入时就使用@Resource这个注解了。

    实验3.1:按照byName类型对bean属性为java对象的依赖注入。

    上个实验中的Student类保持不变,再提供一个Teacher类。

    @Component("teacher")
    public class Teacher {
        
        @Resource(name = "student")
        private Student student;
        
        public Student getStudent() {
            return student;
        }
    }

    测试类:

    public class MyTest {
        public static void main(String[] args) {
            
            ApplicationContext ac = new ClassPathXmlApplicationContext("spring-bean.xml");
            Teacher teacher = (Teacher)ac.getBean("teacher");
            System.out.println(teacher.getStudent().getName());
            System.out.println(teacher.getStudent().getAge());
        }
    
    }

    实验结果:

    实验3.2:按照byType类型对bean属性为java对象的依赖注入。

    对于这种方式的依赖注入,只需分别修改Student类和Teacher类。

    @Component
    public class Student {
        
        @Value("张三")
        private String name;
        @Value("23")
        @Qualifier
        private int age;
        
        public String getName() {
            return name;
        }
    
        public int getAge() {
            return age;
        }
    }
    @Component("teacher")
    public class Teacher {
        
        @Resource
        private Student student;
        
        public Student getStudent() {
            return student;
        }
    }

    当然在这里Student类的id也是可以保留的,这里这么做是为了说明问题。实验结果同上。

     最后说一点,我们作为程序员,研究问题还是要仔细深入一点的。当你对原理了解的有够透彻,开发起来也就得心应手了,很多开发中的问题和疑惑也就迎刃而解了,而且在面对其他问题的时候也可做到触类旁通。当然在开发中没有太多的时间让你去研究原理,开发中要以实现功能为前提,可等项目上线的后,你有大把的时间或者空余的时间,你大可去刨根问底,深入的去研究一项技术,为觉得这对一名程序员的成长是很重要的事情。

  • 相关阅读:
    DOS命令如何删除文件和文件夹
    屏蔽电信流氓弹出广告
    Wcf客户端配置里Endpoint的Name属性无效
    正则替换中的一个Bug
    【原】解决 iframe 在 iPad 上不能滚动的问题
    【原】JSON.parse() 和 JSON.stringify()
    【原】SQL 使用 PIVOT 和 UNPIVOT 行转列 列转行
    【原】How to fix: Handler “PageHandlerFactoryIntegrated” has a bad module “ManagedPipelineHandler” in its module list
    【原】DES Encrypt / Decrypt
    【原】JSON.parse() 和 JSON.stringify()
  • 原文地址:https://www.cnblogs.com/cdf-opensource-007/p/6441296.html
Copyright © 2011-2022 走看看