-
基础配置模版
<?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" />
相关推荐
为了保护这些敏感信息不被非法访问或篡改,我们可以对Spring配置文件进行加密处理。本文将深入探讨如何在Java环境中,利用TE网络技术实现Spring配置文件的加密。 首先,我们需要理解Spring配置文件的基本结构。...
spring配置文件实例
Spring 配置文件详解 Spring 配置文件是 Spring 框架中最重要的配置文件之一,它负责定义和配置应用程序的Bean对象,以及它们之间的依赖关系。Spring 配置文件通常以XML文件的形式存在,文件名通常为...
spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件...
《Spring配置文件模板详解》 在Java开发领域,Spring框架以其强大的依赖注入(Dependency Injection,简称DI)和面向切面编程(Aspect-Oriented Programming,简称AOP)能力,成为了企业级应用开发的重要选择。而...
Spring 配置文件详解 Spring 配置文件是指-guide Spring 工厂进行 Bean 生产、依赖关系注入(装配)及 Bean 实例分发的“图纸”。Java EE 程序员必须学会并灵活应用这份“图纸”准确地表达自己的“生产意图”。...