1、创建一个新的项目,并添加Spring框架
2、创建HelloWorld.java
package com.broadtext.beans.helloworld; public class HelloWorld {
private String name; public HelloWorld() {
} @Override
public String toString() {
return "HelloWorld [name=" + name + "]";
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
3、修改applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="helloWorld" class="com.broadtext.beans.helloworld.HelloWorld">
<property name="name" value="hjj"></property>
</bean> </beans>
这时候HelloWorld.java文件上会多出'S'标记
4、添加Main.java和Main方法测试HelloWorld bean
package com.broadtext.beans.helloworld; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
System.out.println(helloWorld);
} }
5、运行,看看控制台打印的HelloWorld