码迷,mamicode.com
首页 > 编程语言 > 详细

Spring中di配合接口编程

时间:2015-08-25 21:46:45      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

一:DI基本概念

依赖注入(DI),是spring容器实现的基础,在spring-core模块中实现的。所谓DI,就是指对象是被动接受依赖类而不是自己主动去找,换句话说就是指对象不是从容器中查找它依赖的类,而是在容器实例化对象的时候主动将它依赖的类注入给它。


DI作用:

di配合接口编程,的确可以减少层(web层) 和 业务层的耦合度.


二:DI配合接口编程案例

1.项目截图

技术分享


2.基本代码

package com.cloud.inter;

public interfaceChangeLetter {

   public String change();

}

 

 

 

package com.cloud.inter;

public classLowerLetter implements ChangeLetter{

   private String str;

   public String getStr() {

      return str;

   }

   public void setStr(String str) {

      this.str = str;

   }

   @Override

   public String change() {

      // TODO Auto-generated method stub

      return str.toLowerCase();

   }

  

}

 


package com.cloud.inter;

public classUpperLetter implements ChangeLetter{

   private String str;

   public String getStr() {

      return str;

   }

   public void setStr(String str) {

      this.str = str;

   }

   @Override

   public String change() {

      // TODO Auto-generated method stub

      return str.toUpperCase();

   }

}



3.配置代码

<?xml version="1.0"encoding="utf-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xmlns:context="http://www.springframework.org/schema/context"

      xmlns:tx="http://www.springframework.org/schema/tx"

      xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd

            http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd

            http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

 

<!-- 在测试代码中任意切换这两种配置方法 -->

<!--

<beanid="changeLetter" class="com.cloud.inter.UpperLetter">

   <propertyname="str">

      <value>abcef</value>

   </property>

</bean>

 -->

<bean id="changeLetter"class="com.cloud.inter.LowerLetter">

   <property name="str" value="ABCDE"/>

</bean>

</beans>


4.测试代码

package com.cloud.inter;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

public class App1 {

         /**

          * @param args

          */

         publicstatic void main(String[] args) {

                   //TODO Auto-generated method stub

                   ApplicationContextac=new ClassPathXmlApplicationContext("com/cloud/inter/beans.xml");

                   //获取不使用接口

                   //UpperLetterchangeLetter=(UpperLetter) ac.getBean("changeLetter");

                   //System.out.println(changeLetter.change());

                   //使用接口访问bean

                   ChangeLetterchangeLetter=(ChangeLetter) ac.getBean("changeLetter");

                   System.out.println(changeLetter.change());

         }

}


版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21

Spring中di配合接口编程

标签:

原文地址:http://blog.csdn.net/dzy21/article/details/47979789

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!