题解代码:
1.HelloWorld.java
package step1;
public class HelloWorld {
/********** Begin **********/
public void sayHello(){
System.out.println("Hello Spring");
}
/********** End **********/
}
2.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="helloworld" class="step1.HelloWorld"></bean>
</beans>
3.Test1.java
package step1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import step1.HelloWorld;
public class Test1 {
public static void main(String[] args) {
/********** Begin **********/
//1、创建Spring的IOC容器的对象
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//2、从IOC的容器中获取Bean的实例
HelloWorld helloWorld = (HelloWorld)app.getBean("helloworld");;
//3、调用方法
helloWorld.sayHello();
/********** End **********/
}
}