在@SpringBootApplication包扫描不起作用

时间:2018-05-01 10:07:39

标签: maven web-services spring-mvc spring-boot

我有一个Spring启动应用程序,它使用maven依赖的其他模块。

所有模块都具有相同的包结构。

例如:com.ams.web.modelcom.ams.web.repositorycom.ams.web.servicecom.ams.web.websocket

我使用main方法为springbootapplication的主模块配置com.ams.web。但是当我尝试启动服务器时,它会因自动装配问题而失败。这表示应用程序无法从子模块扫描包。 [所有必需的课程都有@Service@Repository@Component注释,可以让他们进行扫描]

默认情况下,pacakge扫描如何在springboot应用程序中工作,该应用程序在其他模块上具有maven依赖性?如何从我的spring应用程序中启用其他模块的打包扫描。

代码:

package com.ams.web;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.Resource;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.DispatcherServlet;

import com.ams.web.filters.AllRequestFilter;

@SpringBootApplication(scanBasePackages={"com.ams.web"})
public class WebInitializer extends SpringBootServletInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {
       AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
       context.register(ApplicationConfig.class);

       PropertyConfigurator.configure(loadLog4jProperties(context));
       String profile = loadProperties.getProperty("env.name", "app");

       System.out.println("Env Profile : " + profile);

        ConfigurableEnvironment environment = context.getEnvironment();
    environment.setActiveProfiles(profile);

        context.setServletContext(servletContext);


        servletContext.addFilter("AllRequestFilter", new AllRequestFilter()).addMappingForUrlPatterns(null, true, "/*");

        servletContext.addFilter("amsSpringAllRequestFilter", new AmsSpringAllRequestFilter()).addMappingForUrlPatterns(null, true, "/*");

        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    }

UrlList:

package com.ams.web.repository;

import java.util.concurrent.ConcurrentLinkedQueue;

import org.springframework.stereotype.Component;

import com.ams.web.environment.All;

@All
@Component
public class UrlList {

我在另一个模块中的自定义注释:

package com.ams.web.environment;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.Profile;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Profile( "app", "mock", "test")
@Component
public @interface All {}

0 个答案:

没有答案
相关问题