JavaWeb_(Spring框架)xml配置文件
系列博文
JavaWeb_(Spring框架)xml配置文件 传送门
JavaWeb_(Spring框架)注解配置 传送门
Xml配置
a)Bean元素:交由Spring管理的对象都要配置在bean标签中;
i.Bean标签介绍和创建方式:空参构造、静态工厂、动态工厂;
ii.Scope属性介绍:singleton、protoptype、request、session;
iii.初始化方法Init-method和 销毁方法destroy-method介绍;
b)属性注入:
i.Set方法注入;
ii.构造函数注入;
iii.复杂类型注入:Array、List、Set、Map、Properties
1、xml配置-bean标签-配置及创建方式
ApplicationContext 配置的所有bean都会在容器创建的时候被创建出来,
如果配置的bean较多,那么在创建容的时候,会产生内存过大的问题;这种情况在机器硬件性能较为落后的时候体现的比较明显;
延迟加载(懒加载) true就是创建容器时不加载配置的bean对象,在获取的时候才创建;
<!-- name 是起一个名字,我们可以通过这个name来利用容器获取对象
name 可以使用特殊字符
name 可以重复
我们在实际开发中不推荐将多个对象名字命名为重复的-->
<!-- id与name作用基本相同,单不推荐使用 不支持特殊字符,不能重复 --> <!-- class:是被管理对象的全包名,spring会通过这个包名来创建对象 -->
<bean name="user" class="com.Gary.bean.User" lazy-init ="true">
<!-- 为u_id注入了一个id为2的值 -->
<property name="u_id" value="2"></property> </bean>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <!-- name 是起一个名字,我们可以通过这个name来利用容器获取对象
name 可以使用特殊字符
name 可以重复
我们在实际开发中不推荐将多个对象名字命名为重复的-->
<!-- id与name作用基本相同,单不推荐使用 不支持特殊字符,不能重复 --> <!-- class:是被管理对象的全包名,spring会通过这个包名来创建对象 -->
<!-- request 在web环境下,如果scope属性为request 那么这个对象被创建出来 他的生命周期会与request请求一致-->
<!-- session 同理 ,生命周期与session一致 -->
<bean name="user" class="com.Gary.bean.User" lazy-init ="true">
<!-- 为u_id注入了一个id为2的值 -->
<property name="u_id" value="2"></property> </bean> </beans>
applicationContext.xml
2、xml配置-bean标签-scope属性
scope="singleton" 表示<bean>是单例的
<bean name="user" class="com.Gary.bean.User" lazy-init ="true" scope="singleton">
<!-- 为u_id注入了一个id为2的值 -->
<property name="u_id" value="2"></property>
</bean>
package com.Gary.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.Gary.bean.User; public class HelloSpring2 { //Spring是一个容器,它将帮助我们管理对象
@Test
public void Test2() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //scope="singleton" 默认值 单例的 只允许创建出一份 User u1 = (User) ac.getBean("user");
User u2 = (User) ac.getBean("user");
User u3 = (User) ac.getBean("user"); System.out.println(u1==u2); } }
HelloSpring2.java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <!-- name 是起一个名字,我们可以通过这个name来利用容器获取对象
name 可以使用特殊字符
name 可以重复
我们在实际开发中不推荐将多个对象名字命名为重复的-->
<!-- id与name作用基本相同,单不推荐使用 不支持特殊字符,不能重复 --> <!-- class:是被管理对象的全包名,spring会通过这个包名来创建对象 -->
<!-- request 在web环境下,如果scope属性为request 那么这个对象被创建出来 他的生命周期会与request请求一致-->
<!-- session 同理 ,生命周期与session一致 -->
<bean name="user" class="com.Gary.bean.User" lazy-init ="true" scope="singleton">
<!-- 为u_id注入了一个id为2的值 -->
<property name="u_id" value="2"></property> </bean> </beans>
applicationContext.xml
scope="prototype" 表示<bean>是多例的
<bean name="user" class="com.Gary.bean.User" lazy-init ="true" scope="prototype">
<!-- 为u_id注入了一个id为2的值 -->
<property name="u_id" value="2"></property>
</bean>
package com.Gary.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.Gary.bean.User; public class HelloSpring2 { //Spring是一个容器,它将帮助我们管理对象
@Test
public void Test2() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //scope="prototype" 多例的 允许创建出多份对象 User u1 = (User) ac.getBean("user");
User u2 = (User) ac.getBean("user");
User u3 = (User) ac.getBean("user"); System.out.println(u1==u2); } }
HelloSpring2.java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <!-- name 是起一个名字,我们可以通过这个name来利用容器获取对象
name 可以使用特殊字符
name 可以重复
我们在实际开发中不推荐将多个对象名字命名为重复的-->
<!-- id与name作用基本相同,单不推荐使用 不支持特殊字符,不能重复 --> <!-- class:是被管理对象的全包名,spring会通过这个包名来创建对象 -->
<!-- request 在web环境下,如果scope属性为request 那么这个对象被创建出来 他的生命周期会与request请求一致-->
<!-- session 同理 ,生命周期与session一致 -->
<bean name="user" class="com.Gary.bean.User" lazy-init ="true" scope="prototype">
<!-- 为u_id注入了一个id为2的值 -->
<property name="u_id" value="2"></property> </bean> </beans>
applicationContext.xml
scrope的其它两个属性request、session(基本用不到这两个属性)
一般情况下使用singleton单例的,特殊情况下使用prototype多例的(使用struts时,它创建的action是多例的)
<!-- request 在web环境下,如果scope属性为request 那么这个对象被创建出来 他的生命周期会与request请求一致-->
<!-- session 同理 ,生命周期与session一致 -->
<bean name="user" class="com.Gary.bean.User" lazy-init ="true" scope="prototype">
<!-- 为u_id注入了一个id为2的值 -->
<property name="u_id" value="2"></property> </bean>
3、xml配置-bean标签-init-method与destroy-method的使用
在User.java中添加一个userInit()初始化方法与userDestroy()销毁容器时的方法
public void userInit() {
System.out.println("user init");
} public void userDestroy() {
System.out.println("user destroy");
}
<bean name="user" class="com.Gary.bean.User" lazy-init ="true" init-method="userInit" destroy-method="userDestroy">
<!-- 为u_id注入了一个id为2的值 -->
<property name="u_id" value="2"></property> </bean>
package com.Gary.bean; public class User { private Integer u_id;
private String u_username;
private String u_password; /*public User() {
System.out.println("默认使用 User 对象空参构造方法");
}*/
public Integer getU_id() {
return u_id;
}
public void setU_id(Integer u_id) {
this.u_id = u_id;
}
public String getU_username() {
return u_username;
}
public void setU_username(String u_username) {
this.u_username = u_username;
}
public String getU_password() {
return u_password;
}
public void setU_password(String u_password) {
this.u_password = u_password;
}
@Override
public String toString() {
return "User [u_id=" + u_id + ", u_username=" + u_username + ", u_password=" + u_password + "]";
} public void userInit() {
System.out.println("user init");
} public void userDestroy() {
System.out.println("user destroy");
} }
User.java
package com.Gary.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.Gary.bean.User; public class HelloSpring2 { //Spring是一个容器,它将帮助我们管理对象
@Test
public void Test2() { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //scope="prototype" 多例的 允许创建出多份对象 User u1 = (User) ac.getBean("user");
User u2 = (User) ac.getBean("user");
User u3 = (User) ac.getBean("user"); System.out.println(u1==u2); //关闭容器激活destroy-method方法
ac.close(); } }
HelloSpring2.java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <!-- name 是起一个名字,我们可以通过这个name来利用容器获取对象
name 可以使用特殊字符
name 可以重复
我们在实际开发中不推荐将多个对象名字命名为重复的-->
<!-- id与name作用基本相同,单不推荐使用 不支持特殊字符,不能重复 --> <!-- class:是被管理对象的全包名,spring会通过这个包名来创建对象 -->
<!-- request 在web环境下,如果scope属性为request 那么这个对象被创建出来 他的生命周期会与request请求一致-->
<!-- session 同理 ,生命周期与session一致 -->
<bean name="user" class="com.Gary.bean.User" lazy-init ="true" init-method="userInit" destroy-method="userDestroy">
<!-- 为u_id注入了一个id为2的值 -->
<property name="u_id" value="2"></property> </bean> </beans>
applicationContext.xml
4、xml配置-属性注入-Set方式注入
注入一个基本类型
创建一个applicationContext_Injection.xml和Test_Injection.java
<!-- 将user对象交给Spring管理,并注入类型 -->
<bean name="user" class="com.Gary.bean.User">
<property name="u_id" value="1"/>
<property name="u_username" value="Gary"/>
<property name="u_password" value="123"/>
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <!-- 将user对象交给Spring管理,并注入类型 -->
<bean name="user" class="com.Gary.bean.User">
<property name="u_id" value="1"/>
<property name="u_username" value="Gary"/>
<property name="u_password" value="123"/>
</bean> </beans>
applicationContext_Injection.xml
package com.Gary.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.Gary.bean.User; public class Test_Injection { //Spring是一个容器,它将帮助我们管理对象
@Test
public void Test2() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_Injection.xml"); User u1 = (User) ac.getBean("user"); System.out.println(u1); } }
Test_Injection.java
package com.Gary.bean; public class User { private Integer u_id;
private String u_username;
private String u_password; /*public User() {
System.out.println("默认使用 User 对象空参构造方法");
}*/
public Integer getU_id() {
return u_id;
}
public void setU_id(Integer u_id) {
this.u_id = u_id;
}
public String getU_username() {
return u_username;
}
public void setU_username(String u_username) {
this.u_username = u_username;
}
public String getU_password() {
return u_password;
}
public void setU_password(String u_password) {
this.u_password = u_password;
}
@Override
public String toString() {
return "User [u_id=" + u_id + ", u_username=" + u_username + ", u_password=" + u_password + "]";
} public void userInit() {
System.out.println("user init");
} public void userDestroy() {
System.out.println("user destroy");
} }
User.java
注入引用类型
创建一个Pet.java宠物类
package com.Gary.bean; public class Pet { //宠物类型 猫 狗
private String petType;
//宠物颜色
private String color; @Override
public String toString() {
return "Pet [petType=" + petType + ", color=" + color + "]";
}
public String getPetType() {
return petType;
}
public void setPetType(String petType) {
this.petType = petType;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
} }
Pet.java
在User.java中加入宠物字段
private Integer u_id;
private String u_username;
private String u_password; //加入宠物字段
private Pet u_pet;
package com.Gary.bean; public class User { private Integer u_id;
private String u_username;
private String u_password; //加入宠物字段
private Pet u_pet; @Override
public String toString() {
return "User [u_id=" + u_id + ", u_username=" + u_username + ", u_password=" + u_password + ", u_pet=" + u_pet
+ "]";
}
public Pet getU_pet() {
return u_pet;
}
public void setU_pet(Pet u_pet) {
this.u_pet = u_pet;
}
/*public User() {
System.out.println("默认使用 User 对象空参构造方法");
}*/
public Integer getU_id() {
return u_id;
}
public void setU_id(Integer u_id) {
this.u_id = u_id;
}
public String getU_username() {
return u_username;
}
public void setU_username(String u_username) {
this.u_username = u_username;
}
public String getU_password() {
return u_password;
}
public void setU_password(String u_password) {
this.u_password = u_password;
} public void userInit() {
System.out.println("user init");
} public void userDestroy() {
System.out.println("user destroy");
} }
User.java
<!-- 将user对象交给Spring管理,并注入类型 -->
<bean name="user" class="com.Gary.bean.User">
<property name="u_id" value="1"/>
<property name="u_username" value="Gary"/>
<property name="u_password" value="123"/>
<!-- 引用类型的注入 -->
<property name="u_pet" ref="dog"/>
</bean> <!-- 注入引用类型 -->
<bean name="dog" class="com.Gary.bean.Pet">
<property name="petType" value="二哈"/>
<property name="color" value="灰白"/>
</bean>
package com.Gary.bean; public class User { private Integer u_id;
private String u_username;
private String u_password; //加入宠物字段
private Pet u_pet; @Override
public String toString() {
return "User [u_id=" + u_id + ", u_username=" + u_username + ", u_password=" + u_password + ", u_pet=" + u_pet
+ "]";
}
public Pet getU_pet() {
return u_pet;
}
public void setU_pet(Pet u_pet) {
this.u_pet = u_pet;
}
/*public User() {
System.out.println("默认使用 User 对象空参构造方法");
}*/
public Integer getU_id() {
return u_id;
}
public void setU_id(Integer u_id) {
this.u_id = u_id;
}
public String getU_username() {
return u_username;
}
public void setU_username(String u_username) {
this.u_username = u_username;
}
public String getU_password() {
return u_password;
}
public void setU_password(String u_password) {
this.u_password = u_password;
} public void userInit() {
System.out.println("user init");
} public void userDestroy() {
System.out.println("user destroy");
} }
User.java
package com.Gary.bean; public class Pet { //宠物类型 猫 狗
private String petType;
//宠物颜色
private String color; @Override
public String toString() {
return "Pet [petType=" + petType + ", color=" + color + "]";
}
public String getPetType() {
return petType;
}
public void setPetType(String petType) {
this.petType = petType;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
} }
Pet.java
package com.Gary.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.Gary.bean.User; public class Test_Injection { //Spring是一个容器,它将帮助我们管理对象
@Test
public void Test2() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_Injection.xml"); User u1 = (User) ac.getBean("user"); System.out.println(u1); } }
Test_Injection.java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <!-- 将user对象交给Spring管理,并注入类型 -->
<bean name="user" class="com.Gary.bean.User">
<property name="u_id" value="1"/>
<property name="u_username" value="Gary"/>
<property name="u_password" value="123"/>
<!-- 引用类型的注入 -->
<property name="u_pet" ref="dog"/>
</bean> <!-- 注入引用类型 -->
<bean name="dog" class="com.Gary.bean.Pet">
<property name="petType" value="二哈"/>
<property name="color" value="灰白"/>
</bean> </beans>
applicationContext.xml
5、xml配置-属性注入-构造函数注入
在User.java中创建构造函数(一定要带空参构造方法)
public User(String u_username, Pet u_pet) {
System.out.println("方法1 String,Pet");
this.u_username = u_username;
this.u_pet = u_pet;
}
package com.Gary.bean; public class User { private Integer u_id;
private String u_username;
private String u_password; public User(String u_username, Pet u_pet) {
System.out.println("方法1 String,Pet");
this.u_username = u_username;
this.u_pet = u_pet;
} //加入宠物字段
private Pet u_pet; @Override
public String toString() {
return "User [u_id=" + u_id + ", u_username=" + u_username + ", u_password=" + u_password + ", u_pet=" + u_pet
+ "]";
}
public Pet getU_pet() {
return u_pet;
}
public void setU_pet(Pet u_pet) {
this.u_pet = u_pet;
}
/*public User() {
System.out.println("默认使用 User 对象空参构造方法");
}*/
public Integer getU_id() {
return u_id;
}
public void setU_id(Integer u_id) {
this.u_id = u_id;
}
public String getU_username() {
return u_username;
}
public void setU_username(String u_username) {
this.u_username = u_username;
}
public String getU_password() {
return u_password;
}
public void setU_password(String u_password) {
this.u_password = u_password;
} public void userInit() {
System.out.println("user init");
} public void userDestroy() {
System.out.println("user destroy");
} }
User.java
在applicationContext_Injection.xml中使用构造方法注入配置<bean>元素
<!-- 构造方法注入 -->
<bean name="user1" class="com.Gary.bean.User">
<constructor-arg name="u_username" value="Gary2"/>
<constructor-arg name="u_pet" ref="dog"/>
</bean>
package com.Gary.bean; public class User { private Integer u_id;
private String u_username;
private String u_password; public User(String u_username, Pet u_pet) {
System.out.println("方法1 String,Pet");
this.u_username = u_username;
this.u_pet = u_pet;
} //加入宠物字段
private Pet u_pet; @Override
public String toString() {
return "User [u_id=" + u_id + ", u_username=" + u_username + ", u_password=" + u_password + ", u_pet=" + u_pet
+ "]";
}
public Pet getU_pet() {
return u_pet;
}
public void setU_pet(Pet u_pet) {
this.u_pet = u_pet;
} public User() {
System.out.println("默认使用 User 对象空参构造方法");
} public Integer getU_id() {
return u_id;
}
public void setU_id(Integer u_id) {
this.u_id = u_id;
}
public String getU_username() {
return u_username;
}
public void setU_username(String u_username) {
this.u_username = u_username;
}
public String getU_password() {
return u_password;
}
public void setU_password(String u_password) {
this.u_password = u_password;
} }
User.java
package com.Gary.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.Gary.bean.User; public class Test_Injection { //Spring是一个容器,它将帮助我们管理对象
@Test
public void Test2() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_Injection.xml"); User u1 = (User) ac.getBean("user1"); System.out.println(u1); } }
Test_Injection.java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <!-- 将user对象交给Spring管理,并注入类型 -->
<bean name="user" class="com.Gary.bean.User">
<property name="u_id" value="1"/>
<property name="u_username" value="Gary"/>
<property name="u_password" value="123"/>
<!-- 引用类型的注入 -->
<property name="u_pet" ref="dog"/>
</bean> <!-- 注入引用类型 -->
<bean name="dog" class="com.Gary.bean.Pet">
<property name="petType" value="二哈"/>
<property name="color" value="灰白"/>
</bean> <!-- 构造方法注入 -->
<bean name="user1" class="com.Gary.bean.User">
<constructor-arg name="u_username" value="Gary2"/>
<constructor-arg name="u_pet" ref="dog"/>
</bean> </beans>
applicationContext_Injection.xml
如果有多个构造方法,可以使用type:指定参数的类型,index :指定参数
6、xml配置-属性注入-复杂类型注入Array、List、Set、Map、Properties
在MyCollection.java中定义几个复杂类型变量
//数组
private Object[] array; //list
private List list; //set
private Set set; //map
private Map map; //properties
private Properties properties;
<!-- 复杂类型注入 -->
<bean name="collection" class="com.Gary.bean.MyCollection">
<!-- array -->
<property name="array">
<array>
<value>123</value>
<value>abc</value>
</array>
</property>
</bean>
package com.Gary.bean; import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; public class MyCollection {
//数组
private Object[] array; //list
private List list; //set
private Set set; //map
private Map map; //properties
private Properties properties; public Object[] getArray() {
return array;
} public void setArray(Object[] array) {
this.array = array;
} public List getList() {
return list;
} public void setList(List list) {
this.list = list;
} public Set getSet() {
return set;
} public void setSet(Set set) {
this.set = set;
} public Map getMap() {
return map;
} public void setMap(Map map) {
this.map = map;
} public Properties getProperties() {
return properties;
} public void setProperties(Properties properties) {
this.properties = properties;
} @Override
public String toString() {
return "MyCollection [array=" + Arrays.toString(array) + ", list=" + list + ", set=" + set + "]";
} }
MyCollection.java
package com.Gary.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.Gary.bean.MyCollection;
import com.Gary.bean.User; public class Test_Injection { //Spring是一个容器,它将帮助我们管理对象
@Test
public void Test2() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_Injection.xml"); MyCollection mc = (MyCollection) ac.getBean("collection"); System.out.println(mc); } }
Test_Injection.java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <!-- 将user对象交给Spring管理,并注入类型 -->
<bean name="user" class="com.Gary.bean.User">
<property name="u_id" value="1"/>
<property name="u_username" value="Gary" />
<property name="u_password" value="123"/>
<!-- 引用类型的注入 -->
<property name="u_pet" ref="dog"/>
</bean> <!-- 注入引用类型 -->
<bean name="dog" class="com.Gary.bean.Pet">
<property name="petType" value="二哈"/>
<property name="color" value="灰白"/>
</bean> <!-- 构造方法注入 -->
<bean name="user1" class="com.Gary.bean.User">
<constructor-arg name="u_username" value="Gary2" type=""/>
<constructor-arg name="u_pet" ref="dog"/>
</bean> <!-- 复杂类型注入 -->
<bean name="collection" class="com.Gary.bean.MyCollection">
<!-- array -->
<property name="array">
<array>
<value>123</value>
<value>abc</value>
</array>
</property>
</bean> </beans>
applicationContext_Injection.xml
同理剩下几个复杂类型配置
<!-- 复杂类型注入 -->
<bean name="collection" class="com.Gary.bean.MyCollection">
<!-- array -->
<property name="array">
<array>
<value>123</value>
<value>abc</value>
<ref bean="dog"/>
</array>
</property> <!-- list -->
<property name="list">
<list>
<value>456</value>
<value>cba</value>
<ref bean="user1"/>
</list>
</property> <!--set -->
<property name="set">
<set>
<value>111</value>
<value>aaa</value>
<ref bean="user1"/>
</set>
</property> <!-- map -->
<property name="map">
<map>
<entry key="username" value="root"/>
<entry key="password" value="123"/>
<entry key-ref="user1" value-ref="dog"/>
</map>
</property> <!-- properties -->
<property name="prop">
<props>
<prop key="name">老李</prop>
<prop key="age">25</prop>
</props>
</property> </bean>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <!-- 将user对象交给Spring管理,并注入类型 -->
<bean name="user" class="com.Gary.bean.User">
<property name="u_id" value="1"/>
<property name="u_username" value="Gary" />
<property name="u_password" value="123"/>
<!-- 引用类型的注入 -->
<property name="u_pet" ref="dog"/>
</bean> <!-- 注入引用类型 -->
<bean name="dog" class="com.Gary.bean.Pet">
<property name="petType" value="二哈"/>
<property name="color" value="灰白"/>
</bean> <!-- 构造方法注入 -->
<bean name="user1" class="com.Gary.bean.User">
<constructor-arg name="u_username" value="Gary2" type=""/>
<constructor-arg name="u_pet" ref="dog"/>
</bean> <!-- 复杂类型注入 -->
<bean name="collection" class="com.Gary.bean.MyCollection">
<!-- array -->
<property name="array">
<array>
<value>123</value>
<value>abc</value>
<ref bean="dog"/>
</array>
</property> <!-- list -->
<property name="list">
<list>
<value>456</value>
<value>cba</value>
<ref bean="user1"/>
</list>
</property> <!--set -->
<property name="set">
<set>
<value>111</value>
<value>aaa</value>
<ref bean="user1"/>
</set>
</property> <!-- map -->
<property name="map">
<map>
<entry key="username" value="root"/>
<entry key="password" value="123"/>
<entry key-ref="user1" value-ref="dog"/>
</map>
</property> <!-- properties -->
<property name="prop">
<props>
<prop key="name">老李</prop>
<prop key="age">25</prop>
</props>
</property> </bean> </beans>
applicationContext_Injection.xml
JavaWeb_(Spring框架)xml配置文件的更多相关文章
- Spring框架xml配置文件 复杂类型属性注入——数组 list map properties DI dependency injection 依赖注入——属性值的注入依赖于建立的对象(堆空间)
Person类中的各种属性写法如下: package com.swift.person; import java.util.Arrays; import java.util.List; import ...
- JavaWeb_(Spring框架)注解配置
系列博文 JavaWeb_(Spring框架)xml配置文件 传送门 JavaWeb_(Spring框架)注解配置 传送门 Spring注解配置 a)导包和约束:基本包.aop包+context约束 ...
- JavaWeb_(Mybatis框架)主配置文件介绍_四
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- JavaWeb_(Spring框架)Spring整合Hibernate
Dao层类要继承HibernateDaoSupport.java父类 原先使用Hibernate框架hibernate.cfg.xml配置数据库 <hibernate-configuration ...
- Spring 通过XML配置文件以及通过注解形式来AOP 来实现前置,环绕,异常通知,返回后通知,后通知
本节主要内容: 一.Spring 通过XML配置文件形式来AOP 来实现前置,环绕,异常通知 1. Spring AOP 前置通知 XML配置使用案例 2. Spring AOP ...
- (转)在编写Spring框架的配置文件时,标签无提示符的解决办法
http://blog.csdn.net/yerenyuan_pku/article/details/52831618 问题描述 初学者在学习Spring框架的过程中,大概会碰到这样一个问题:在编写S ...
- Spring框架的配置文件
Spring框架的配置文件 (2014-12-18 20:43:42) 转载▼ 标签: 配置文件 例子 构造函数 成员 spring 分类: 专业知识 (注:文中的"<"均需 ...
- [error] eclipse编写spring等xml配置文件时只有部分提示,tx无提示
eclipse编写spring等xml配置文件时只有<bean>.<context>等有提示,其他标签都没有提示 这时就需要做以下两步操作(下面以事务管理标签为例) 1,添加命 ...
- Spring根据XML配置文件注入对象类型属性
这里有dao.service和Servlet三个地方 通过配过文件xml生成对象,并注入对象类型的属性,降低耦合 dao文件代码: package com.swift; public class Da ...
随机推荐
- SQL Server系统函数:日期函数
原文:SQL Server系统函数:日期函数 1.返回当前日期和时间 select GETDATE() '当前日期-精确到33毫秒' select GETUTCDATE() 'UTC日期和时间-精确到 ...
- Abp 领域事件简单实践 <四> 聚合根的领域事件
聚合根有个 DomainEvents 属性. 首先聚合根是一个实体.这个实体的仓储有变化(增删改)的时候,会触发这个DomainEvents 里的事件.就像EventBus.Trigger一样. pu ...
- 如何禁止Chrome浏览器隐藏URL的WWW前缀
如何禁止Chrome浏览器隐藏URL的WWW前缀 一.打开Chrome浏览器 二.在Chrome浏览器的地址栏中输入以下内容并回车: chrome://flags/#omnibox-ui-hide-s ...
- Keras 训练 inceptionV3 并移植到OpenCV4.0 in C++
1. 训练 # --coding:utf--- import os import sys import glob import argparse import matplotlib.pyplot as ...
- 在网页中添加google搜索
网页中插入谷歌搜索,至于怎么上谷歌,后面有时间会更,推荐百度 <form method="GET" action="http://www.google.com.hk ...
- 1.Java集合-HashMap实现原理及源码分析
哈希表(Hash Table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表,而HashMap的实现原理也常常 ...
- [LeetCode] 328. Odd Even Linked List ☆☆☆(奇偶节点分别放一起)
每天一算:Odd Even Linked List 描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝 ...
- vscode-golang跳转定义无效问题
问题发现: 本来可以进行"ctrl"+点击或者F12,进行跳转定义的方式,突然失效了 问题分析: 1.辅助工具是否关闭 解决方案,进入首选项,设置,查找go.docsTool,选项 ...
- ELK6.x_Kafka 安装配置文档
1. 环境描述 1.1. 环境拓扑 如上图所示:Kafka为3节点集群负责提供消息队列,ES为3节点集群.日志通过logstash或者filebeat传送至Kafka集群,再通过logstash发 ...
- 13_sqoop数据迁移概述
3. sqoop数据迁移 3.1 概述 sqoop是apache旗下一款“Hadoop体系和关系数据库服务器之间传送数据”的工具. 导入数据:MySQL,Oracle导入数据到Hadoop的HDFS. ...