码迷,mamicode.com
首页 > 编程语言 > 详细

Spring IOC基础使用

时间:2018-11-05 20:53:19      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:com   info   XML   ioc   style   enc   ati   entry   xmla   

先下载、导入核心jar包

技术分享图片

编写Book类和CollectionUse类

技术分享图片
 1 package MyPackageOne;
 2 
 3 public class Book {
 4     private String title;
 5     private double price;
 6 
 7     public Book() {
 8         this("", 0.0);
 9     }
10 
11     public Book(String title, double price) {
12         this.title = title;
13         this.price = price;
14     }
15 
16     public String getTitle() {
17         return title;
18     }
19 
20     public void setTitle(String title) {
21         this.title = title;
22     }
23 
24     public double getPrice() {
25         return price;
26     }
27 
28     public void setPrice(double price) {
29         this.price = price;
30     }
31 
32     @Override
33     public String toString() {
34         return "title: " + title + ", price: " + price;
35     }
36 }
View Code
技术分享图片
 1 package MyPackageOne;
 2 
 3 import java.lang.reflect.Array;
 4 import java.util.Arrays;
 5 import java.util.List;
 6 import java.util.Map;
 7 import java.util.Set;
 8 
 9 public class CollectionUse {
10     private List<String> stringList;
11     private String[] strings;
12     private Set<String> stringSet;
13     private Map<String, String> stringStringMap;
14 
15     public List<String> getStringList() {
16         return stringList;
17     }
18 
19     public void setStringList(List<String> stringList) {
20         this.stringList = stringList;
21     }
22 
23     public String[] getStrings() {
24         return strings;
25     }
26 
27     public void setStrings(String[] strings) {
28         this.strings = strings;
29     }
30 
31     public Set<String> getStringSet() {
32         return stringSet;
33     }
34 
35     public void setStringSet(Set<String> stringSet) {
36         this.stringSet = stringSet;
37     }
38 
39     public Map<String, String> getStringStringMap() {
40         return stringStringMap;
41     }
42 
43     public void setStringStringMap(Map<String, String> stringStringMap) {
44         this.stringStringMap = stringStringMap;
45     }
46 
47     @Override
48     public String toString() {
49         return "CollectionUse{" +
50                 "stringList=" + stringList +
51                 ", strings=" + Arrays.toString(strings) +
52                 ", stringSet=" + stringSet +
53                 ", stringStringMap=" + stringStringMap +
54                 ‘}‘;
55     }
56 }
View Code

在applicationContext.xml中写好类实例

技术分享图片
 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        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5     <bean id="bookOne" class="MyPackageOne.Book">
 6         <property name="title" value="java"></property>
 7         <property name="price" value="15.2"></property>
 8     </bean>
 9     <bean id="collectionUseOne" class="MyPackageOne.CollectionUse">
10         <property name="stringList">
11             <list>
12                 <value>帅气的我list</value>
13                 <value>勇气的我list</value>
14                 <value>厉害的我list</value>
15                 <value>无敌的我list</value>
16                 <value>666的我list</value>
17             </list>
18         </property>
19         <property name="strings">
20             <array>
21                 <value>帅气的我string</value>
22                 <value>勇气的我string</value>
23                 <value>厉害的我string</value>
24                 <value>无敌的我string</value>
25                 <value>666的我string</value>
26             </array>
27         </property>
28         <property name="stringSet">
29             <set>
30                 <value>帅气的我set</value>
31                 <value>勇气的我set</value>
32                 <value>厉害的我set</value>
33                 <value>无敌的我set</value>
34                 <value>666的我set</value>
35             </set>
36         </property>
37         <property name="stringStringMap">
38             <map>
39                 <entry>
40                     <key><value>name</value></key>
41                     <value>无敌</value>
42                 </entry>
43                 <entry>
44                     <key><value>你好啊</value></key>
45                     <value>enen</value>
46                 </entry>
47             </map>
48         </property>
49     </bean>
50 </beans>
View Code

主函数中观察获取结果:

技术分享图片
1 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
2 Book bookOne = (Book) ac.getBean("bookOne");
3 System.out.println(bookOne);
4 CollectionUse cu = (CollectionUse) ac.getBean("collectionUseOne");
5 System.out.println(cu);
View Code

技术分享图片

 

Spring IOC基础使用

标签:com   info   XML   ioc   style   enc   ati   entry   xmla   

原文地址:https://www.cnblogs.com/sqdtss/p/9911191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!