Spring整合Junit(spring注解方式注入bean)

7 篇文章 0 订阅
2 篇文章 0 订阅

一、开发环境

1.idea 2018.1

2.maven 3.3.3

3.jdk1.8

二、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com.ygq</groupId>
    <artifactId>spring_02_DI</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    </dependencies>
</project>

三、spring核心配置文件applicationContext.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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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.xsd">
    <!--开启包扫描-->
    <context:component-scan base-package="com.ygq"/>
    <!--开启注解扫描-->
    <context:annotation-config />

    <!--由 Spring容器创建该类的实例对象 &ndash;&gt;-->
   <!--<bean id="messageDao" class="com.ygq.dao.MessageDao" />-->
    <!--<bean id="messageService" class="com.ygq.service.impl.MessageServiceImpl" />-->
   <!--<bean id="messageController" class="com.ygq.controller.MessageController" />-->
</beans>

四、项目结构

1.整体结构

2.MessageDao.java

package com.ygq.dao;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Repository;


@Repository
public class MessageDao {
    public  String sayHello(){
        return "Hello I'm MessageDao!";
    }

}

3.MessageService.java

package com.ygq.service;

public interface MessageService {
    public   String sayHello();
}

4.MessageServiceImpl.java

package com.ygq.service.impl;

import com.ygq.dao.MessageDao;
import com.ygq.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private  MessageDao messageDao;
    public  String sayHello() {
        String s = messageDao.sayHello();
        System.out.println(s);
        return "Hello I'm MessageServiceImpl!";
    }
}

5.测试类 com.ygq.service.impl.MessageServiceImplTest.java

package com.ygq.service.impl;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class MessageServiceImplTest {

    @Autowired
    private MessageServiceImpl messageServiceImpl;

    @Test
    public void sayHello() {
        String s = messageServiceImpl.sayHello();
        System.out.println(s);
    }

}

6.运行结果

Hello I'm MessageDao!
Hello I'm MessageServiceImpl!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

再写一行代码就下班

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

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

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

打赏作者

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

抵扣说明:

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

余额充值