引入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"?>
<beans xmlns="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">
<!-- 配置bean的实例 -->
<bean id="greetingServiceBean"class="cn.csdn.hr.service.GreetingServiceBean">
<!-- propertybean中的属性值 -->
<property name="greeting">
<value>你好!</value>
</property>
</bean>
</beans>
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();
}
}