一、 Bean的基本配置
① 可使用<value>250</value>的方式注入,此时不需要框架进行类型强转
② 基本类型及其封装类型和String等类型,都可以采用字面值注入
③ 若字面值中含有特殊字符,可以使用<![CDATA]>把字面值包裹起来
④ 构造方法的调用,对于重载仅变量顺序不同的构造器应当增加index参数来进行区分
<!-- 使用构造器2 -->
<bean id="specialWord" class="com.spring.hellowrod.HelloWord">
<constructor-arg index="1" name="name" type="java.lang.String">
<!-- 使用特殊字符即xml中的特殊字符初始化使用方式 -->
<value><![CDATA[<Spring>]]></value>
</constructor-arg>
<constructor-arg index="0" name="age" value="-27" type="int"/>
</bean>
⑤ 引用关系,通过ref元素或者属性,也可以在属性或构造器里包含Bean声明
<!-- 包含关系 car对象指向car2 全局的car2 -->
<bean id="person" class="com.spring.hellowrod.Person">
<property name="name" value="Spring"/>
<property name="age" value="26"/>
<property name="car" ref="car2"/>
</bean>
<bean id="car2" class="com.spring.hellowrod.Car">
<property name="type" >
<value><![CDATA[<吉普>]]]></value>
</property>
<property name="yeas" value="2"/>
</bean>
⑥ 为引用的对象设置null值,级联属性赋值
<!-- 为对象设置 null值 -->
<bean id="personNew" class="com.spring.hellowrod.Person">
<constructor-arg name="name" value="nullTest"/>
<constructor-arg name="age" value="2"/>
<constructor-arg name="car"><null/></constructor-arg>
</bean>
<!-- 修改一个引用的全局 级联属性赋值 Bean来初始化引用的对象
原Bean不会被修改 不能直接 car. 必须先初始化后才能被级联属性赋值,
否则报错 null 多数时候用不着 -->
<bean id="person0" class="com.spring.hellowrod.Person">
<constructor-arg name="name" value="person0"/>
<constructor-arg name="age" value="23"/>
<constructor-arg name="car" ref="car2"/>
<!-- 修改级联属性的值 -->
<property name="car.type" value="长城"/>
<property name="car.yeas" value="1"/>
</bean>
⑦ 内部bean,即为一个复杂的对象属性赋值时可以使用匿名的内部bean对其进行初始化
二、集合属性及工具集合属性以及命名空间
2.1 List 同时使用内部Bean和引用ref
<!-- 配置集合属性 list -->
<bean id="personList" class="com.spring.container.Person">
<constructor-arg name="name" value="高小明"/>
<constructor-arg name="age" value="25"/>
<property name="carList">
<list>
<ref bean="car2"/>
<bean class="com.spring.hellowrod.Car">
<constructor-arg name="type" value="丰田"/>
<constructor-arg name="yeas" value="1"/>
</bean>
<bean class="com.spring.hellowrod.Car">
<constructor-arg name="type" value="大众"/>
<constructor-arg name="yeas" value="2"/>
</bean>
</list>
</property>
</bean>
2.2 Set同时使用内部Bean和引用ref
<!-- 配置集合属性 Set -->
<bean id="personSet" class="com.spring.container.Person">
<constructor-arg name="name" value="高小明"/>
<constructor-arg name="age" value="1"/>
<property name="carSet">
<set>
<ref bean="car2"/>
<ref bean="car2"/>
<ref bean="car2"/>
<bean class="com.spring.hellowrod.Car">
<constructor-arg name="type" value="丰田"/>
<constructor-arg name="yeas" value="1"/>
</bean>
<bean class="com.spring.hellowrod.Car">
<constructor-arg name="type" value="大众"/>
<constructor-arg name="yeas" value="2"/>
</bean>
</set>
</property>
</bean>
2.3 Map同时使用内部Bean和引用ref
<!-- 配置集合属性 map -->
<bean id="personMap" class="com.spring.container.Person">
<constructor-arg name="name" value="Gxm"/>
<constructor-arg name="age" value="1"/>
<property name="carIntegerMap">
<map>
<entry key-ref="car2" value="1"/>
<entry value="2">
<key>
<bean class="com.spring.hellowrod.Car">
<constructor-arg name="type" value="福特"/>
<constructor-arg name="yeas" value="1"/>
</bean>
</key>
</entry>
</map>
</property>
</bean>
2.4 工具集合
<!-- 配置单里的集合bean 供多个 bean使用 此处限定泛型类型为 com.spring.hellowrod.Car 不限定类型则直接忽略不写-->
<util:list id="carList" list-class="java.util.ArrayList" value-type="com.spring.hellowrod.Car">
<ref bean="car2"/>
<bean class="com.spring.hellowrod.Car">
<constructor-arg name="type" value="红旗"/>
<constructor-arg name="yeas" value="1"/>
</bean>
</util:list>
<!-- 配置工具类set 同上,简单的内置属性可以缩写 -->
<util:set id="carSet" set-class="java.util.HashSet" value-type="com.spring.hellowrod.Car">
<ref bean="car2"/>
<bean class="com.spring.hellowrod.Car">
<constructor-arg name="type" value="摩托"/>
<constructor-arg name="yeas" value="2"/>
</bean>
</util:set>
<!-- 配置工具类map 同上复杂类型,可以再次使用内置bean -->
<util:map id="carMap" map-class="java.util.HashMap" key-type="com.spring.hellowrod.Car"
value-type="java.lang.String">
<entry key-ref="car2" value=""/>
<entry value="23">
<key>
<bean class="com.spring.hellowrod.Car">
<property name="type" value="丰田"/>
<property name="yeas" value="1"/>
</bean>
</key>
</entry>
</util:map>
<!-- 配置工具类属性 -->
<util:properties id="propUtil">
<prop key="user">root</prop>
<prop key="password">123456789</prop>
<prop key="address">188.131.165.252:3306</prop>
<prop key="driver">com.jdbc.mysql.Driver</prop>
</util:properties>
2.5 p命名空间
需要先导入schema配置:
xmlns:p="http://www.springframework.org/schema/p"
在localtion中添加路径和解析文件路径
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
<!-- 通过p命名空间为属性命名 简化上述配置过程 -->
<bean id="pNameSpace" class="com.spring.container.Person" p:name="gxm" p:age="14"/>
三、完整的maven依赖配置及代码以及xml属性文件
3.1 maven依赖配置
同入门配置一致
3.2 applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
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 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
">
<!-- 配置bean 属性注入,实际需要调用无参构造函数得到对象,然后使用set方法 -->
<bean id="helloWord1" class="com.spring.hellowrod.HelloWord">
<property name="name" value="Spring"/>
<property name="age" value="25"/>
</bean>
<!-- 配置bean 构造方法注入,直接调用 对应的构造方法获得对象 -->
<bean id="helloWord2" class="com.spring.hellowrod.HelloWord">
<!-- ① 指定参数位置和类型确定调用的构造方法 ② 使用构造参数的命名确定调用的构造方法 注意同名但顺序不同的构造方法默认调用最后一个 -->
<!--constructor-arg value="gxm" type="java.lang.String"/>
<constructor-arg value="123" type="int"/-->
<constructor-arg index="0" name="name" value="Spring"/>
<constructor-arg index="1" name="age" value="125"/>
</bean>
<!-- 使用构造器2 -->
<bean id="specialWord" class="com.spring.hellowrod.HelloWord">
<constructor-arg index="1" name="name" type="java.lang.String">
<!-- 使用特殊字符即xml中的特殊字符初始化使用方式 -->
<value><![CDATA[<Spring>]]></value>
</constructor-arg>
<constructor-arg index="0" name="age" value="-27" type="int"/>
</bean>
<!-- 包含关系 car对象指向car2 -->
<bean id="person" class="com.spring.hellowrod.Person">
<property name="name" value="Spring"/>
<property name="age" value="26"/>
<property name="car" ref="car2"/>
</bean>
<bean id="car2" class="com.spring.hellowrod.Car">
<property name="type">
<value><![CDATA[<吉普>]]]></value>
</property>
<property name="yeas" value="2"/>
</bean>
<!-- 为对象设置 null值 -->
<bean id="personNew" class="com.spring.hellowrod.Person">
<constructor-arg name="name" value="nullTest"/>
<constructor-arg name="age" value="2"/>
<constructor-arg name="car">
<null/>
</constructor-arg>
</bean>
<!-- 修改一个引用的全局 级联属性赋值 Bean来初始化引用的对象
原Bean不会被修改 不能直接 car. 必须先初始化后才能被级联属性赋值,
否则报错 null 多数时候用不着 -->
<bean id="person0" class="com.spring.hellowrod.Person">
<constructor-arg name="name" value="person0"/>
<constructor-arg name="age" value="23"/>
<constructor-arg name="car" ref="car2"/>
<!-- 修改级联属性的值 -->
<property name="car.type" value="长城"/>
<property name="car.yeas" value="1"/>
</bean>
<!-- 配置集合属性 list -->
<bean id="personList" class="com.spring.container.Person">
<constructor-arg name="name" value="高小明"/>
<constructor-arg name="age" value="25"/>
<property name="carList">
<list>
<ref bean="car2"/>
<bean class="com.spring.hellowrod.Car">
<constructor-arg name="type" value="丰田"/>
<constructor-arg name="yeas" value="1"/>
</bean>
<bean class="com.spring.hellowrod.Car">
<constructor-arg name="type" value="大众"/>
<constructor-arg name="yeas" value="2"/>
</bean>
</list>
</property>
</bean>
<!-- 配置集合属性 Set -->
<bean id="personSet" class="com.spring.container.Person">
<constructor-arg name="name" value="高小明"/>
<constructor-arg name="age" value="1"/>
<property name="carSet">
<set>
<ref bean="car2"/>
<ref bean="car2"/>
<ref bean="car2"/>
<bean class="com.spring.hellowrod.Car">
<constructor-arg name="type" value="丰田"/>
<constructor-arg name="yeas" value="1"/>
</bean>
<bean class="com.spring.hellowrod.Car">
<constructor-arg name="type" value="大众"/>
<constructor-arg name="yeas" value="2"/>
</bean>
</set>
</property>
</bean>
<!-- 配置集合属性 map -->
<bean id="personMap" class="com.spring.container.Person">
<constructor-arg name="name" value="Gxm"/>
<constructor-arg name="age" value="1"/>
<property name="carIntegerMap">
<map>
<entry key-ref="car2" value="1"/>
<entry value="2">
<key>
<bean class="com.spring.hellowrod.Car">
<constructor-arg name="type" value="福特"/>
<constructor-arg name="yeas" value="1"/>
</bean>
</key>
</entry>
</map>
</property>
</bean>
<!-- 配置 properties 属性值 -->
<bean id="prop" class="com.spring.container.Prop">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">123456789</prop>
<prop key="jdbcUrl">jdbc:mysql</prop>
<prop key="driverClass">com.jdbc.Driver</prop>
</props>
</property>
</bean>
<!-- 配置单里的集合bean 供多个 bean使用 此处限定泛型类型为 com.spring.hellowrod.Car 不限定类型则直接忽略不写-->
<util:list id="carList" list-class="java.util.ArrayList" value-type="com.spring.hellowrod.Car">
<ref bean="car2"/>
<bean class="com.spring.hellowrod.Car">
<constructor-arg name="type" value="红旗"/>
<constructor-arg name="yeas" value="1"/>
</bean>
</util:list>
<!-- 配置工具类set 同上,简单的内置属性可以缩写 -->
<util:set id="carSet" set-class="java.util.HashSet" value-type="com.spring.hellowrod.Car">
<ref bean="car2"/>
<bean class="com.spring.hellowrod.Car">
<constructor-arg name="type" value="摩托"/>
<constructor-arg name="yeas" value="2"/>
</bean>
</util:set>
<!-- 配置工具类map 同上复杂类型,可以再次使用内置bean -->
<util:map id="carMap" map-class="java.util.HashMap" key-type="com.spring.hellowrod.Car"
value-type="java.lang.String">
<entry key-ref="car2" value=""/>
<entry value="23">
<key>
<bean class="com.spring.hellowrod.Car">
<property name="type" value="丰田"/>
<property name="yeas" value="1"/>
</bean>
</key>
</entry>
</util:map>
<!-- 配置工具类属性 -->
<util:properties id="propUtil">
<prop key="user">root</prop>
<prop key="password">123456789</prop>
<prop key="address">188.131.165.252:3306</prop>
<prop key="driver">com.jdbc.mysql.Driver</prop>
</util:properties>
<!-- 通过p命名空间为属性命名 简化上述配置过程 -->
<bean id="pNameSpace" class="com.spring.container.Person" p:name="gxm" p:age="14"/>
</beans>
3.3 测试中用到的所有类
3.3.1 项目目录结构
3.3.2 com.spring.container.Person 类
/**
* Created with IntelliJ IDEA.
* User: gxm
* Date: 2020/4/8
* Time: 11:06
* To change this template use File | Settings | File Templates.
* Description:
**/
package com.spring.container;
import com.spring.hellowrod.Car;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* @author gxm
*/
public class Person {
private String name;
private int age;
private List<Car> carList;
private Set<Car> carSet;
private Map<Car ,Integer> carIntegerMap;
public Person() {
}
public Set<Car> getCarSet() {
return carSet;
}
public Map<Car, Integer> getCarIntegerMap() {
return carIntegerMap;
}
public void setCarIntegerMap(Map<Car, Integer> carIntegerMap) {
this.carIntegerMap = carIntegerMap;
}
public void setCarSet(Set<Car> carSet) {
this.carSet = carSet;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Car> getCarList() {
return carList;
}
public void setCarList(List<Car> carList) {
this.carList = carList;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", carList=" + carList +
", carSet=" + carSet +
", carIntegerMap=" + carIntegerMap +
'}';
}
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Person personList = applicationContext.getBean("personList", Person.class);
System.out.println("list test");
System.out.println(personList);
personList = applicationContext.getBean("personSet", Person.class);
System.out.println("set test");
System.out.println(personList);
personList = applicationContext.getBean("personMap", Person.class);
System.out.println("map test");
System.out.println(personList);
System.out.println("Prop test");
Prop prop = applicationContext.getBean("prop",Prop.class);
System.out.println(prop);
System.out.println("工具类 carList");
ArrayList carList = applicationContext.getBean("carList", ArrayList.class);
System.out.println(carList);
System.out.println("工具类 carSet");
HashSet carSet = applicationContext.getBean("carSet", HashSet.class);
System.out.println(carSet);
}
}
3.3.3 com.spring.container.Prop 类
/**
* Created with IntelliJ IDEA.
* User: gxm
* Date: 2020/4/8
* Time: 21:05
* To change this template use File | Settings | File Templates.
* Description:
**/
package com.spring.container;
import java.util.Properties;
public class Prop {
private Properties properties;
public Prop() {
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "Prop{" +
"properties=" + properties +
'}';
}
}
3.3.4 com.spring.HelloWorld.Car类
/**
* Created with IntelliJ IDEA.
* User: gxm
* Date: 2020/4/8
* Time: 10:11
* To change this template use File | Settings | File Templates.
* Description:
**/
package com.spring.hellowrod;
import java.util.Objects;
/**
* @author gxm
*/
public class Car {
private String type;
private int yeas;
public Car(String type, int yeas) {
this.type = type;
this.yeas = yeas;
}
public Car() {
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getYeas() {
return yeas;
}
public void setYeas(int yeas) {
this.yeas = yeas;
}
@Override
public String toString() {
return "Car{" +
"type='" + type + '\'' +
", yeas=" + yeas +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Car)) {
return false;
}
Car car = (Car) o;
return getYeas() == car.getYeas() &&
getType().equals(car.getType());
}
@Override
public int hashCode() {
return Objects.hash(getType(), getYeas());
}
}
3.3.5 com.spring.HelloWorld.HelloWorld 类
/**
* Created with IntelliJ IDEA.
* User: gxm
* Date: 2020/4/7
* Time: 17:09
* To change this template use File | Settings | File Templates.
* Description:
**/
package com.spring.hellowrod;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.logging.Logger;
/**
* @author gxm
*/
public class HelloWord {
private static final Logger logger = Logger.getLogger(HelloWord.class.getName());
private String name;
private int age;
public HelloWord(String name, int age) {
this.name = name;
this.age = age;
System.out.println("调用构造方法1");
}
public HelloWord(int age, String name) {
this.name = name;
this.age = age;
System.out.println("调用构造方法2");
}
// 提供无参构造方法,供属性注入使用
public HelloWord() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println("set name");
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
System.out.println("set age");
}
public void hello() {
System.out.println("hello " + name);
}
public static void main(String[] args) throws IllegalAccessException, InstantiationException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWord helloWord = applicationContext.getBean("specialWord", HelloWord.class);
helloWord.hello();
logger.info("test");
Person person = applicationContext.getBean("person0", Person.class);
System.out.println(person);
}
}
3.3.6 com.spring.hellowrod.Person 类
/**
* Created with IntelliJ IDEA.
* User: gxm
* Date: 2020/4/8
* Time: 10:12
* To change this template use File | Settings | File Templates.
* Description:
**/
package com.spring.hellowrod;
/**
* @author gxm
*/
public class Person {
private String name;
private int age;
private Car car;
public Person(String name, int age, Car car) {
this.name = name;
this.age = age;
this.car = car;
}
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", car=" + car +
'}';
}
}