Spring学习笔记-注入实战篇

 

spring对各种数据类型都提供了注入支持,像java基本类型,对象,集合等,这篇文章以代码实践为主,代码注释中会解释注入的细节

测试类中包含了我们编程中最常见的数据结构

package com.crazycoder2010.spring.injection; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Student { private int age; private double weight; private String name; private boolean registed; private Properties properties; private Map<?, ?> attributes; private List<?> scores; private Set<?> sets; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isRegisted() { return registed; } public void setRegisted(boolean registed) { this.registed = registed; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public Map<?, ?> getAttributes() { return attributes; } public void setAttributes(Map<?, ?> attributes) { this.attributes = attributes; } public List<?> getScores() { return scores; } public void setScores(List<?> scores) { this.scores = scores; } public Set<?> getSets() { return sets; } public void setSets(Set<?> sets) { this.sets = sets; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("age=").append(this.age).append("/n").append("weight=") .append(this.weight).append("/n").append("name=") .append(this.name).append("/n").append("registed=") .append(this.registed).append("/n").append("properties={"); StringBuilder tmp = new StringBuilder(); for (Object key : this.properties.keySet()) { tmp.append(key).append("=").append(properties.get(key)).append(","); } builder.append(tmp).append("}/n"); builder.append("attributes={"); StringBuilder map = new StringBuilder(); for (Object key : attributes.keySet()) { map.append(key).append("=").append(attributes.get(key)).append(","); } builder.append(map).append("}/n"); builder.append("scores={"); for (Object obj : scores) { builder.append(obj).append(","); } builder.append("}/n"); builder.append("sets={"); for (Object obj : sets) { builder.append(obj).append(","); } builder.append("}/n"); return builder.toString(); } }  

看下在配置文件中如何对各种类型注入

<?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-2.5.xsd"> <bean id="student" class="com.crazycoder2010.spring.injection.Student"> <!-- 整形普通属性直接写入值即可 --> <property name="age" value="20"></property> <!-- 也可以搞个value标签 --> <property name="name"> <value>张三</value> </property> <!-- 布尔型 --> <property name="registed" value="true"></property> <!-- double类型 --> <property name="weight" value="142.5"></property> <!-- list类型注入 --> <property name="scores"> <list><!-- 这个地方是个list --> <value>90</value> <value>98</value> <value>87</value> </list> </property> <!-- 注入map --> <property name="attributes"> <map><!--这个地方是map,注意内嵌的标签是entry,是一个键值对 --> <entry key="a1"> <value>12345</value> </entry> <entry key="a2" value="hello-world"></entry> </map> </property> <!-- Properties注入 --> <property name="properties"> <props><!-- 雷同与map,但是注意prop标签只有key属性,因为value只能是String类型,prop对应的值直接写在标签内部 --> <prop key="p1">789</prop> <prop key="p2">567</prop> </props> </property> <!-- 集合注入 --> <property name="sets"> <set> <value>你好</value> <value>123</value> <null></null><!-- 这个地方我们注入一个null进去 --> </set> </property> </bean> </beans>  

打印输出

 

age=20

weight=142.5

name=张三

registed=true

properties={p2=567,p1=789,}

attributes={a1=12345,a2=hello-world,}

scores={90,98,87,}

sets={你好,123,null,}

 

附BaseSpring类

package com.crazycoder2010.spring; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BaseSpring { BeanFactory beanFactory = null; public Object getBean(String beanName) { init();//确保spring容器已经启动 return beanFactory.getBean(beanName); } private void init() { if (beanFactory == null) { beanFactory = new ClassPathXmlApplicationContext(configFiles());//这个地方 } } protected String[] configFiles() { //这里作了个约定,默认的spring文件都放在与当前类平行的目录下,名称为applicationContex.xml //如要测试的类为com.crazycoder2010.spring.injection.StudentTest //则该测试的spring配置文件路径为com/crazycoder2010/spring/injection/applicationContex.xml //子类可以复写这个方法类实现定制化需求 String folder = getClass().getPackage().getName() .replaceAll("//.", "/") + "/"; String file = "applicationContext.xml"; return new String[] { folder + file }; } }  

 

你可能感兴趣的:(spring,String,object,properties,Class,attributes)