1.spring 体系结构
2.构建maven 项目
- 创建maven项目
- 选择模板
- 填写参数
- 项目结构
3. 编写helloworld 类
package hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestHello {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:Beans.xml");
HelloWorld helloworld = (HelloWorld) applicationContext.getBean("helloWorld");
helloworld.setMessage("hello world,my spring first program");
System.out.println(helloworld.getMessage());
}
}
4.配置bean
<?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="helloWorld" class="hello.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>
5.测试类
package hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestHello {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:Beans.xml");
HelloWorld helloworld = (HelloWorld) applicationContext.getBean("helloWorld");
helloworld.setMessage("hello world,my spring first program");
System.out.println(helloworld.getMessage());
}
}
6.结果
完!