Java 创建名为“securityConfig”的 bean 时出错:注入自动装配的依赖项失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23262168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error creating bean with name 'securityConfig': Injection of autowired dependencies failed
提问by totoDaryl
I am trying to combine Java-config and xml-config for spring security authentication. But i received an error:
我正在尝试结合 Java-config 和 xml-config 进行 spring 安全身份验证。但我收到一个错误:
Error creating bean with name 'securityConfig': Injection of autowired dependencies failed
创建名为“securityConfig”的 bean 时出错:注入自动装配的依赖项失败
What seems to be the problem with my code? Been googling for answers but have not found any.
我的代码似乎有什么问题?一直在谷歌上搜索答案,但没有找到。
Thanks in advance. hope you can help me.
提前致谢。希望您能够帮助我。
Stack-trace:
堆栈跟踪:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setAuthenticationConfiguration(org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setAuthenticationConfiguration(org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
java-config: SecurityConfig.java
java-config: SecurityConfig.java
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/webapp/resources/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
public void registerGlobalAuthentication(
AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
web.xml
网页.xml
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
i have already declared component-scanning in my servlet-context.xml
我已经在我的 servlet-context.xml 中声明了组件扫描
<context:component-scan base-package="ph.project.p3.conf" />
回答by Anil Satija
You need to add <context:component-scan>
in Spring-config file. Otherwise your application will not scan your package structure to find and register beans within application context.
您需要添加<context:component-scan>
Spring-config 文件。否则您的应用程序将不会扫描您的包结构以在应用程序上下文中查找和注册 bean。
Syntax:<context:component-scan base-package="org.example.<yourapplicationName>"/>
Eg: <context:component-scan base-package="oph.project.p3.conf"/>
语法:<context:component-scan base-package="org.example.<yourapplicationName>"/>
例如:<context:component-scan base-package="oph.project.p3.conf"/>
回答by Yannick Block
You could try adding a @Component
annotation. That way the autowiring should work.
您可以尝试添加@Component
注释。这样自动装配应该可以工作。