Spring bean在java配置中初始化两次

时间:2018-04-12 09:28:05

标签: spring spring-mvc spring-security

我使用基于java的配置的spring-security创建了一个spring应用程序。我的项目中还包含了一个jar文件(由我创建)。 我面临的问题是: - 我必须在两个类中编写 @ComponentScan(basePackages = {" com.mypackage"}) SpringConfig.java和SecurityConfig .java )导致bean初始化两次。 删除任何一个@componentscan都会导致错误: - 使用名称' securityConfig' 创建bean时出错。

以下是我的java课程。

SpringConfig.java

package com.mypackage.config;

import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = {"com.mypackage"})
public class SpringConfig extends WebMvcConfigurerAdapter {
    private static final Logger logger = LoggerFactory.getLogger(SpringConfig.class);

    @PostConstruct
    public void init(){
        logger.debug("Spring Config initialized");
    }
}

SecurityConfig.java

package com.mypackage.config;

import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
@ComponentScan(basePackages = {"com.mypackage"})
public class SecurityConfig extends WebSecurityConfigurerAdapter{
    private static final Logger logger = LoggerFactory.getLogger(SecurityConfig.class);

    //This Configuration class is in my jar file.
    // with package starting with  same name com.mypackage
    @Autowired
    com.mypackage.frameworks.config.Configuration config;

    @PostConstruct
    public void init(){
        logger.debug("Security config initiaziled");
    }

    @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) {
    try {
        auth.inMemoryAuthentication()
            .withUser("admin").password("admin").roles("USER");
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

MyController.java

package com.mypackage.controller;

import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

@Controller
public class MyController {
    private static final Logger logger = LoggerFactory.getLogger(MyController.class);

    @PostConstruct
    public  void init(){
        logger.debug("-------Controller created-------");
    }
}

2 个答案:

答案 0 :(得分:0)

Bean将被配置和创建两次,因为两个应用程序上下文都扫描相同的包" com.mypackage"。一种解决方案是将SpringConfig bean包与SecurityConfig bean包分开。尽可能在@ComponentScan包值

中更具体

答案 1 :(得分:0)

您已将bean定义配置为多个@Configuration类。我的建议是 - 将 @Conmpiguration 类与 @Import 集成到一个地方。 现在,您可以在一个地方应用 @ComponentScan(basePackages = {" com.mypackage"}),并且上下文也只加载一次bean。

@Import 注释提供了这种支持,它直接等同于Spring beans XML文件中的 元素。 请参阅此链接 - https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch04s03.html