form-message
,但是在新增的子module里的controller里的接口在swagger在线文档里不展示,下面是解决此问题以及发现别问题的各种处理方法 @Beanpublic CommandLineRunner appRun(ApplicationContext ac) {return args -> {/*** 通过类名(BeanName)获取已注入对象* 这里 helloWorld 是一个被注入到容器中的helloWorld==》helloWorldController类*/System.out.println("=====================================================");Object helloWorld = ac.getBean("helloWorldController");System.out.println(helloWorld);System.out.println("=====================================================");Object dog = ac.getBean("dogController");System.out.println(dog);};}
Description:Field dogService in com.liu.susu.start.controller.DogController required a bean of type 'com.liu2.susu.message.service.DogService' that could not be found.The injection point has the following annotations:- @org.springframework.beans.factory.annotation.Autowired(required=true)Action:Consider defining a bean of type 'com.liu2.susu.message.service.DogService' in your configuration.
每一个Spring项目都有独立的Spring容器去存储自己项目中所注册的bean。
上面问题出现的原因就是:启动类 StartApplication.java
启动时,模块 form-start
中Spring容器加载时未能将模块form-message
中所定义的bean注册进Spring容器中,导致@Autowired
注入模块form-message
中的bean进行使用时,找不到对应的bean,才会出现上面的问题 Consider defining a bean of type 'com.liu2.susu.message.service.DogService' in your configuration
.
form-start
的Spring容器启动时,去扫描模块form-message
包下的所有的组件并注册到模块form-start
的Spring容器中即可解决上述问题。解决问题如下继续……按照上面的分析,需要修改启动类上的注解了,
这个修改需要注意注解 @ComponentScan
与 注解@SpringBootApplication
的使用
@ComponentScan
注解的作用是:扫描标注了@Controller、@Service、@Repository、@Component 的类@SpringBootApplication(scanBasePackages = {"com.liu.susu","com.liu2.susu"})
@ComponentScan("com.liu.susu.*")
注释掉 或者 @ComponentScan
扫描多包即可,如下两种写法都可以://@ComponentScan("com.liu.susu.*")
@ComponentScan(basePackages = {"com.liu.susu.*", "com.liu2.susu"})
@SpringBootApplication
//@SpringBootApplication(scanBasePackages = {"com.liu.susu","com.liu2.susu"})
//@ComponentScan("com.liu.susu.*")
//@ComponentScan(basePackages = {"com.liu.susu.*", "com.liu2.susu"})
//@SpringBootApplication
@SpringBootApplication(scanBasePackages = {"com.liu.susu","com.liu2.susu"})
Description:Field dogMapper in com.liu2.susu.message.service.DogServiceImpl required a bean of type 'com.liu2.susu.message.mapper.DogMapper' that could not be found.The injection point has the following annotations:- @org.springframework.beans.factory.annotation.Autowired(required=true)Action:Consider defining a bean of type 'com.liu2.susu.message.mapper.DogMapper' in your configuration.
上面的原因:
在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类,但是@ComponentScan不扫描@Mapper
所以用注解@MapperScan
解决问题
@MapperScan
注解的作用是:指定要编译成接口实现类的包路径,在编译完成后这个包下的所有接口都会生成相应的接口实现类。
@MapperScan("com.liu2.susu.message.mapper")
好了,到此就完美解决了!,再看看接口文档
//@MapperScan("com.liu.susu.**.mapper")
@MapperScan(basePackages={"com.liu.susu.**.mapper","com.liu2.susu.message.mapper"})
或者用**
匹配@MapperScan("com.**.susu.**.mapper")