1
2 1------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
2
注解:
说起注解,哇哦,每个人都或多或少的用到过
像什么@Overried,@Test,@Param等等之前就早已熟悉的注解,现在要用注解实现di的注入
注解的本质是什么?就是一个接口,他里面的参数是什么呢?就是这个接口里面的方法,so,我们怎么做?
案例如下:
基于注解的jar包就不用说了,按照之前的博客走下来的无需再添加新的jar包
还是俩个类,一个car,一个student,学生有一辆小汽车,基于注解的di注入
Car类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 1package cn.dawn.day07annotationdi;
2
3import org.springframework.beans.factory.annotation.Value;
4import org.springframework.stereotype.Component;
5
6/**
7 * Created by Dawn on 2018/3/5.
8 */
9@Component("car")
10public class Car {
11 @Value("红色")
12 private String color;
13 @Value("奔驰")
14 private String type;
15
16 public String getColor() {
17 return color;
18 }
19
20 public void setColor(String color) {
21 this.color = color;
22 }
23
24 public String getType() {
25 return type;
26 }
27
28 public void setType(String type) {
29 this.type = type;
30 }
31}
32
@Componed("car") //表示这个生成的对象的名字
@Value("奔驰") //用于给属性赋值
Student类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 1package cn.dawn.day07annotationdi;
2
3
4import org.springframework.beans.factory.annotation.Autowired;
5import org.springframework.beans.factory.annotation.Qualifier;
6import org.springframework.beans.factory.annotation.Value;
7import org.springframework.stereotype.Component;
8
9import javax.annotation.Resource;
10
11/**
12 * Created by Dawn on 2018/3/5.
13 */
14//student类
15 @Component("student")
16public class Student {
17 @Value("老胡小子,呵呵哒")
18 private String name;
19 @Value("20")
20 private Integer age;
21
22 //@Resource(name = "car")
23 @Autowired
24 @Qualifier(value = "car")
25 private Car car;
26
27 //带参构造
28 public Student(String name, Integer age, Car car) {
29 this.name = name;
30 this.age = age;
31 this.car = car;
32 }
33
34 //无参构造
35 public Student() {
36 }
37
38 public String getName() {
39 return name;
40 }
41
42 public void setName(String name) {
43 this.name = name;
44 }
45
46 public Integer getAge() {
47 return age;
48 }
49
50 public void setAge(Integer age) {
51 this.age = age;
52 }
53
54 public Car getCar() {
55 return car;
56 }
57
58 public void setCar(Car car) {
59 this.car = car;
60 }
61}
62
一样的就不做解释了,说一下下面这几个
@Resource(name="car") //这个是javax包下的注解,可以实现域属性的注入,下面还有一种方式,
@AutoWried
@Qualifier(value="car") //这两行联用,他是spring的注解,也是给对象的域属性赋值
在Spring的配置文件中,需要配置一点内容,首先导入命名空间context,和注解的包扫描器(我是idea,写完下面的节点,上面的命名空间自动生成)
1
2
3
4
5
6
7
8
9
10
11
12
13
14 1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/aop
9 http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
10 <context:component-scan base-package="cn.dawn.day07annotationdi">
11 </context:component-scan>
12
13</beans>
14
单测方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 1package cn.dawn.day07annotationdi;
2
3
4import org.junit.Test;
5import org.springframework.context.ApplicationContext;
6import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8/**
9 * Created by Dawn on 2018/3/3.
10 */
11public class test20180306 {
12
13
14 @Test
15 /*di注解注入*/
16 public void t01(){
17 ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day07annotationdi.xml");
18 Student student = (Student) context.getBean("student");
19 System.out.println("学生"+student.getName()+"开"+student.getCar().getType());
20 }
21}
22