Spring容器的具体测试代码

8 篇文章 0 订阅

首先先建立一个Worker类

import java.io.Serializable;

public class Worker implements Serializable {
      private String name="光头强";
      private Tool tool;
      public void setTool(Tool tool) {
          System.out.println("注入tool对象");
          this.tool=tool;
      }
    public void work() {
        System.out.println(name+"使用"+tool+"砍树");
    }
}

武器类:

import java.io.Serializable;

public class Axe implements Tool,Serializable{
   private String name="开天斧";
   @Override
    public String toString() {
        // TODO Auto-generated method stub
        return name;
    }    
}

import java.io.Serializable;

public class Saw implements Tool,Serializable{
       private String name="寒冰锯";
       @Override
    public String toString() {
        // TODO Auto-generated method stub
        return name;
    }
}

Tool接口

public interface Tool {
}

beans3.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <!-- 利用Spring 调用对象的生命周期管理方法 -->
    <bean id="logger" class="day02.Logger"  
    init-method="open"    lazy-init="true" destroy-method="close"/>
    <!--依赖注入DI  -->
    <bean id="a1" class="day02.Axe"></bean>
    <bean id="qiang" class="day02.Worker">
    <!--注入属性name="axe"则自动查找setAxe方法  -->
    <property name="axe" ref="a1"></property>
    </bean>
    <!-- 解耦:spring和接口配合,实现组件之间解耦,也就是紧耦合转换为松耦合关系 -->
    <bean id="ax" class="day02.demo.Axe"/>
    <bean id="sw" class="day02.demo.Saw"/>
    <bean id="worker" class="day02.demo.Worker">
    <property name="tool" ref="sw"></property>
    </bean>

 

测试类

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestCase4 {
        
    ClassPathXmlApplicationContext ctx;
    @Before
        public void init() {
        ctx=new ClassPathXmlApplicationContext("beans3.xml");
    }
    
    @Test
    public void testWork() {
        day02.demo.Worker worker=ctx.getBean("worker1",day02.demo.Worker.class);
        worker.work();
    }   
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.tedu</groupId>
  <artifactId>Spring02</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
      <maven.compiler.target>1.8</maven.compiler.target>
      <maven.compiler.source>1.8</maven.compiler.source>
      </properties>
   <dependencies>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>4.3.25.RELEASE</version>
      </dependency>
      <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      </dependency>
      <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
      
      </dependency>
      
      
      
           <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
      
      </dependency>
      
      
  </dependencies>
</project>

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

饭九钦vlog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值