1.action
package action; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import service.TestIocService; public class TestIocAction { private TestIocService testIocService; // //1.依赖类,set方式注入 // public void setTestIocService(TestIocService testIocService) { // this.testIocService = testIocService; // } //2.依赖类,构造器方式注入 public TestIocAction(TestIocService testIocService) { this.testIocService = testIocService; } public void add(){ testIocService.add(); } public static void main(String[] args) { //使用Spring的工厂将ioc容器中的对象取出 BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml"); TestIocAction test = (TestIocAction) factory.getBean("testIocAction"); test.add(); } }
2.service
package service; import dao.TestIocDao; public class TestIocService { private TestIocDao testIocDao; // //1.依赖类,set方式注入 // public void setTestIocDao(TestIocDao testIocDao) { // this.testIocDao = testIocDao; // } //2.依赖类,构造器方式注入 public TestIocService(TestIocDao testIocDao) { this.testIocDao = testIocDao; } public void add(){ testIocDao.add(); } }
3.dao
package dao; public class TestIocDao { public void add(){ System.out.println("Dao==>>向数据库添加数据"); } }
4.applicationContext.xml【1.依赖类,set方式注入】
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd"> <!-- action配置 --> <bean id="testIocAction" class="action.TestIocAction"> <!-- property代表是通过set方法注入,ref的值表示注入的内容--> <property name="testIocService" ref="testIocService"/> </bean> <!-- service配置 --> <bean id="testIocService" class="service.TestIocService"> <!-- property代表是通过set方法注入,ref的值表示注入的内容--> <property name="testIocDao" ref="testIocDao"/> </bean> <!-- dao配置 --> <bean id="testIocDao" class="dao.TestIocDao"></bean> </beans>
4.applicationContext.xml【1.依赖类,构造器方式注入】
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd"> <!-- action配置 --> <bean id="testIocAction" class="action.TestIocAction"> <!--constructor-arg代表是通过构造函数注入,ref的值表示注入的内容--> <constructor-arg ref="testIocService"/> </bean> <!-- service配置 --> <bean id="testIocService" class="service.TestIocService"> <!--constructor-arg代表是通过构造函数注入,ref的值表示注入的内容--> <constructor-arg ref="testIocDao"/> </bean> <!-- dao配置 --> <bean id="testIocDao" class="dao.TestIocDao"></bean> </beans>
时间: 2024-10-12 01:35:03