java

DI注入

rzk · 4月5日 · 2020年本文共2664个字 · 预计阅读9分钟83次已读
 1  2 
 3 public class Address {
 4     private String address;
 5 
 6     public String getAddress() {
 7         return address;
 8     }
 9 
10     public void setAddress(String address) {
11         this.address = address;
12     }
13 
14     @Override
15     publi睿共享c String toString() {
16         return "Addr睿共享ess{" +
17                 "address='" + address + ''' +
18                 '}';
19     }
20 }
 1 public class Age {
 2     private int age;
 3 
 4     public int getAge() {
 5         return age;
 6     }
 7 
 8     public void setAge(int age) {
 9         this.age = age;
10     }
11 
12     @Override
13     public String toString() {
14         return "Age{" +
15                 "age=" + age +
16                 '}';
17     }
18 }
  1 public class Student {
  2     private String name;
  3     /*自动装配*/
  4 //    @Autowired
  5     private Address address;
  6 //    @Autowired
  7     private Age age;
  8     private String[] books;
  9     private List hobbys;
 10     private Map card;
 11     private Set games;
 12     private String wife;
 13     private Properties info;
 14 
 15     public String getName() {
 16         return name;
 17     }
 18 
 19     public void setName(String name) {
 20         this.name = name;
 21     }
 22 
 23     public Address getAddress() {
 24         return address;
 25     }
 26 
 27     public void setAddress(Address address) {
 28         this.address = address;
 29     }
 30 
 31     public Age getAge() {
 32         return age;
 33     }
 34 
 35     public void setAge(Age age) {
 36         this.age = age;
 37     }
 38 
 39     public String[] getBooks() {
 40         return books;
 41     }
 42 
 43     public void setBooks(String[] books) {
 44         this.books = books;
 45     }
 46 
 47     public List getHobbys() {
 48         return hobbys;
 49     }
 50 
 51     public void setHobbys(List hobbys) {
 52         this.hobbys = hobbys;
 53     }
 54 
 55     public Map getCard() {
 56         return card;
 57     }
 58 
 59     public void setCard(Map card) {
 60         this.card = card;
 61     }
 62 
 63     public Set getGames() {
 64         return games;
 65     }
 66 
 67     public void setGames(Set games) {
 68         this.games = games;
 69     }
 70 
 71     public String getWife() {
 72         return wife;
 73     }
 74 
 75     public void setWife(String wife) {
 76         this.wife = wife;
 77     }
 78 
 79     public Properties getIn睿共享fo() {
 80         return info;
 81     }
 82 
 83     public void setInfo(Properties info) {
 84         this.info = info;
 85     }
 86 
 87     @Override
 88     public Stri睿共享ng toString() {
 89         return "Student{" +
 90                 "name='" + name + ''' +
 91                 ", address=" + address +
 92                 ", age=" + age +
 93                 ", books=" + Arrays.toString(books) +
 94                 ", hobbys=" + hobbys +
 95                 ", card=" + card +
 96                 ", games=" + games +
 97                 ", wife='" + wife + ''' +
 98                 ", info=" + info +
 99                 '}';
100     }
101 }

applicationContext.xml 需要加

1 http://www.springframework.org/schema/context 2 http://www.springframework.org/schema/context/spring-context.xsd

 1 "1.0" encoding="UTF-8"?>
 2 "http://www.springframework.org/schema/beans"
 3        xmlns_xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns_context="http://www.springframework.org/schema/context"
 5        xsi_schemaLocation="http://www.springframework.org/schema/beans
 6         https://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context
 8         http://www.springframework.org/schema/context/spring-context.xsd">
 9     
10     base-package="com.rzk.pojo"/>
11     "address" class="com.rzk.pojo.Address">
12         "address" value="中国社区"/>
13     
14 
15     "age" class="com.rzk.pojo.Age">
16         "age" value="18"/>
17     
18 
19     "student" class="com.rzk.pojo.Student">
20         
21         "name" value="小明"/>
22         
23         "address" ref="address"/>
24         "age" ref="age"/>
25 
26         
27         "books">
28             
29                 西游记1
30                 西游记2
31                 西游记3
32                 西游记4
33             
34         
35         
36         "hobbys">
37             
38                 看电影
39                 看电影
40                 看电影
41             
42         
43         
44         "card">
45             
46                 "1" key="A"/>
47                 "2" key="B"/>
48                 "3" key="C"/>
49             
50         
51         
52         "games">
53             <set>
54                 lol
55                 开门狗
56                 刺客信条
57             set>
58         
59         
60         "wife">
61             <null/>
62         
63         
64         "info">
65             
66                 "driver">连接
67                 "root">用户
68                 "pwd">密码
69             
70         
71     
72 

测试

1 public class Mytest {
2     public static void main(String[] args) {
3         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
4         Student student = (Student) context.getBean("student");
5         System.out.println(student);
6 
7     }
8 }

DI注入

————————————————–

加上自动装配 需要在xml添加

DI注入

在xml配置箭头所指的

DI注入

加上注解的话可以把xml配置ref bean注释掉

DI注入

注解就 替换了xml的配置

/*autowired睿共享 默认是byType*/
autowire="byType"
autowire="byName"
如果设置了byName 的话会直接找 id
如果设置了byType 的话会直接找 class属性所对应的类名
@Autowired :自动转配通过类型,名字
如果Autowired不能通过唯一自动装配上属性,如果想要用byName,则需要通过 @Qualifier(value = "xxx")
0 条回应