代码:
1.配置文件
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-2.5.xsd">
<bean id="hw" class="com.springtest1.HelloWorld">
</bean>
</beans>
2.源代码
HelloWorld.java
package com.springtest1;
public class HelloWorld {
//定义方法show,在控制台输出信息
public void show(){
System.out.println("欢迎学习Spring框架");
}
}
TestHelloWorld.java
package com.springtest1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestHelloWorld {
public static void main(String[] args) {
//加载applicationContext.xml配置
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取配置中的实例
HelloWorld hw=(HelloWorld) context.getBean("hw");
//调用方法
hw.show();
}
}
截图: