Spring(属性值的注入)

断了今生、忘了曾经 提交于 2020-04-01 13:05:25
 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:context="http://www.springframework.org/schema/context"
 5     xmlns:util="http://www.springframework.org/schema/util"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
 8         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
 9 <bean id="hw1" class="com.hanqi.HelloWorld"><!-- id="实例化的对象名" class="需要实例化的对象" -->
10     <!-- 属性注入 -->
11     <property name="name" value="jack"></property><!-- name="属性名" value="属性值" -->
12 </bean>
13     <!-- 构造器注入 -->
14 <bean id="hw2" class="com.hanqi.HelloWorld">
15     <constructor-arg index="0" value="Tom"></constructor-arg><!-- 按索引匹配入参 -->
16 </bean>
17     <!-- 构造器注入 -->
18 <bean id="hw3" class="com.hanqi.HelloWorld">
19     <constructor-arg type="int" value="20"></constructor-arg><!-- 按照数据类型匹配入参 -->
20     <constructor-arg type="java.lang.String" value="Mike"></constructor-arg>
21 </bean>
22     <!-- 构造器注入 -->
23 <bean id="hw4" class="com.hanqi.HelloWorld">
24     <!--按照属性名匹配入参  如果只设定value,那么就按照参数个数寻找匹配上的第一个构造方法 -->
25     <constructor-arg name="name"> <value><![CDATA[<Rose>]]></value></constructor-arg>
26     <constructor-arg name="age" value="30"></constructor-arg>
27 </bean>
28     <!-- 外部Bean -->
29 <bean id="us1" class="com.hanqi.User">
30     <property name="userCode" value="123"></property>
31     <property name="userName" value="张三"></property>
32 </bean>
33 <bean id="hw5" class="com.hanqi.HelloWorld">
34     <property name="user" ref="us1"></property><!-- 传入外部Bean给属性赋值 -->
35 </bean>
36     <!-- 内部Bean -->
37 <bean id="hw6" class="com.hanqi.HelloWorld">
38 <property name="user" >
39 <bean class="com.hanqi.User">
40 <property name="userCode" value="123"></property>
41 <property name="userName" value="李四"></property>
42 </bean>
43 </property>
44 </bean>
45 <!-- 给List集合进行赋值 -->
46 <util:list id="namelist">
47     <value>"abc"</value>
48     <value>"ABC"</value>
49     <value>"DEF"</value>
50 </util:list>
51 <bean id="hw7" class="com.hanqi.HelloWorld">
52     <property name="names" ref="namelist"></property><!-- 给list类型的属性进行赋值 -->
53 </bean>
54 <!-- 给set集合进行赋值 -->
55 <util:set id="nameset">
56     <value>"abc"</value>
57 </util:set>
58 <!-- 给map集合进行赋值 -->
59 <util:map id="namemap">
60     <entry key="u1" value="淄博1"></entry>
61     <entry key="u2" value="淄博2"></entry>
62     <entry key="u3" value="淄博3"></entry>
63     <entry key="u4" value="淄博4"></entry>
64 </util:map>
65 
66 </beans>
 1 package com.hanqi;
 2 
 3 import java.util.List;
 4 
 5 public class HelloWorld 
 6 {
 7     private String name;
 8     private int age;
 9     
10     private User user;
11     private List<String> names;
12     
13     
14     
15     public List<String> getNames() {
16         return names;
17     }
18 
19     public void setNames(List<String> names) {
20         this.names = names;
21     }
22     public User getUser() {
23         return user;
24     }
25     public void setUser(User user) {
26         this.user = user;
27     }
28 
29     public String getName() {
30         return name;
31     }
32 
33     public void setName(String name) {
34         this.name = name;
35     }
36 
37     public int getAge() {
38         return age;
39     }
40 
41     public void setAge(int age) {
42         this.age = age;
43     }
44 
45     
46 
47     public HelloWorld( int age,String name) {
48         super();
49         this.age = age;
50         this.name = name;
51     }
52     
53     public HelloWorld(String name, int age) {
54         super();
55         this.name = name;
56         this.age = age;
57     }
58 
59     public HelloWorld(String name) {
60         super();
61         this.name = name;
62     }
63     
64     public HelloWorld() {
65         super();
66     }
67 
68 
69 
70     
71     public void sayHello()
72     {
73         System.out.println("你好"+name);
74     }
75     
76     public void sayHelloUser()
77     {
78         System.out.println("你好User"+user.getUserName());
79     }
80     
81     public void ShowList()
82     {
83         System.out.println(names);
84     }
85 }
 1 package com.hanqi;
 2 
 3 public class User 
 4 {
 5     private String userCode;
 6     
 7     private String userName;
 8     
 9     public String getUserCode() {
10         return userCode;
11     }
12     public void setUserCode(String userCode) {
13         this.userCode = userCode;
14     }
15     public String getUserName() {
16         return userName;
17     }
18     public void setUserName(String userName) {
19         this.userName = userName;
20     }
21 }
 1 package com.hanqi;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Test 
 7 {
 8 
 9     public static void main(String[] args)
10     {
11         HelloWorld hw=new HelloWorld();
12         hw.setName("tom");
13         hw.sayHello();
14         //spring方式
15         //1得到Spring的IOC容器
16         ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
17         //2从容器中得到bean实例
18         HelloWorld hw1=(HelloWorld)ac.getBean("hw1");//得到的是对象,需要转成需要的类型
19         //3调用方法
20         hw1.sayHello();
21         
22         HelloWorld hw2=(HelloWorld)ac.getBean("hw2");
23         hw2.sayHello();
24         
25         HelloWorld hw3=(HelloWorld)ac.getBean("hw3");
26         hw3.sayHello();
27         
28         HelloWorld hw4=(HelloWorld)ac.getBean("hw4");
29         hw4.sayHello();
30         
31         HelloWorld hw5=(HelloWorld)ac.getBean("hw5");
32         hw5.sayHelloUser();
33         
34         HelloWorld hw6=(HelloWorld)ac.getBean("hw6");
35         hw6.sayHelloUser();
36         
37         HelloWorld hw7=(HelloWorld)ac.getBean("hw7");
38         hw7.ShowList();
39     }
40 
41 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!