1.使用Eclipse创建Web应用并导入jar包

2.创建接口 TestDao

Spring 解决的是业务逻辑层和其它层的耦合问题,因此它将面向接口的编程思想贯穿整个系统应用

package dao;

public interface TestDao {

	public void sayHello();
}

3.创建接口TestDao的实现类TestDaoImpl

package dao;

public class TestDaoImpl implements TestDao {

	@Override
	public void sayHello() {
		// TODO Auto-generated method stub
		System.out.println("Hello,Study hard!");
	}

}

4.创建配置文件 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="..." class="...">
        collaborators and configuration for this bean go here
    </bean>

    <bean id="..." class="...">
        collaborators and configuration for this bean go here
    </bean>

    more bean definitions go here -->
    
    <!-- 将指定类TestDaoImpl配置给Spring,让Spring创建它的实例 -->
    <bean id="test" class="dao.TestDaoImpl"></bean>

</beans>

5.创建测试类

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dao.TestDao;

public class Test {

	public static void main(String[] args) {
		//初始化Spring容器ApplicationContext,加载配置文件
		ApplicationContext appCon =new ClassPathXmlApplicationContext("applicationContext.xml");
		//通过容器获取test实例
		TestDao tt =(TestDao)appCon.getBean("test"); //test为配置文件中的id
		tt.sayHello();
	}

}