spring IOC第一个案例

2014-11-24 07:45:51 · 作者: · 浏览: 1

引入spring两个核心jar文件 commons-logging-1.0.4.jar和spring.jar

bean类编写

public classGreetingServiceBean implements GreetingService{
private String greeting;

public void sayGreeting(){
System.out.println(greeting);
}
/*bean配置文件中property属性 name的名称与greeting*/
public String getGreeting() {
return greeting;
}

public voidsetGreeting(String greeting) {
this.greeting = greeting;
}
}

applicationContext.xml文件编写

< xmlversion="1.0" encoding="UTF-8" >
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-2.5.xsd">




你好!


AppMain.class测试类编写

public class AppMain {
@Test
public void Text(){
ClassPathResource resource = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
GreetingServiceBean serviceBean =(GreetingServiceBean)factory.getBean("greetingServiceBean");
serviceBean.sayGreeting();
}
}

摘自 扈亚楠