1.添加Spring支持
![](https://img-blog.csdn.net/20180611085600513)
2.编写HelloWorld类
package com.shw;
public class HelloWorld {
public void show(){
System.out.println("欢迎学习spring框架");
}
}
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-4.1.xsd">
<bean id="hello" class="com.shw.HelloWorld">
</bean>
</beans>
4.编写测试类
package com.shw;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//加载applicationContext.xml
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取实例
HelloWorld hello=(HelloWorld) context.getBean("hello");
//调用方法
hello.show();
}
}
运行效果:
![](https://img-blog.csdn.net/20180611085828961)