- 导入需要的jar包
- 在src目录下新建一个applicationContext.xml文件
对应版本的xml文件可能有所不同去案例里面找
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="one" class="MK.OneInterfaceImpl"></bean>
</beans>
- 加载xml文件,得到ApplicationContext对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
- 使用ApplicationContext对象的方法getBean(string)创建对象
OneInterface o = (OneInterfaceImpl)ac.getBean("one");