spring boot配置应用程序上下文层次结构

时间:2017-07-15 00:23:44

标签: spring spring-boot configuration

我有一个春季启动层次结构应用程序上下文配置的问题,我的项目是一场网络大战,日志显示spring boot启动两次,一次是通过web容器初始化,一次是用嵌入式容器盯着,因为它不应该启动有嵌入式容器,所以第二次抛出异常。这是我的SpringBootServletInitializer:

    @Order(Ordered.HIGHEST_PRECEDENCE)
    @Configuration
    public class RootConfig extends SpringBootServletInitializer {


        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return  application.sources(ConsoleWebApplication.class).child(WebConfig.class).web(true).sibling(AjaxConfig.class).web(true);
        }

    ......
    ......
    }

ConsoleWebApplication.java:

    @SpringBootApplication(exclude = DispatcherServletAutoConfiguration.class)
    @ComponentScan(
            basePackages = "com",
            excludeFilters =
            @ComponentScan.Filter({Controller.class, ControllerAdvice.class}))
    @EnableAspectJAutoProxy(proxyTargetClass = true)
    public class ConsoleWebApplication  {

        public static void main(String[] args) {
            SpringApplication.run(ConsoleWebApplication.class, args);
        }
    }    

WebConfig.java:

    @EnableWebMvc
    @ComponentScan(
            basePackages = "com",
            useDefaultFilters = false,
            includeFilters = @ComponentScan.Filter(WebController.class)
    )
    public class WebConfig extends WebMvcConfigurerAdapter {

        @PostConstruct
        public void init(){
            System.out.println("-----------------------WebConfig");
        }

        @Override
        public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
            argumentResolvers.add(new CurrentUserMethodArgumentResolver());
            super.addArgumentResolvers(argumentResolvers);
        }

     ......
     ......
    }    

AjaxConfig:

    @EnableWebMvc
    @ComponentScan(
             basePackages = "com",
             useDefaultFilters = false,
             includeFilters =
             @ComponentScan.Filter({AjaxController.class, AjaxControllerAdvice.class}))
    public class AjaxConfig extends WebMvcConfigurerAdapter {

         @PostConstruct
         public void init(){
             System.out.println("-----------------------AjaxConfig");
         }


         @Autowired
         ObjectMapper objectMapper;

         @Autowired
         Marshaller marshaller;

         @Autowired
         Unmarshaller unmarshaller;

    ......
    ......
    }   

web pom.xml:

<modelVersion>4.0.0</modelVersion>
<artifactId>console-web</artifactId>
<packaging>war</packaging>
......
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>    
......

tomcat启动日志:

    ......Connected to server
        [2017-07-15 07:58:06,249] Artifact web: Artifact is being deployed, please wait...
        15-Jul-2017 07:58:10.295 信息 [RMI TCP Connection(5)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
        2017-07-15 07:58:11,808 INFO o.s.b.StartupInfoLogger [RMI TCP Connection(5)-127.0.0.1] Starting RootConfig on DESKTOP-FEKUQDP with PID 11396 (H:\快盘\开发\spring\console-parent\console-web\target\console-web-1.0.0\WEB-INF\classes started by yyi in E:\webapp\apache-tomcat-8.5.16\bin)
        2017-07-15 07:58:11,818 INFO o.s.b.SpringApplication [RMI TCP Connection(5)-127.0.0.1] No active profile set, falling back to default profiles: default
        2017-07-15 07:58:12,054 INFO o.h.v.i.u.Version [background-preinit] HV000001: Hibernate Validator 5.3.5.Final
        2017-07-15 07:58:12,231 INFO o.s.c.s.AbstractApplicationContext [RMI TCP Connection(5)-127.0.0.1] Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2d7d1b26: startup date [Sat Jul 15 07:58:12 CST 2017]; root of context hierarchy
        2017-07-15 07:58:14,121 INFO o.s.d.r.c.RepositoryConfigurationDelegate [RMI TCP Connection(5)-127.0.0.1] Multiple Spring Data modules found, entering strict repository configuration mode!
        15-Jul-2017 07:58:16.042 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [E:\webapp\apache-tomcat-8.5.16\webapps\manager]
        15-Jul-2017 07:58:16.094 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [E:\webapp\apache-tomcat-8.5.16\webapps\manager] has finished in [51] ms
        2017-07-15 07:58:16,130 INFO o.s.d.r.c.RepositoryConfigurationDelegate [RMI TCP Connection(5)-127.0.0.1] Multiple Spring Data modules found, entering strict repository configuration mode!
        2017-07-15 07:58:16,798 INFO o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker [RMI TCP Connection(5)-127.0.0.1] Bean 'shiroConfig' of type [com.founder.console.web.config.ShiroConfig$$EnhancerBySpringCGLIB$$a102b8d1] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
        2017-07-15 07:58:16,879 INFO o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker [RMI TCP Connection(5)-127.0.0.1] Bean 'jpaConfig' of type [com.founder.config.JpaConfig$$EnhancerBySpringCGLIB$$3c46880a] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
        2017-07-15 07:58:17,197 INFO o.s.c.e.EhCacheManagerFactoryBean [RMI TCP Connection(5)-127.0.0.1] Initializing EhCache CacheManager
        2017-07-15 07:58:17,381 INFO o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker [RMI TCP Connection(5)-127.0.0.1] Bean 'ehCacheManagerFactoryBean' of type [org.springframework.cache.ehcache.EhCacheManagerFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
        2017-07-15 07:58:17,395 INFO o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker [RMI TCP Connection(5)-127.0.0.1] Bean 'ehCacheManagerFactoryBean' of type [net.sf.ehcache.CacheManager] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
        ......
        ......
        ......
        ......
        017-07-15 07:58:29,368 INFO o.s.j.e.MBeanExporter [RMI TCP Connection(5)-127.0.0.1] Located managed bean 'beansEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=beansEndpoint]
        2017-07-15 07:58:29,372 INFO o.s.j.e.MBeanExporter [RMI TCP Connection(5)-127.0.0.1] Located managed bean 'infoEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=infoEndpoint]
        2017-07-15 07:58:29,376 INFO o.s.j.e.MBeanExporter [RMI TCP Connection(5)-127.0.0.1] Located managed bean 'loggersEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=loggersEndpoint]
        2017-07-15 07:58:29,386 INFO o.s.j.e.MBeanExporter [RMI TCP Connection(5)-127.0.0.1] Located managed bean 'metricsEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=metricsEndpoint]
        2017-07-15 07:58:29,390 INFO o.s.j.e.MBeanExporter [RMI TCP Connection(5)-127.0.0.1] Located managed bean 'traceEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=traceEndpoint]
        2017-07-15 07:58:29,394 INFO o.s.j.e.MBeanExporter [RMI TCP Connection(5)-127.0.0.1] Located managed bean 'dumpEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=dumpEndpoint]
        2017-07-15 07:58:29,398 INFO o.s.j.e.MBeanExporter [RMI TCP Connection(5)-127.0.0.1] Located managed bean 'autoConfigurationReportEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=autoConfigurationReportEndpoint]
        2017-07-15 07:58:29,401 INFO o.s.j.e.MBeanExporter [RMI TCP Connection(5)-127.0.0.1] Located managed bean 'configurationPropertiesReportEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=configurationPropertiesReportEndpoint]
        2017-07-15 07:58:29,434 INFO o.s.b.StartupInfoLogger [RMI TCP Connection(5)-127.0.0.1] Started RootConfig in 18.43 seconds (JVM running for 24.637)

          .   ____          _            __ _ _
         /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
        ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
         \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
          '  |____| .__|_| |_|_| |_\__, | / / / /
         =========|_|==============|___/=/_/_/_/
         :: Spring Boot ::        (v1.5.4.RELEASE)

        2017-07-15 07:58:30,099 INFO o.s.b.SpringApplication [RMI TCP Connection(5)-127.0.0.1] No active profile set, falling back to default profiles: default
        2017-07-15 07:58:30,103 INFO o.s.c.s.AbstractApplicationContext [RMI TCP Connection(5)-127.0.0.1] Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@234aeedc: startup date [Sat Jul 15 07:58:30 CST 2017]; parent: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2d7d1b26
        2017-07-15 07:58:30,715 WARN o.s.c.s.AbstractApplicationContext [RMI TCP Connection(5)-127.0.0.1] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
        2017-07-15 07:58:30,724 ERROR o.s.b.SpringApplication [RMI TCP Connection(5)-127.0.0.1] Application startup failed
        org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
            at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
            at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
            at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
            at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
            at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
            at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:134) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
            at org.springframework.boot.builder.SpringApplicationBuilder.runAndExtractParent(SpringApplicationBuilder.java:219) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
            at org.springframework.boot.builder.SpringApplicationBuilder.sibling(SpringApplicationBuilder.java:247) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
            at com.founder.console.web.config.RootConfig.configure(RootConfig.java:35) ~[classes/:?]
            at org.springframework.boot.web.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:118) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
            at org.springframework.boot.web.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:86) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
            at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
            at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5196) ~[catalina.jar:8.5.16]
            at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[catalina.jar:8.5.16]
            at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752) ~[catalina.jar:8.5.16]
            at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728) ~[catalina.jar:8.5.16]
            at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734) ~[catalina.jar:8.5.16]
            at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1739) ~[catalina.jar:8.5.16]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_121]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_121]
            at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_121]
            at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) ~[tomcat-coyote.jar:8.5.16]
            at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) ~[?:1.8.0_121]
            at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) ~[?:1.8.0_121]
            at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482) ~[catalina.jar:8.5.16]
            at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431) ~[catalina.jar:8.5.16]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_121]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_121]
            at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_121]
            at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) ~[tomcat-coyote.jar:8.5.16]
            at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) ~[?:1.8.0_121]
            at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) ~[?:1.8.0_121]
            at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468) ~[?:1.8.0_121]
            at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) ~[?:1.8.0_121]
            at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) ~[?:1.8.0_121]
            at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401) ~[?:1.8.0_121]
            at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829) ~[?:1.8.0_121]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_121]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_121]
            at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_121]
            at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346) ~[?:1.8.0_121]
            at sun.rmi.transport.Transport$1.run(Transport.java:200) ~[?:1.8.0_121]
            at sun.rmi.transport.Transport$1.run(Transport.java:197) ~[?:1.8.0_121]
            at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_121]
            at sun.rmi.transport.Transport.serviceCall(Transport.java:196) ~[?:1.8.0_121]
            at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568) ~[?:1.8.0_121]
            at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826) ~[?:1.8.0_121]
            at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683) ~[?:1.8.0_121]
            at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_121]

2 个答案:

答案 0 :(得分:0)

谢谢,但我没有修改build pom.xml,接下来是我的整个web pom.xml:                  http://maven.apache.org/xsd/maven-4.0.0.xsd">             4.0.0             控制台网络             战

        <name>console-web</name>
        <description>Founder securities console web</description>

        <parent>
            <groupId>com.founder</groupId>
            <artifactId>console</artifactId>
            <version>1.0.0</version>
        </parent>

        <properties>
            <thymeleaf.version>3.0.6.RELEASE</thymeleaf.version>
            <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
            <thymeleaf-shiro.version>2.0.0</thymeleaf-shiro.version>
            <start-class>com.founder.console.web.ConsoleWebApplication</start-class>
        </properties>


        <dependencies>
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>console-domain</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>console-service</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.github.theborakompanioni</groupId>
                <artifactId>thymeleaf-extras-shiro</artifactId>
                <version>${thymeleaf-shiro.version}</version>
            </dependency>

        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>


            </plugins>
        </build>


    </project>

答案 1 :(得分:-1)

您需要添加一些<plugin>。如果您使用并替换spring-boot-maven插件,请删除所有插件。您可以使用以下代码: -

   <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>

并添加更多依赖项,您可以获得以下依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.4.1</version>
    <scope>test</scope>
</dependency>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
</dependency>
   <dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>