1.创建java project,并导入所需jar包,Spring20个核心包加上一个commons-logger.jar包
2.编写Springbean类
public class HelloSpring {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
3.编写配置文件
<?xml version="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-3.0.xsd">
<bean id="hello" class="org.spring.test.HelloSpring">
<property name="message" value="成兮"></property>
</bean>
</beans>
主函数测试代码
public class TestMain {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloSpring hello = (HelloSpring) context.getBean("hello",HelloSpring.class);
System.out.println(hello.getMessage());
}
}
成功输出结果
成兮