<bean id="userService" class="cn.itcast.service.UserService">
<property name="name" value="张三"></property>
</bean> |
package cn.itcast.service;
/*
* 表明业务层组建
* */
public class UserService {
private String name;
/*
* 模拟保存操做
* */
public void save(){
System.out.println("保存用户..."+name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} |
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.service.UserService;
public class SpringTest {
//测试spring工厂
@Test
public void test(){
//将UserService对象的建立交给spring。直接从spring工厂中获取
//读取spring配置文件建立spring工厂
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
//从工厂中获取对象
UserService us = (UserService) ctx.getBean("userService");
us.save();
}
} |