记录一下第一次创建spring项目的步骤
项目创建
等待完成项目创建,创建完成的界面如下图
创建完成后,开始我们的第一个案例
HelloWorld案例实现
创建一个HelloWorld类
/**
* @author moyv
*/
public class HelloWorld {
public HelloWorld(){
System.out.println("helloworld");
}
}
然后再对spring-config.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">
<!-- id是后面用来调用class文件的id-->
<bean id="HelloWorld" class="HelloWorld"/>
</beans>
写一个测试类调用HelloWorld
/**
* @author moyv
*/
public class Test {
public static void main(String[] args) {
//读取配置文件,加载里面的配置等
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml");
//通过getBean得到对象,其中HelloWorld就是我们刚刚在application.xml中配置的<bean/>中的id
HelloWorld helloWorld= (HelloWorld) applicationContext.getBean("HelloWorld");
}
}
最后运行一下测试类,可以看到控制台上打印了helloworld
这样我们的第一个spring项目就完成了