Проблема
Я не могу загрузить бины из spring-beans.xml
в дополнение к программной конфигурации.Я создал пользовательский org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
и аннотировал класс следующими аннотациями: @Configuration, @ImportResource('classpath*:spring-beans.xml'), @EnableWebSecurity
.Как вы можете видеть в логах, бины не загружаются.Зачем?Моя цель состоит в том, чтобы иметь полу гибкую конфигурацию.Я хочу настроить некоторые части в коде и некоторые части в файле 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]
Javacode
@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 ...'.