参考:http://c.biancheng.net/view/4244.html
1.下载包
2.新建接口+类
3.新建xml文件
4.测试
package com.ligy.ioc; public interface IStudent { void do1(); }
package com.ligy.ioc; public class StudentImpl implements IStudent { @Override public void do1() { System.out.println("this is in do1"); } }
<?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-3.2.xsd"> <!-- 由 Spring容器创建该类的实例对象 --> <bean id="studentDao" class="com.ligy.ioc.StudentImpl" /> </beans>
package com.ligy.test; import com.ligy.ioc.StudentImpl; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test1 { @Test public void do1() { // 定义Spring配置文件的路径 String xmlPath = "applicationContext.xml"; // 初始化Spring容器,加载配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext( xmlPath); // 通过容器获取personDao实例 StudentImpl personDao = (StudentImpl) applicationContext .getBean("studentDao"); // 调用 personDao 的 add ()方法 personDao.do1(); } }