开发者社区> 问答> 正文

spring初始化bean2次。读取了2次配置文件:报错

@红薯

展开
收起
kun坤 2020-06-06 23:57:07 702 0
1 条回答
写回答
取消 提交回答
  • 构造方法打印了2次,没找到问题,求大神解答啊。web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>xxxx</display-name>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/config/application-*.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    </web-app> 



    ######

    spring配置乱就会导致这个问题,

    spring-mvc.xml中,只注入Controller

      <context:component-scan base-package="" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
      </context:component-scan>

    spring-ctx.xml中注入非Controller对象,

      <context:component-scan base-package="">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
      </context:component-scan>

    ######理一理配置吧,######配置文件 在多个 地方添加了扫描Bean的标签。######应该不是配置扫描了2次,从启动日志看,是spring容器初始化了2次,导致读取了2次配置文件,所以就初始化了2次bean.昨天我把所有无关的配置都删除了,只剩下1个扫描的配置,只剩下1个包。但是还是执行了2次。确实不知道问题出在哪里。应该不是代码的问题。######SLF4J配置还有问题。。 可以把日志配置正常了看看。######从截图来看像是项目部署了两次了,我记得我们也有次碰到这种情况,那个项目是同时部署在多个地方,是个定时任务,有的地方每天都执行两次,但有的地方只执行一次,后来才发现是投产过程中,把项目放在tomcat/webapps下了,投产人员同时在项目部署文件中又配置了一个访问地址,这样导致这个项目有两个访问地址,就是一个是带项目名的,一个是不带项目名的,这样就会导致项目加载了两次######

    引用来自“探讨_止境”的评论

    从截图来看像是项目部署了两次了,我记得我们也有次碰到这种情况,那个项目是同时部署在多个地方,是个定时任务,有的地方每天都执行两次,但有的地方只执行一次,后来才发现是投产过程中,把项目放在tomcat/webapps下了,投产人员同时在项目部署文件中又配置了一个访问地址,这样导致这个项目有两个访问地址,就是一个是带项目名的,一个是不带项目名的,这样就会导致项目加载了两次
    我的web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>xxxx</display-name>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/config/application-*.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    </web-app> 
    //下面是application-hibernate.xml.其中只有一出配置了扫描包
    <!-- 该文件由ContextLoaderListener所在的上下文加载,并根据component-scan创建扫描包下面的对象,(不扫描controller) -->
    <context:component-scan base-package="com.easycoding.apps,com.easycoding.plugin">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    ######

    引用来自“探讨_止境”的评论

    从截图来看像是项目部署了两次了,我记得我们也有次碰到这种情况,那个项目是同时部署在多个地方,是个定时任务,有的地方每天都执行两次,但有的地方只执行一次,后来才发现是投产过程中,把项目放在tomcat/webapps下了,投产人员同时在项目部署文件中又配置了一个访问地址,这样导致这个项目有两个访问地址,就是一个是带项目名的,一个是不带项目名的,这样就会导致项目加载了两次

    引用来自“代骥”的评论

    我的web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>xxxx</display-name>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/config/application-*.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    </web-app> 
    //下面是application-hibernate.xml.其中只有一出配置了扫描包
    <!-- 该文件由ContextLoaderListener所在的上下文加载,并根据component-scan创建扫描包下面的对象,(不扫描controller) -->
    <context:component-scan base-package="com.easycoding.apps,com.easycoding.plugin">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    我说的问题是项目本身没有问题,是在项目部署到tomcat过程中,tomcat部署有两种方式,一种是直接放到tomcat/webapps路径下,另一种是在server.xml添加项目文件路径进行引用,两种方式采取其中一种方式即可,若是两种方式都进行采用的话,就会把项目部署两次
    2020-06-06 23:57:12
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多