Spring Ioc

一,术语介绍

  轻量级:相对于重量级容器(如;EJB容器),Spring容器小,可在小型服务器上运行。

  非侵入性:上层框架不会渗透到下层组件,提高组件的移值性和重用性

       上层框架不依赖实现而是依赖抽象(委托接口),使得实现类变化不会影响上层组件。

  loC(lnversion of Control):使得用 loC使得你不需要在代码中建立依赖关系,在配置中指定,使得程序更灵活,降低依赖。

二,自动绑定  

   可以通过类型(byType)或名称(byName)自动绑定

   byType是按照Bean的class的类型   byName是通过Bean的i或name。

   参考下面代码:

 1 package indi.xiaoen.bean;
 2 
 3 import java.util.Date;
 4 
 5 public class HelloBean {
 6     private String helloWorld;
 7     private Date date;
 8     
 9     public String getHelloWorld() {
10         return helloWorld;
11     }
12     public void setHelloWorld(String helloWorld) {
13         this.helloWorld = helloWorld;
14     }
15     public Date getDate() {
16         return date;
17     }
18     public void setDate(Date date) {
19         this.date = date;
20     }
21     
22     @Override
23     public String toString() {
24         return "HelloBean [helloWorld=" + helloWorld + ", date=" + date + "]";
25     }    
26     
27     
28 }
HelloBean
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 3 <beans>
 4     <bean id="date" class="java.util.Date"/>
 5     <!-- 加autowire 自动绑定  新版本的spring用不了-->    
 6 
 7     <bean id="hello" class="indi.xiaoen.bean.HelloBean" autowire="byName">
 8         <property name="helloWorld" value="hi girl" />
 9         <!-- <property name="date" ref="date" /> -->
10     </bean>
11 </beans>.
bean.xml
1 Resource resource =new ClassPathResource("bean.xml");
2         XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(resource);
3         //自动绑定
4         HelloBean helloBean=(HelloBean)xmlBeanFactory.getBean("hello");
5         System.out.println(helloBean.toString());
TestClss

  上面的这段bean.xml代码里面的 autowire="byName" 意思是通过id=“userDao” 来查找Bean中的userDao对象。

三,集合对象的注入

  重要对 数组,List,Properties,Map,Set 的注入

   请参考下面代码:

 1 package indi.xiaoen.bean;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 import java.util.Map;
 6 
 7 public class SomeBean {
 8     private String[] strArrary;
 9     private HelloBean[] helloBean;
10     private List list;
11     private Map map;
12         
13     public String[] getStrArrary() {
14         return strArrary;
15     }
16     public void setStrArrary(String[] strArrary) {
17         this.strArrary = strArrary;
18     }
19     
20     public HelloBean[] getHelloBean() {
21         return helloBean;
22     }
23     public void setHelloBean(HelloBean[] helloBean) {
24         this.helloBean = helloBean;
25     }
26     public List getList() {
27         return list;
28     }
29     public void setList(List list) {
30         this.list = list;
31     }
32     public Map getMap() {
33         return map;
34     }
35     public void setMap(Map map) {
36         this.map = map;
37     }
38     
39     
40     @Override
41     public String toString() {
42         return "SomeBean [strArrary=" + Arrays.toString(strArrary) + ", helloBean=" + Arrays.toString(helloBean) + ", list=" + list
43                 + ", map=" + map + "]";
44     }    
45     
46 }
SomeBean.java
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 3 <beans>
 4     
 5     <!-- 对象集合的注入方式 -->
 6     <bean class="indi.xiaoen.bean.SomeBean" id="some">
 7         <property name="strArrary">
 8             <list>
 9                 <value>v1</value>
10                 <value>v2</value>
11                 <value>v3</value>
12             </list>
13         </property>
14         
15         <!-- list注入 -->
16         <property name="list">
17             <list>
18                 <value>Now's the time</value>
19                 <ref local="date"/>
20             </list>
21         </property>
22         
23         <!-- 对象注入 -->
24         <property name="helloBean">
25             <list>
26                 <ref bean="hello"/>    
27                 <ref bean="hello"/>    
28                 <ref bean="hello"/>            
29             </list>
30         </property>
31         
32         <!-- 对map的处理 -->
33         <property name="map">
34             <map>
35                 <entry key="two">
36                     <ref local="date"/>
37                 </entry>
38                 <entry key="one">
39                     <value>Now's the time</value>                
40                 </entry>
41             </map>
42         </property>
43     </bean>
44     
45     
46 </beans>
bean.xml
1 //集合对象的注入
2         Resource resource =new ClassPathResource("bean.xml");
3         XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(resource);
4         SomeBean someBean=(SomeBean)xmlBeanFactory.getBean("some");
5         System.out.println(someBean.toString());
TestClss

四,PropertyPlaceholderConfigurer

  PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是BeanFactoryPostProcessor接口的一个实现。

  在Spring中,使用PropertyPlaceholderConfigurer可以在xml配置文件中加入外部属性文件,当然也可以指定外部文件编码。

  PropertyPlaceholderConfigurer可以将上下文(配置文件)中的属性值放入另一个单独的标准java Properties文件中去,在xml文件

  中用${key}替换指定的Properties文件中值,所以只要对Properties文件进行修改,而不用修改xml配置文件。

  具体使用参考下面代码:

  

 1 package indi.xiaoen.bean;
 2 
 3 public class OracleConfigBean {
 4     private String url;
 5     private String userName;
 6     private String drive;
 7     private String pwd;
 8     
 9     
10     public String getUrl() {
11         return url;
12     }
13     public void setUrl(String url) {
14         this.url = url;
15     }
16     public String getUserName() {
17         return userName;
18     }
19     public void setUserName(String userName) {
20         this.userName = userName;
21     }
22     public String getDrive() {
23         return drive;
24     }
25     public void setDrive(String drive) {
26         this.drive = drive;
27     }
28     public String getPwd() {
29         return pwd;
30     }
31     public void setPwd(String pwd) {
32         this.pwd = pwd;
33     }
34     @Override
35     public String toString() {
36         return "OracleConfigBean [url=" + url + ", userName=" + userName + ", drive=" + drive + ", pwd=" + pwd + "]";
37     }
38     
39     
40 }
ConfigBean.java
1 #oracle\u8FDE\u63A5\u6570\u636E\u5E93\u53C2\u6570
2 jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
3 jdbc.driver=oracle.jdbc.driver.OracleDriver
4 jdbc.user=scott
5 jdbc.pwd=tiger
config.properties

 在xml配置文件中引入Properties文件

 1 <!-- 定义properties文件 --><!-- 多个 -->
 2     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" id="configBeans">
 3         <property name="locations">
 4             <list>
 5                 <value>classpath:bean.properties</value>
 6                 <value>classpath:config.properties</value>
 7             </list>    
 8         </property>    
 9     </bean>
10 
11  <!-- 单个 -->
12      <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" id="configBean">
13         <property name="location" value="classpath:config.properties">            
14         </property>    
15     </bean>
16 
17 <!-- 配置数据源 -->
18     <bean id="connectConfig" class="indi.xiaoen.bean.OracleConfigBean">
19         <property name="url"  value="${jdbc.url}"/>
20         <property name="drive"  value="${jdbc.driver}"/>
21         <property name="userName"  value="${jdbc.user}"/>
22         <property name="pwd"  value="${jdbc.pwd}"/>
23     </bean>
bean.xml

 在启动容器时,初始化bean时,${key}就会替换成properties文件中的值

1 //配置文件访问外部的properties文件参数
2          WebApplicationContext applicationContext= WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
3          OracleConfigBean  configBean=(OracleConfigBean)applicationContext.getBean("connectConfig");
4          System.out.println(configBean.toString())
Test.java

 

  

 

 

  

  

   

posted @ 2019-09-20 08:44  听~风在北边  阅读(197)  评论(0编辑  收藏  举报