spring 实现测试解耦

第一步:新建maven 项目  FirstSpringJAVA

第二步:导入spring对应的jar包

pom.xml

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.1.RELEASE</version>
    </dependency>
    
     <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-beans</artifactId>  
            <version>4.3.1.RELEASE</version>  
            <type>jar</type>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-webmvc</artifactId>  
            <version>4.3.1.RELEASE</version>  
            <type>jar</type>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-orm</artifactId>  
            <version>4.3.1.RELEASE</version>  
            <type>jar</type>  
        </dependency>

第三步:声明接口

People.java

package com.xuzhiwen.spring2;

public interface People {
    public abstract void eat();
}

第四步:新建两个接口实现类

Man.java

package com.xuzhiwen.spring2;

public class Man implements People{

    @Override
    public void eat() {
        System.out.println("man eat()...");
    }
}

Woman.java

package com.xuzhiwen.spring2;

public class Woman implements People{

    @Override
    public void eat() {
        System.out.println("Woman eat()...");
    }
}

第五步:新建工厂类

SpringFactory.java

package com.xuzhiwen.spring2;

public class SpringFactory {
    public People people;
    
    public void setPeople(People people) {
        this.people = people;
    }
    
    public People getPeople() {
        return people;
    }
}

 

第六步: 新建spring配置文件

applicationContext.xml

<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-3.0.xsd">

    <bean id="springFactory" class="com.xuzhiwen.spring2.SpringFactory">
        <property name="people" ref="woman" />
    </bean>
    
    <bean id="man" class="com.xuzhiwen.spring2.Man" />
    <bean id="woman" class="com.xuzhiwen.spring2.Woman" />
    
</beans>

第七步:编写测试类

TestSpringFactory.java

package com.xuzhiwen.spring2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpringFactory {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        SpringFactory people = (SpringFactory) app.getBean("springFactory");
        people.getPeople().eat();
    }
}

第八步:运行结果如下

第九步:这样就可以通过直接修改配置文件来实现解耦

<property name="people" ref="woman" />


 

转载于:https://www.cnblogs.com/beibidewomen/p/7382699.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值