标签:style blog http io ar color os 使用 sp
package cn.com.xf; public class User { private String name; private int age; private String remark; //省略属性的getter/setter方法
@Override public String toString() { return "User [name=" + name + ", age=" + age + ", remark=" + remark + "]"; } }
package cn.com.xf; public class UserUtil { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public boolean isUser() { if (user != null) { return true; } else { return false; } } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<!--注意,以下使用了P标签--> 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"> <bean id="user" class="cn.com.xf.User" p:name="jikoy" p:age="28" p:remark="this is remark"></bean> <bean id="userUtil" class="cn.com.xf.UserUtil"> <property name="user" ref="user"></property> </bean> </beans>
以下是个测试类:全部代码亲测通过。
package cn.com.xf; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) {
//ApplicationContext接口没有提供关闭方法,eclipse会提示,故改为它的子接口 ConfigurableApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); User user=(User) ctx.getBean("user"); ctx.close(); System.out.println(user); } }
Spring系列【2】应用setter注入法实现Bean的注入
标签:style blog http io ar color os 使用 sp
原文地址:http://www.cnblogs.com/jikoy/p/4162213.html