Spring配置文件

  • 基礎配置模版
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd">
    
      <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
      </bean>
    
      <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
      </bean>
    
      <!-- more bean definitions go here -->
    
    </beans>
       
  • 配置文件的名字可以根據自己需要進行定義
  • 使用這個配置文件
    		//加載配置文件,創建配置文件的bean
    		ApplicationContext context =new ClassPathXmlApplicationContext("Spring.xml");
    		//第一種方法:根據id取到配置的bean
    		Person per1 = (Person) context.getBean("person");
    		//第二種
    		Person per2 =  context.getBean("person",Person.class);
    		//默認是單例模式,spring容器一直持有這個對象
    		System.out.println(per1==per2);
       
  • 設置成非單例模式,每次想spring容器請求對象,spring都創建一個新的返回
    	<bean id="t3" class="i.test.Test3" scope="prototype" />
     
  • 可以一次加載多個配置文件
    ApplicationContext context =new ClassPathXmlApplicationContext("Spring.xml","Beans.xml");
      
  • 將多個配置文件導入一個配置文件中
    <beans>
    
        <import resource="services.xml"/>
        <import resource="resources/messageSource.xml"/>
        <import resource="/resources/themeSource.xml"/>
    
        <bean id="bean1" class="..."/>
        <bean id="bean2" class="..."/>
    
    </beans>
     
  • 爲id設置別名
    	<!-- id="t2" 設置別名 t3 -->
    	<alias name="t2" alias="t3"/>
      
  • 靜態工廠方法     
    public class FactoryBean {
    	private static Test2 t2 = new Test2();
    
    	private FactoryBean() {}
    
    	public static Test2 getInstanceT2() {
    		return t2;
    	}
    }
     
    	<!-- Spring不會初始化這個class,而是每次調用getInstanceT2 -->
    	<bean id="t2" class="i.test.FactoryBean" factory-method="getInstanceT2" />
     如果getInstanceT2 不是靜態的方法,就要將類初始化,然後調用
    	<bean id="factoryBean" class="i.test.FactoryBean" />
    	<bean id="t2" factory-bean="factoryBean" factory-method="getInstanceT2" />
      
  •  配置有參數構造器
    public class Test3 {
    
    	public Test3(Test1 t1, String str, int i) {
    	}
    }
    
     配置1
    	<bean id="t1" class="i.test.Test1" />
    	<bean id="t3" class="i.test.Test3">
    		<constructor-arg ref="t1" />
    		<constructor-arg  type="java.lang.String"  value="1"/>
    		<constructor-arg  type="int" value="2"/>
    	</bean>
     配置2:
    	<bean id="t1" class="i.test.Test1" />
    	<bean id="t3" class="i.test.Test3">
    		<constructor-arg index="0" ref="t1" />
    		<constructor-arg index="1"  value="1"/>
    		<constructor-arg index="2" value="2"/>
    	</bean>
     配置3:
    	<bean id="t1" class="i.test.Test1" />
    	<bean id="t3" class="i.test.Test3">
    		<constructor-arg name="t1" ref="t1" />
    		<constructor-arg name="i"  value="1"/>
    		<constructor-arg name="str" value="2"/>
    	</bean>
      
  •  set方法注入
    public class Test3 {
    	private Test1 t1;
    	private Test2 t2;
    	private int i;
    
    	public void setT1(Test1 t1) {
    		this.t1 = t1;
    	}
    
    	public void setT2(Test2 t2) {
    		this.t2 = t2;
    	}
    
    	public void setInteger(int i) {
    		this.i = i;
    	}
    }
     配置
    	<bean id="t1" class="i.test.Test1" />
    	<bean id="t2" class="i.test.Test2" />
    	<bean id="t3" class="i.test.Test3">
    		<property name="test1"><ref bean="t1" /></property>
    		<property name="test2" ref="t2" />
    		<property name="integer" value="1" />
    	</bean>
     
  • 構造器注入
    public class Test3 {
    	private Test1 t1;
    	private Test2 t2;
    	private int i;
    
    	public Test3(Test1 t1, Test2 t2, int i) {
    		this.t1 = t1;
    		this.t2 = t2;
    		this.i = i;
    	}
    }
     
    	<bean id="t1" class="i.test.Test1" />
    	<bean id="t2" class="i.test.Test2" />
    
    	<bean id="t3" class="i.test.Test3">
    		<constructor-arg><ref bean="t1" /></constructor-arg>
    		<constructor-arg ref="t2" />
    		<constructor-arg type="int" value="1" />
    	</bean>
     
  • 配置一個帶參數的靜態工廠方法
    public class Test3 {
    	private Test1 t1;
    	private Test2 t2;
    	private int i;
    
    	private Test3(Test1 t1, Test2 t2, int i) {
    		this.t1 = t1;
    		this.t2 = t2;
    		this.i = i;
    	}
    
    	public static Test3 createInstance(Test1 t1, Test2 t2, int i){
    		return  new Test3(t1, t2, i);
    	}
    }
     
    	<bean id="t1" class="i.test.Test1" />
    	<bean id="t2" class="i.test.Test2" />
    
    	<bean id="t3" class="i.test.Test3" factory-method="createInstance">
    		<constructor-arg ref="t1" />
    		<constructor-arg ref="t2" />
    		<constructor-arg value="1" />
    	</bean>
     
  • 定義<property/> or <constructor-arg/>內部屬性bean
    <bean id="outer" class="...">
    <!-- instead of using a reference to a target bean, simply define the target bean inline -->
    <property name="target">
      <bean class="com.example.Person"> <!-- this is the inner bean -->
        <property name="name" value="Fiona Apple"/>
        <property name="age" value="25"/>
      </bean>
    </property>
    </bean>
     
  • collections
    <bean id="moreComplexObject" class="example.ComplexObject">
    <!-- results in a setAdminEmails(java.util.Properties) call -->
    <property name="adminEmails">
      <props>
          <prop key="administrator">[email protected]</prop>
          <prop key="support">[email protected]</prop>
          <prop key="development">[email protected]</prop>
      </props>
    </property>
    <!-- results in a setSomeList(java.util.List) call -->
    <property name="someList">
      <list>
          <value>a list element followed by a reference</value>
          <ref bean="myDataSource" />
      </list>
    </property>
    <!-- results in a setSomeMap(java.util.Map) call -->
    <property name="someMap">
      <map>
          <entry key="an entry" value="just some string"/>
          <entry key ="a ref" value-ref="myDataSource"/>
      </map>
    </property>
    <!-- results in a setSomeSet(java.util.Set) call -->
    <property name="someSet">
      <set>
          <value>just some string</value>
          <ref bean="myDataSource" />
      </set>
    </property>
    </bean>
     
  • idref
    	<bean id="theTargetBean" class="..." />
    
    	<bean id="theClientBean" class="...">
    		<property name="targetName">
    			<idref bean="theTargetBean" />
    		</property>
    	</bean>
    
    	<!--上面效果等同下面,idref在加載時會檢查是否存在id=“theTargetBean”的bean -->
    
    	<bean id="client" class="...">
    		<property name="targetName" value="theTargetBean" />
    	</bean>
      
  • local跟bean的區別只加載當前xml的bean
     <ref local="someBean"/>
     
    <property name="targetName">
     <idref local="theTargetBean"/>
    </property>
     
  • 可以通過parent來繼承屬性 
    	<!-- abstract="true" 容器不會初始化這個bean,可作爲公共屬性使用 -->
    	<bean id="parent" abstract="true">
    		<property name="aa" value="xiao" />
    		<property name="ee" value="xiaoe" />
    	</bean>
    
    	<!-- 繼承id="parent"的屬性 -->
    	<bean id="child" class="i.test.Test3" parent="parent">
    		<property name="cc" value="xiaoc" />
    	</bean>
     

 

  • 使用p來簡化配置文件
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xmlns:p="http://www.springframework.org/schema/p"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    
    </beans>
     
    	<!-- 以前配置 -->
    	<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost:3306/mysql" />
    		<property name="username" value="root" />
    		<property name="password" value="xiaobai" />
    	</bean>
    
    	<!-- 簡化後 -->
    	<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close" 
    		p:driverClassName="com.mysql.jdbc.Driver"
    		p:url="jdbc:mysql://localhost:3306/mysql" 
    		p:username="root" 
    		p:password="xiaobai" />
      配置一個bean
    	<bean id="t1" class="i.test.Test1" />
    	<bean id="t3" class="i.test.Test3" p:traget-ref="t1" />
  •   使用c簡化配置文件
    public class Test3 {
    
    	public Test3(String str, Test1 t1, int i) {
    		System.out.println(str+t1+i);
    	}
    
    }
     
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:c="http://www.springframework.org/schema/c"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    	<bean id="t1" class="i.test.Test1" />
    	
       <!-- 一般的配置方法 -->
    	<bean id="t" class="i.test.Test3">
    		<constructor-arg value="go" />
    		<constructor-arg ref="t1" />
    		<constructor-arg value="3" />
    	</bean>
    
       <!-- 簡化的配置方法 -->
    	<bean id="t3" class="i.test.Test3" c:str="go" c:t1-ref="t1" c:i="3" />
    
    </beans>
     

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章