CommonUtils 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package com.xtuer.util;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.core.util.DefaultIndenter;import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;import com.fasterxml.jackson.databind.ObjectMapper;public class CommonUtils { private static DefaultPrettyPrinter printer; static { DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter(" " , DefaultIndenter.SYS_LF); printer = new DefaultPrettyPrinter(); printer.indentObjectsWith(indenter); printer.indentArraysWith(indenter); } public static void output (Object obj) { ObjectMapper mapper = new ObjectMapper(); try { System.out.println(mapper.writer(printer).writeValueAsString(obj)); } catch (JsonProcessingException e) { e.printStackTrace(); } } }
User 1 2 3 4 5 6 7 8 9 10 package com.xtuer.beans;public class User { private int id; private String username; private String password; private Address address; }
Address 1 2 3 4 5 6 7 8 9 10 package com.xtuer.beans;public class Address { private int id; private String country; private String province; private String street; }
spring-beans.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?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 ="address" class ="com.xtuer.beans.Address" > <property name ="country" value ="China" /> <property name ="province" value ="BeiJing" /> <property name ="street" value ="ChaoYang" /> </bean > <bean id ="user" class ="com.xtuer.beans.User" > <property name ="username" value ="Alice" /> <property name ="password" value ="Passw0rd" /> <property name ="address" ref ="address" /> </bean > </beans >
InjectionTest 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import com.xtuer.beans.User;import com.xtuer.util.CommonUtils;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class InjectionTest { private static ApplicationContext context; @BeforeClass public static void setup () { context = new ClassPathXmlApplicationContext("spring-beans.xml" ); } @Test public void test () { User user = context.getBean("user" , User.class); CommonUtils.output(user); } }
输出 1 2 3 4 5 6 7 8 9 10 11 { "id" : 0 , "username" : "Alice" , "password" : "Passw0rd" , "address" : { "id" : 0 , "country" : "China" , "province" : "BeiJing" , "street" : "ChaoYang" } }
说明
<property name="username" value="Alice"/>
调用 user.setUsername(“Alice”) 设置属性 username。
<property name="address" ref="address"/>
,找到标志为 address (可以是 id, name, alias) 的 Bean,然后调用 user.setAddress(address) 设置属性 address。
注入方式: attribute 和 element
value 和 ref 都可以使用 attribute 和 element 的方式注入,推荐尽量用 attribute 的方式,这样会简洁一些
value 用于简单类型: primitive type(int, boolean, float 等), String, Date, 还有注册了 PropertyEditor 的类型(把字符串转换成对象)。
ref 用于引用 Bean Configuration File 里定义的另一个 Bean
value 用 element 的方式如下注入: 1 2 3 4 5 6 7 <bean id ="user" class ="com.xtuer.beans.User" > <property name ="username" > <value > Alice</value > </property > <property name ="password" value ="Passw0rd" /> <property name ="address" ref ="address" /> </bean >
ref 用 element 的方式如下注入: 1 2 3 4 5 6 7 <bean id ="user" class ="com.xtuer.beans.User" > <property name ="username" value ="Alice" /> <property name ="password" value ="Passw0rd" /> <property name ="address" > <ref bean ="address" /> </property > </bean >
注入匿名对象 每创建一次 User,都会新创建一个 Address 的匿名对象。 使用 <bean>
,不能设置 id, name 等。
1 2 3 4 5 6 7 8 9 10 11 <bean id ="user" class ="com.xtuer.beans.User" > <property name ="username" value ="Alice" /> <property name ="password" value ="Passw0rd" /> <property name ="address" > <bean class ="com.xtuer.beans.Address" > <property name ="country" value ="Germany" /> <property name ="province" value ="Braunschweig" /> <property name ="street" value ="Wiesenstrasse" /> </bean > </property > </bean >