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">administrator@example.org</prop>
          <prop key="support">support@example.org</prop>
          <prop key="development">development@example.org</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>
     

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值