Spring配制的一些细节:
1. 属性中特殊字符的处理
2. 配制bean之间的关联 --ref用法
3. 内部bean的配制
4. 级联属性赋值
5. null值的定义
6.配制list集合
7.Map集合的配制
8.配制Properties属性值
9.配置单例的集合bean,以供多个bean进行引用,要导入util命名空间
10.导入p命名空间
要用到的java类:
package com.spring.config; public class Manager { private String name; private String address; private double height; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } @Override public String toString() { return "Manager [name=" + name + ", address=" + address + ", height=" + height + "]"; } }
package com.spring.config; public class Department { private String name; private Manager manager; public String getName() { return name; } public void setName(String name) { this.name = name; } public Manager getManager() { return manager; } public void setManager(Manager person) { this.manager = person; } @Override public String toString() { return "Department [name=" + name + ", manager=" + manager + "]"; } }
package com.spring.config; import java.util.List; import java.util.Map; public class Company { private String name; private List<Department> departments; private Map<String,Department> mainDepartent; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Department> getDepartment() { return departments; } public void setDepartments(List<Department> departments) { this.departments = departments; } public Map<String, Department> getMainDepartent() { return mainDepartent; } public void setMainDepartent(Map<String, Department> mainDepartent) { this.mainDepartent = mainDepartent; } public List<Department> getDepartments() { return departments; } @Override public String toString() { return "Company [name=" + name + ", departments=" + departments + ", mainDepartent=" + mainDepartent + "]"; } }
package com.spring.config; import java.util.Properties; public class DataSource { private Properties properties; public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "DataSource [properties=" + properties + "]"; } }
package com.spring.config; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml"); // 1.属性中有特殊字符 Manager p = (Manager) ctx.getBean("manager"); System.out.println(p); // 2. bean之间的关系 Department de = (Department) ctx.getBean("department"); System.out.println(de); // 3.内部bean Department de1 = (Department) ctx.getBean("department1"); System.out.println(de1); // 4.级联属性 Department de2 = (Department) ctx.getBean("department2"); System.out.println(de2); // 5.null值的定义 Department de3 = (Department) ctx.getBean("department3"); System.out.println(de3); // 6.配制list集合 Company company = (Company) ctx.getBean("company"); System.out.println(company); // 7.Map集合的配制 Company company1 = (Company) ctx.getBean("company1"); System.out.println(company1); // 8.配制Properties属性值 DataSource dataSource = (DataSource) ctx.getBean("dataSource"); System.out.println(dataSource + "\t\n>>>" + dataSource.getProperties().get("pwd")); // 9.配置单例的集合bean,以供多个bean进行引用,要导入util命名空间 Company company2 = (Company) ctx.getBean("company2"); System.out.println(company2); // 10.导入p命名空间 Company company3 = (Company) ctx.getBean("company3"); System.out.println(company3); } }
spring-xml
<?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: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-4.2.xsd"> <!-- 1. 有特殊字符时,可以直接转义。 或者用value属性加<![CDATA[] 解决 --> <bean id="manager" class="com.spring.config.Manager"> <!-- <property name="address" value="China>SH"></property> --> <property name="address"> <value><![CDATA[China>SH]]></value> </property> <property name="name" value="matt"></property> <property name="height" value="175"></property> </bean> <bean id="department" class="com.spring.config.Department"> <!-- 2.ref可以引入指定的bean --> <property name="manager" ref="manager"></property> <property name="name" value="CDC"></property> </bean> <!--3. 内部bean --> <bean id="department1" class="com.spring.config.Department"> <property value="CNN" name="name"></property> <property name="manager"> <bean id="manager" class="com.spring.config.Manager"> <property name="address"> <value><![CDATA[China>HZ]]></value> </property> <property name="name" value="Rose"></property> <property name="height" value="185"></property> </bean> </property> </bean> <!-- 4.定义级联属性 --> <bean id="department2" class="com.spring.config.Department"> <property name="manager" ref="manager" /> <property name="name" value="CDC" /> <property name="manager.height" value="180" /> </bean> <!-- 5.null值的定义 --> <bean id="department3" class="com.spring.config.Department"> <property name="manager"> <null /> </property> <property name="name" value="CMC" /> </bean> <!-- 6.配置list集合 --> <bean id="company" class="com.spring.config.Company"> <property name="name" value="WG"></property> <property name="departments"> <list> <ref bean="department" /> <ref bean="department1" /> <ref bean="department2" /> </list> </property> <property name="mainDepartent"> <null /> </property> </bean> <!-- 7.配置Map集合 --> <bean id="company1" class="com.spring.config.Company"> <property name="name" value="WG1"></property> <property name="departments"> <null /> </property> <property name="mainDepartent"> <map> <entry key="AAA" value-ref="department" /> </map> </property> </bean> <!-- 8.配置Properties属性值 --> <bean id="dataSource" class="com.spring.config.DataSource"> <property name="properties"> <props> <prop key="user">root</prop> <prop key="pwd">******</prop> <prop key="jdbcUrl">jdbc:mysql:///mydata</prop> <prop key="driverClass">com.mysql.jdbc.Driver</prop> </props> </property> </bean> <!-- 9.配置单例的集合bean,以供多个bean进行引用,要导入util命名空间 --> <util:list id="departments"> <ref bean="department1" /> <ref bean="department2" /> </util:list> <bean id="company2" class="com.spring.config.Company"> <property name="name" value="WG1"></property> <property name="departments" ref="departments"></property> <property name="mainDepartent"> <map> <entry key="AAA" value-ref="department" /> </map> </property> </bean> <util:map id="mainDe"> <entry key="AAA" value-ref="department" /> </util:map> <!-- 10. 加入p命名空间,要导入p命名空间 --> <bean id="company3" class="com.spring.config.Company" p:name="WG3" p:departments-ref="departments" p:mainDepartent-ref="mainDe" /> </beans>