• SSM-Spring-07:Spring基于注解的di注入


    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

    注解:

      说起注解,哇哦,每个人都或多或少的用到过

      像什么@Overried,@Test,@Param等等之前就早已熟悉的注解,现在要用注解实现di的注入

      注解的本质是什么?就是一个接口,他里面的参数是什么呢?就是这个接口里面的方法,so,我们怎么做?

    案例如下:

      基于注解的jar包就不用说了,按照之前的博客走下来的无需再添加新的jar包

      还是俩个类,一个car,一个student,学生有一辆小汽车,基于注解的di注入

        Car类

    package cn.dawn.day07annotationdi;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    /**
     * Created by Dawn on 2018/3/5.
     */
    @Component("car")
    public class Car {
        @Value("红色")
        private String color;
        @Value("奔驰")
        private String type;
    
        public String getColor() {
            return color;
        }
    
        public void setColor(String color) {
            this.color = color;
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    }

        @Componed("car")  //表示这个生成的对象的名字

        @Value("奔驰")    //用于给属性赋值

      Student类

    package cn.dawn.day07annotationdi;
    
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.Resource;
    
    /**
     * Created by Dawn on 2018/3/5.
     */
    //student类
        @Component("student")
    public class Student {
            @Value("老胡小子,呵呵哒")
        private String name;
            @Value("20")
        private Integer age;
    
            //@Resource(name = "car")
        @Autowired
        @Qualifier(value = "car")
        private Car car;
    
        //带参构造
        public Student(String name, Integer age, Car car) {
            this.name = name;
            this.age = age;
            this.car = car;
        }
    
        //无参构造
        public Student() {
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public Car getCar() {
            return car;
        }
    
        public void setCar(Car car) {
            this.car = car;
        }
    }

        一样的就不做解释了,说一下下面这几个

        @Resource(name="car")    //这个是javax包下的注解,可以实现域属性的注入,下面还有一种方式,

        @AutoWried

        @Qualifier(value="car")    //这两行联用,他是spring的注解,也是给对象的域属性赋值

      

      在Spring的配置文件中,需要配置一点内容,首先导入命名空间context,和注解的包扫描器(我是idea,写完下面的节点,上面的命名空间自动生成)

    <?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:aop="http://www.springframework.org/schema/aop"
           xmlns:p="http://www.springframework.org/schema/p" 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/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        <context:component-scan base-package="cn.dawn.day07annotationdi">
        </context:component-scan>
    
    </beans>

      单测方法

    package cn.dawn.day07annotationdi;
    
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Created by Dawn on 2018/3/3.
     */
    public class test20180306 {
    
    
        @Test
        /*di注解注入*/
        public void t01(){
            ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day07annotationdi.xml");
            Student student = (Student) context.getBean("student");
            System.out.println("学生"+student.getName()+""+student.getCar().getType());
        }
    }
  • 相关阅读:
    CRoss IndustryStandard Process- for Data Mining 跨行业数据挖掘标准流程(中)
    CRoss IndustryStandard Process- for Data Mining 跨行业数据挖掘标准流程(上)
    window下安装pip工具,再利用pip安装工具来安装其他的python包
    采用ubuntu系统来安装tensorflow
    eclipse中添加python开发环境
    分类器的评价指标
    HBase的基本架构及其原理介绍
    快速排序的递归方式和非递归方式
    DeepCTR专题:DeepFM论文学习和实现及感悟
    DeepCTR专题:Neural Factorization Machines 论文学习和实现及感悟
  • 原文地址:https://www.cnblogs.com/DawnCHENXI/p/8516172.html
Copyright © 2020-2023  润新知