一个简单的 spring 项目,HellWorld
1、创建 Java project 工程,名称:HelloWorld
2、向项目中导入spring核心包
3、创建 HelloWorld.java 类
package com.java.test;
public class HelloWorld {
public void say(){
System.out.println("hello world!");
}
}
4、将 HelloWorld 类引入 spring 的配置文件 beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloWorld" class="com.java.test.HelloWorld"></bean>
</beans>
5、创建 Test 测试类
package com.java.service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java.test.HelloWorld;
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
HelloWorld helloWorld=(HelloWorld)ac.getBean("helloWorld");
helloWorld.say();
}
}
到这里就完成了一个简单的 spring 项目啦~~