【Application启动类】
所在的包位置从上往下扫描不在启动类所在包当前包
或者启动类所在包的子包
中,那么需要在启动类中配置@ComponentScan注解来添加这些类的包指【SpringBoot项目入口类】
这个类的位置很关键
:启动类如下(Spring自动生成的启动类)
测试类【必须】在启动类当前目录
或者启动类所在类的子目录
(把测试类看作一般类即可),如果想把测试类放到启动类的父级目录,首先要1.把启动类的位置修改到目标目录
,2.再修改测试类的位置到目标目录
。
package com.example.rabbitmq;
import org.springframework.amqp.core.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"com"})
public class DemoRabbitmqApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(DemoRabbitmqApplication.class, args);
}
}
测试类:
package com.example.rabbitmq;
import com.demo.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Created by zhangtengda on 2018/5/30.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class CheckTest {
@Autowired
private Person person;
@Test
public void testPerson() {
person.say("dada", 5);
}
}