![](/img/trans.png)
[英]Spring Security: The dependencies of some of the beans in the application context form a cycle
[英]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]
@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
}
}
<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>
該項目是標准的Maven文件夾結構
├── src
│ ├── main
│ │ ├── java
│ │ ├── resources
│ │ │ ├── META-INF
│ │ │ └── spring
│ │ └── webapp
│ │ └── WEB-INF
│ └── test
│ └── java
@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"})
為了解決這個問題,我只需要移動目錄。
修復前的結構
└── src
└── main
├── java
└── resources
├── META-INF
└── spring
解
└── src
└── main
├── java
├── resources
│ └── META-INF
└── spring
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.