1、先看下项目的目录
2、新建一个Spring Start Project
3、新建HelloWorld的类,简单的输入name
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void printHello() {
System.out.println("Spring 3 : Hello ! " + name);
}
}
4、新建一个spring bean configuration file的配置文件
5、注意路径一定要正确,否则运行时会提示找不到该配置文件,路径是在src/main/resource下
6、配置钢材新建的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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloBean" class="com.example.demo.HelloWorld">
<property name="name" value = "yuna"/>
</bean>
</beans>
7、在程序入口的地方,将name输出出来,xxxApplication你新建spring项目的时候就创建好了
public class DemoApplication {
private static ApplicationContext content;
private static HelloWorld helloWorld;
public static void main(String[] args) {
// SpringApplication.run(DemoApplication.class, args);
content = new ClassPathXmlApplicationContext("applicationContext.xml");
helloWorld = (HelloWorld)content.getBean("helloBean");
helloWorld.printHello();
}
}
8、运行,项目–右击 –Run As —Spring Boot App,在控制台会有消息打印