1.Helloworld类
public class Helloworld {
private String name;
public void setName(String name) {
this.name = name;
}
public void hello() {
System.out.println("hello: " + name);
}
public Helloworld() {
System.out.println("构造方法");
}
}
2.bean配置文件
<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.xsd">
<bean id="helloworld" class="com.hxzy.Helloworld" >
<!-- name应用于setName方法 -->
<property name="name" value="Spring test"> </property>
</bean>
</beans>
3.测试类
public class TestMain {
public static void main(String[] args) {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Helloworld helloworld = (Helloworld) ctx.getBean("helloworld");
helloworld.hello();
ctx.close();
}
}