将Spring模块化应用程序部署到tomcat后,“ Autowire”失败

巴托

我需要一些帮助,因为我自己也找不到解决方案,谷歌也没有帮助。我有一个模块化的spring应用程序,可以使用SpringBoot从命令行运行而没有问题。我使用gradle war命令创建了一场战争我已在具有PostgreSQL数据库和JRE 8的服务器上安装了Tomcat8。我已将war放入webapps文件夹中,并将外部conf文件放入conf文件夹中。我所有其他模块都位于war文件的libs文件夹中。运行Tomcat时,出现以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personDetailsService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private eu.bato.anyoffice.serviceapi.service.PersonService eu.bato.anyoffice.frontend.config.PersonDetailsService.personService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [eu.bato.anyoffice.serviceapi.service.PersonService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

这是我的主要conf文件的一部分:

@Configuration
@EnableAutoConfiguration
@ComponentScan(value = {"eu.bato.anyoffice"})
@EnableWebMvc
public class Application extends WebMvcConfigurerAdapter{

    @Autowired
    SchedulerService scheduler;

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

    @PostConstruct
    protected void startScheduler(){
        scheduler.start();
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

    @Autowired
    private MessageSource messageSource;

定义PersonDetailsS​​ervice bean的安全配置:

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private static final Logger log = LoggerFactory.getLogger(SecurityConfig.class);

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(personDetailsService()).passwordEncoder(new StandardPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .addFilterBefore(authenticationFilter(), LogoutFilter.class)
                .csrf().disable()
                .authorizeRequests()
                ......
                .permitAll();
    }

    @Bean
    PersonDetailsService personDetailsService() {
        return new PersonDetailsService();
    }

    @Bean
    Filter authenticationFilter() {
        BasicAuthenticationFilter basicAuthFilter = new BasicAuthenticationFilter(customAuthenticationManager(), new BasicAuthenticationEntryPoint());
        return basicAuthFilter;
    }

    @Bean
    ProviderManager customAuthenticationManager() {
        List<AuthenticationProvider> providers = new LinkedList<>();
        providers.add(daoAuthPovider());
        ProviderManager authenticationManager = new ProviderManager(providers);
        authenticationManager.setEraseCredentialsAfterAuthentication(true);
        return authenticationManager;
    }

    @Bean
    DaoAuthenticationProvider daoAuthPovider() {
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
        provider.setUserDetailsService(personDetailsService());
        provider.setPasswordEncoder(new StandardPasswordEncoder());
        //TODO: add salt
        return provider;
    }

PersonDetailsS​​ervice类的一部分:

public class PersonDetailsService implements UserDetailsService {

    private static final Logger log = LoggerFactory.getLogger(PersonDetailsService.class);

    private static final StandardPasswordEncoder encoder = new StandardPasswordEncoder();

    @Autowired
    private PersonService personService;

    @Autowired
    private Environment environment;

    @PostConstruct
    protected void initialize() {
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("Authenticating: " + username);

PersonService接口在程序包中eu.bato.anyoffice.serviceapi.service,其实现在其中,eu.bato.anyoffice.backend.service.impl并具有标签@Service@Transactional

如果有任何提示,我将不胜感激。我可以提供任何进一步的日志和信息。

收获的

您配置中的问题是,PersonService是由中@ComponentScan声明的加载Servlet Context

这样PersonService就无法Application Context在您SecurityConfig的加载位置进行访问

中的BeanServlet Context可以引用中的Bean Application Context,反之亦然!

所以把@ComponentScanSecurityConfig允许该组件是reacheable。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring Autowire在多层上失败

在将Spring / Hibernate应用程序部署到glassfish SessionFactoy初始化期间失败NullPointerException

对于模块化项目,从IntelliJ IDEA运行SpringBoot应用程序失败

部署Spring 4.0.1应用程序失败

将jspresso helloworld Web应用程序(5分钟教程)部署到tomcat6 / tomcat7失败

Spring @Autowire 失败,没有发现依赖错误类型的合格 bean

将统一3D应用程序部署到hololens应用程序失败,代码为-2147483645

将“初学者的 Django”应用程序部署到 Heroku 失败并出现 ModuleNotFoundError:没有名为“_tkinter”的模块

@Autowire因@Repository失败

错误:将应用程序部署到设备失败。错误0x80070005:访问被拒绝

将 React 应用程序部署到 Git Hub 页面失败

将angular 2应用程序部署到github页面-失败

将 django 应用程序部署到数字海洋失败(502 Bad Gateway)

将 Android 应用程序部署到模拟器失败,出现 NoClassDefFoundError

通过Azure Devops将Docker映像部署到Linux上的Web应用程序失败

将PHP应用程序部署到Google AppEngine失败,出现403错误

通过Boxfuse将Grizzly应用程序部署到AWS失败

将 Spring Boot 应用程序部署到 Weblogic -- Weblogic 正在尝试将我的主类加载为 XML 资源并失败

将Spring Boot应用程序部署到独立的Tomcat中后无法正常工作

将Spring Boot应用程序部署到Tomcat 8

将Spring应用程序部署在带有Insight的tc服务器中时,Spring应用程序在启动时失败,并显示NoClassDefFoundError

使用Github Actions将角度应用程序部署到Azure失败。找不到package.json

将应用程序部署到Heroku时,对Rails API后端和React前端的身份验证请求失败

使用Spring的模块化Web应用程序

Java Web应用程序部署失败

在tomcat7上以战争方式部署的Spring Boot应用程序失败,并出现错误“发现了多个名称为[org_apache_tomcat_websocket]的片段”

SpringBoot Rest API:作为 Java 应用程序工作,在 tomcat 上部署时失败

在独立的Java应用程序中使用Spring 3 autowire

问题与Spring启动Web应用程序的部署:没有错误,但应用仍失败