Spring依赖注入与配合接口编程案例
来源:互联网 发布:js根据id获取对象 编辑:程序博客网 时间:2024/05/16 18:39
spring开发提倡接口编程,配合di技术可以层与层的解耦
现在我们体验一下spring的di配合接口编程的,完成一个字母大小写转换的案例:
思路:
1. 创建一个接口 ChangeLetter
package com.xlc.inter;public interface ChangeLetter {//声明一个方法public String change();}
2. 两个类实现接口
package com.xlc.inter;public class LowwerLetter implements ChangeLetter {//把小写变为大写private String str;public String getStr() {return str;}public void setStr(String str) {this.str = str;}@Overridepublic String change() {// TODO Auto-generated method stubreturn str.toLowerCase();}}
package com.xlc.inter;public class UpparLetter implements ChangeLetter {private String str;public String getStr() {return str;}public void setStr(String str) {this.str = str;}@Overridepublic String change() {// TODO Auto-generated method stub//把小写字母变成大写returnstr.toUpperCase();}}
3. 把对象配置到spring容器中
<?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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- <bean id="changeLette" class="com.xlc.inter.UpparLetter"><property name="str"><value>abcdef</value></property></bean>--><bean id="changeLette" class="com.xlc.inter.LowwerLetter"><property name="str"><value>ABCDEFG</value></property></bean></beans>
4. 使用
在applicationContext中拿到bean后 转换成接口类型
package com.xlc.inter;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App1 {<span style="white-space:pre"></span>public static void main(String[] args) {<span style="white-space:pre"></span>// TODO Auto-generated method stub<span style="white-space:pre"></span>ApplicationContext app=new ClassPathXmlApplicationContext("com/xlc/inter/applicationContextContext.xml");<span style="white-space:pre"></span>ChangeLetter changeLetter=(ChangeLetter) app.getBean("changeLette");<span style="white-space:pre"></span>System.out.println(changeLetter.change());<span style="white-space:pre"></span>} }
<strong>ChangeLetter changeLetter=(ChangeLetter) app.getBean("changeLette");中得到的对象是由applicationContext.xml中配置的</strong><pre name="code" class="html" style="font-family: Arial, Helvetica, sans-serif;"><strong><bean id="changeLette" class="com.xlc.inter.UpparLetter">class<span style="font-family: Arial, Helvetica, sans-serif;">具体反射的bean决定的</span></strong>
<span style="font-family:Arial, Helvetica, sans-serif;"><strong>而bean方法又是由</strong></span><pre name="code" class="html"><strong><property name="str"><value>abcdef</value></property>根据实际情况切换的,解耦目的达到</strong>
若将applicationContext.xml中注释切换
<!-- <bean id="changeLette" class="com.xlc.inter.UpparLetter"><property name="str"><value>abcdef</value></property></bean>--><bean id="changeLette" class="com.xlc.inter.LowwerLetter"><property name="str"><value>ABCDEFG</value></property></bean>
<bean id="changeLette" class="com.xlc.inter.UpparLetter"><property name="str"><value>abcdef</value></property></bean><!-- <bean id="changeLette" class="com.xlc.inter.LowwerLetter"><property name="str"><value>ABCDEFG</value></property></bean>-->
di配合接口编程,可以减少层(web层) 和业务层的耦合度.
0 0
- Spring依赖注入与配合接口编程案例
- Spring 与依赖注入
- Spring的依赖注入(接口注入)
- Spring的依赖注入(接口注入)
- Spring的依赖注入(接口注入)
- Spring的依赖注入(接口注入)
- Spring的依赖注入(接口注入)
- 浅谈依赖注入思想与面向接口编程
- Spring 简单案例(依赖注入)_01
- Spring 简单案例(依赖注入)_02
- Spring依赖注入与面向切面编程以及注解
- Spring IOC与依赖注入
- 依赖注入与事件编程
- spring接口实现类 依赖注入
- Spring依赖注入的接口为null
- Spring中DI配合接口编程
- spring的DI配合接口编程
- 从针对接口编程到依赖注入
- Eclipse console返回null
- 知道这20个正则表达式 能让你少写1,000行代码
- Stack(last in first out) and Exception
- 虚拟局域网VLAN
- django框架从零开始_010_自动化测试
- Spring依赖注入与配合接口编程案例
- 2016 UESTC Training for Dynamic Programming C - 柱爷的下凡 预处理打表、背包问题
- 中国餐馆过程(Chinese Restaurant Process)
- Web app相关思考
- lombok工具jar
- android:使用Properties保存程序配置
- Java 注解
- 机器学习常见面试题
- 《c专家编程》学习笔记(一)