除了编程配置之外,我无法从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
}
}
更新代码后,日志保持不变:'...已加载0个bean ...'。
答案 0 :(得分:1)
提供应用程序的文件夹结构将有助于正确回答此问题。
同时您可以尝试以下解决方案-
假设该文件不在任何jar中,请在@ImportResource({"classpath*:spring-beans.xml"})
中指定Bean定义文件的绝对路径。例如:@ImportResource({"classpath:/configurations/spring-beans.xml"})
答案 1 :(得分:1)
要解决此问题,我只需要移动目录。
修复前的结构
└── src
└── main
├── java
└── resources
├── META-INF
└── spring
解决方案
└── src
└── main
├── java
├── resources
│ └── META-INF
└── spring