Spring IOC基础使用

Posted Mr. Tan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring IOC基础使用相关的知识,希望对你有一定的参考价值。

先下载、导入核心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基础使用的主要内容,如果未能解决你的问题,请参考以下文章

Spring基础入门之IoC注入

Spring基础:IOC概念引入

spring基础之二-;Spring IOC基本使用

Spring IOC基础使用

[死磕 Spring 17/43] --- IOC 之从单例缓存中获取单例 bean

spring学习--基础概念