spring boot 搭建遇到的坑

此文章,用来记录小白学习Springboot过程中遇到的一些。

前言:我搭建的项目是基于maven的springboot,其整个工程包含了 web,application,domain,infrastructure,common 这四个modules,其依赖关系如下:

web-->application-->domain

                           --> infrastructure

                          --> common

其中 web,application,domain,infrastructure依赖于common; infrastructure 分别被 application和domain依赖。

问题1.  我用单元测试跑整个工程的时候发现是不会报错的,但正常启动的时候会报:

 orgManager in com.swhy.application.AccountManager required a bean of type 'com.swhy.domain.repository.DddRepository' that could not be found.

其中 DddRepository是一个用于进行数据库的访问操作的接口,其被DddRepostoryImpl实现,在DddRepostoryImpl中有一个Mapper注入依赖 ,类似于如下所示:

@Service
public class DddRepository implements DddRepository{
   
   @Autowired
   private AccountMapper accountMapper;

   @override
   public *** ***(){
      ****
   }
}

其中AccountMapper存放在infrastructure 模块包中,其中AccountMapper源码如下:

@Mapper
public interface AccountMapper{
   List<AccountVO> getAccountList();
}

其springboot启动类中没有进行mapper的扫描。

一般出现 *** required a bean of type,出现的原因有如下几种情况:

  1. 没有给对应的DAO借口注入@Mapper属性
  2. spring没有扫描到相应的bean
  3. 加相应的注释。在注入的时候报这个错误,应该是被注入的对象类上没有如:@Service 的注解

上述几种方法我都试了都不行,因为我都mapper上的注解用的是@Mapper,所以在Springboot启动类中,没有添加MapperScan进行扫描了,

@Repository需要用@MapperScan扫描mapper才能注入@Bean

而@Mapper注解相当于@Repository注解+@MapperScan注解,通过xml文件下的namespace命名空间自动注入bean

但我遇到的情况是,如果不加MapperScan就会报上述错误,加了@MapperScan(value = {"com.swhy"}),其就不会报错,希望大家遇到这种情况的时候,也把@MapperScan注解加上试试。

 

问题2、 访问程序,发现报如下错误:应用程序的调用逻辑如下:dddController(web层) ------> AccountManager#getInfoList(application层)------->OrgManagerImpl#getList(domain层)---->DddRepositoryImpl#findAccountList(infrastucture层)------->accountMapper.findAll() (DAO层)

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.swhy.domain.service.OrgManager.getList
	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235) ~[mybatis-3.5.2.jar:3.5.2]
	at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53) ~[mybatis-3.5.2.jar:3.5.2]
	at org.apache.ibatis.binding.MapperProxy.lambda$cachedMapperMethod$0(MapperProxy.java:61) ~[mybatis-3.5.2.jar:3.5.2]
	at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) ~[na:1.8.0_271]
	at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:61) ~[mybatis-3.5.2.jar:3.5.2]
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:56) ~[mybatis-3.5.2.jar:3.5.2]
	at com.sun.proxy.$Proxy60.getList(Unknown Source) ~[na:na]
	at com.swhy.application.AccountManager.getInfoList(AccountManager.java:28) ~[classes/:na]
	at com.swhy.controller.DddController.list(DddController.java:28) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_271]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_271]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_271]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_271]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:891) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.34.jar:8.5.34]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
	at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]

通过debug,发现一个奇怪的现象,明明我的OrgManager是一个应用层的@Service,怎么就变成了MapperProxy的代理对象了(进行DAO操作的),通过分析发现,@MapperScan(value = {"com.swhy"}),然后mybatis将所有的接口实例化成MapperProxy的代理对象,解决此问题需要将MapperScan进行精确匹配就行了

  出现这个问题还有一个原因就是,其.xml文件没有编译到target目录或者jar包里面导致,其根源在于mybatis的configuration进行反射时报错了(dao和xml进行关联),如dao的方法和xml方法不一致等

 </build>
        <resources>
            <resource>
                <directory>src/main/java</directory><!--所在的目录-->
                <includes><!--包括目录下的.properties,.xml文件都会扫描到-->
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
             <resource>
                <directory>src/test/java</directory><!--所在的目录-->
                <includes><!--包括目录下的.properties,.xml文件都会扫描到-->
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值