2.0ioc
Posted chxbar.cn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2.0ioc相关的知识,希望对你有一定的参考价值。
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>
以上是关于2.0ioc的主要内容,如果未能解决你的问题,请参考以下文章
Linkedin OAuth 2.0 重定向 URL 不能包含片段标识符 (#)
用于模糊的 OpenGL ES 2.0 片段着色器速度慢且质量低
[死磕 Spring 17/43] --- IOC 之从单例缓存中获取单例 bean