<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();
}
} |