User.java
package org.rockie;
public class User {
private String username;
private Home myhome;
pbulc User(Home myhome)
{
this.myhome=myhome;
}
public Home getMyhome() {
return myhome;
}
public void setMyhome(Home myhome) {
this.myhome = myhome;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
Home.java
package org.rockie;
public class Home {
private String homeaddr;
public String getHomeaddr() {
return homeaddr;
}
public void setHomeaddr(String homeaddr) {
this.homeaddr = homeaddr;
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="home" class="org.rockie.Home" abstract="false"
singleton="true" lazy-init="default" autowire="default"
dependency-check="default">
<property name="homeaddr">
<value>深圳..</value>
</property>
</bean>
<bean id="user" class="org.rockie.User" abstract="false"
singleton="true" lazy-init="default" autowire="constructor"
dependency-check="default">
<property name="username">
<value>rockie</value>
</property>
</bean>
</beans>
TestMain.java
package org.rockie;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
ApplicationContext context=new FileSystemXmlApplicationContext("org/rockie/applicationContext.xml");
User user=(User)context.getBean("user");
System.out.println(user.getUsername());
System.out.println(user.getMyhome().getHomeaddr());
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)