002. Spring Inversion of Control (IoC)
来源:互联网 发布:程序员漫画 编辑:程序博客网 时间:2024/04/20 13:59
1、创建Java项目:File -> New -> Java Project
2、引入必要jar包,项目结构如下
3、创建WorkService接口WorkService.java
package com.spring.service;public interface WorkService { public abstract void doWork();}
4、实现WorkServiceImp类WorkServiceImp.java
package com.spring.service.imp;import com.spring.service.WorkService;public class WorkServiceImp implements WorkService { @Override public void doWork() { System.out.println("WorkServiceImp do work..."); }}
5、创建Worker类Worker.java
package com.spring.model;import com.spring.service.WorkService;public class Worker { private WorkService workService = null; public WorkService getWorkService() { return workService; } public void setWorkService(WorkService workService) { this.workService = workService; } public void doWork() { workService.doWork(); }}
6、创建spring配置文件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="workService" class="com.spring.service.imp.WorkServiceImp"></bean> <bean id="worker" class="com.spring.model.Worker"> <property name="workService" ref="workService"></property> </bean></beans>
7、创建Spring测试类SpringUnit.java
package com.spring.junit;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.model.Worker;public class SpringUnit { @Test public void test() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); Worker worker = (Worker) ctx.getBean("worker"); worker.doWork(); ctx.close(); }}
8、测试结果
... 省略Spring日志信息 ...WorkServiceImp do work...... 省略Spring日志信息 ...
阅读全文
0 0
- 002. Spring Inversion of Control (IoC)
- Spring ioc(Inversion of Control )
- Spring (一) IOC ( Inversion Of Control )
- Ioc(Inversion of Control)
- 关于IOC(Inversion of Control)
- 3. Inversion of Control (IoC)
- Spring核心技术(1)控制反转(Inversion of Control,IoC)理论
- Spring的IOC(控制反转Inversion of Control)
- spring ioc(Inversion of Control) 和 Aop 总结
- Spring的IOC(Inversion of Control)(控制反转)
- Spring从IOC(Inversion of Control:控制反转)开始
- IoC(Inversion of Control)反转控制
- 控制反转(Ioc=Inversion of Control)
- Inversion of Control (IoC) and Dependency Injection
- IOC-Inversion of Control, Dependency Injection
- IOC(DI)- Inversion of Control(Dependency Injection)
- 胡扯IoC(Inversion of Control)
- IOC:Inversion Of Control--控制反转
- url 传递参数(特殊字符)
- cocoapods迭代版本时,出现问题
- memcache与redis区别及memcache与redis区别
- bootstrap页面缩小导航收缩和展开
- 【已解决】Android Studio下,gradle project sync failed 错误
- 002. Spring Inversion of Control (IoC)
- C++重载、重写、重定义区别
- 矢量图标库
- 判断StringBuffer是否为空
- jqGrid的时间格式化问题
- linux虚拟机环境搭建和管理
- PHP:win7 ASP.NET环境与PHP(WAMP)环境如何共存
- PHP:phpMyAdmin如何解决本地导入文件(数据库)为2M的限制
- iOS 利用归档解档实现类似微博及一些CRM类软件的草稿箱思路