簡體   English   中英

Spring Security Web應用程序上下文不會加載Bean

[英]Spring Security Web application context doesn't load beans

問題

除了編程配置之外,我無法從spring-beans.xml加載bean。 我已經創建了一個自定義org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter並使用以下注釋為該類添加注釋: @Configuration, @ImportResource('classpath*:spring-beans.xml'), @EnableWebSecurity 正如您在日志中看到的那樣,沒有加載任何bean。 為什么? 我的目標是具有半靈活的配置。 我想在代碼中配置某些部分,並在spring-beans.xml文件中配置一些部分,這些部分應在啟動時加載到應用程序上下文中。 例如,我想定義一個自定義AuthentificationProvider並在spring-beans.xml文件中使用自定義配置創建它。 與提供者一起完成的工作應在代碼中固定。

日志

[INFO] 2018-10-23 14:48:05,569 TRACE (Default Executor-thread-11) [PathMatchingResourcePatternResolver(findAllClassPathResources:322)] Resolved classpath location [spring-beans.xml] to resources []
[INFO] 2018-10-23 14:48:05,573 TRACE (Default Executor-thread-11) [XmlBeanDefinitionReader(loadBeanDefinitions:229)] Loaded 0 bean definitions from location pattern [classpath*:spring-beans.xml]

Java代碼

@Configuration
@ImportResource({"classpath*:spring-beans.xml"})
@EnableWebSecurity
public class HSecurityConfig extends WebSecurityConfigurerAdapter {

    // configuration creates a servlet filter known as 'springSecurityFilterChain'

    @Autowired ApplicationContext applicationContext;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // configure some http stuff
    }

}

spring-beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:security="http://www.springframework.org/schema/security"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security.xsd">

<security:authentication-manager>
    <security:authentication-provider ref="myProvider"/>
</security:authentication-manager>

<bean id="myProvider"
      class="package.providers.MyProvider">
    <constructor-arg value="arg1" />
    <constructor-arg value="arg2" />
</bean>


</beans>

依存關系

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-bom</artifactId>
            <version>5.1.0.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
    </dependency>
</dependencies>

更新1

項目結構

該項目是標准的Maven文件夾結構

├── src
│   ├── main
│   │   ├── java
│   │   ├── resources
│   │   │   ├── META-INF
│   │   │   └── spring
│   │   └── webapp
│   │       └── WEB-INF
│   └── test
│       └── java

編輯Javacode

@Configuration
@ImportResource({"classpath*:/resources/spring/spring-beans.xml"})
@EnableWebSecurity
public class HSecurityConfig extends WebSecurityConfigurerAdapter {

    // configuration creates a servlet filter known as 'springSecurityFilterChain'

    @Autowired ApplicationContext applicationContext;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // configure some http stuff
    }

}

更新代碼后,日志保持不變:'... Loaded 0 beans ...'。

提供應用程序的文件夾結構將有助於正確回答此問題。

同時,您可以嘗試以下解決方案-

假設該文件不在任何jar中,請在@ImportResource({"classpath*:spring-beans.xml"}) ,指定Bean定義文件的絕對路徑。 例如: @ImportResource({"classpath:/configurations/spring-beans.xml"})

感謝@moilejter

為了解決這個問題,我只需要移動目錄。

修復前的結構

└── src
    └── main
        ├── java
        └── resources
            ├── META-INF
            └── spring

└── src
    └── main
        ├── java
        ├── resources
        │   └── META-INF
        └── spring

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM