spring 构造器多构造器指定初始化对象+test测试

本文介绍如何在不使用JUnit4的情况下,在Java包内通过构造器初始化Spring对象并进行测试。
摘要由CSDN通过智能技术生成
package testone.bean;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author helloLi
 * @version 1.0
 * @date 2020/4/7 22:59
 */
public class TestStudent {
    int id;
    String name;

    public TestStudent(String name) {
        this.name = name;
    }

    public TestStudent(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public TestStudent() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
<?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 = "TestStudent" class="testone.bean.TestStudent">
    <constructor-arg name="id" type="int" value="1"/>
    <constructor-arg name="name" type="java.lang.String" value="lisi"/>
    </bean>
    <bean id = "testStudent2" class="testone.bean.TestStudent">
        <constructor-arg name="name" type="java.lang.String" value="wangwu"/>
    </bean>

</beans>
package testone.bean;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.context.support.XmlWebApplicationContext;

import static org.junit.Assert.*;

/**
 * @author helloLi
 * @version 1.0
 * @date 2020/4/8 9:33
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:META-INF/spring/demo.xml")
public class TestStudentTest {

    @Autowired
    @Qualifier("testStudent2")
    private TestStudent testStudent;

    @Test
    public void TestStudentTest() {
        System.out.println(testStudent.getName());
    }
}

在这里插入图片描述

直接在java包里面测试不在Junit4中:

package testone.bean;

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

/**
 * @author helloLi
 * @version 1.0
 * @date 2020/4/8 9:45
 */
public class Test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/demo.xml");
//        String path = "demo.xml";
//        context.setConfigLocation(path);
//        context.refresh();
      TestStudent student  =  (TestStudent) context.getBean("testStudent2");
        System.out.println(student.getName());
    }
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值