gpt4 book ai didi

java - Spring Security Web应用程序上下文不加载bean

转载 作者:行者123 更新时间:2023-12-02 01:51:18 26 4
gpt4 key购买 nike

问题

我无法从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

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
}

}

更新代码后,日志保持不变:“...已加载 0 个 beans...”。

最佳答案

提供应用程序的文件夹结构将有助于正确回答这个问题。

同时您可以尝试以下解决方案 -

假设该文件不在 @ImportResource({"classpath*:spring-beans.xml"}) 中的任何 jar 中,指定bean定义文件的绝对路径。例如:@ImportResource({"classpath:/configurations/spring-beans.xml"})

关于java - Spring Security Web应用程序上下文不加载bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52950239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com