Java 创建名为';securityConfig';:自动关联依赖项的注入失败

Java 创建名为';securityConfig';:自动关联依赖项的注入失败,java,xml,spring,spring-mvc,spring-java-config,Java,Xml,Spring,Spring Mvc,Spring Java Config,我正在尝试将Java配置和xml配置结合起来进行spring安全认证。但我收到了一个错误: 创建名为“securityConfig”的bean时出错:自动连线依赖项的注入失败 我的代码似乎有什么问题?一直在谷歌上搜索答案,但没有找到任何答案 提前谢谢。希望你能帮助我 堆栈跟踪: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfig': Injec

我正在尝试将Java配置和xml配置结合起来进行spring安全认证。但我收到了一个错误:

创建名为“securityConfig”的bean时出错:自动连线依赖项的注入失败

我的代码似乎有什么问题?一直在谷歌上搜索答案,但没有找到任何答案

提前谢谢。希望你能帮助我

堆栈跟踪:

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配置: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

<!-- 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>

上下文配置位置
/WEB-INF/spring/root-context.xml
org.springframework.web.context.ContextLoaderListener
appServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/appServlet/servlet-context.xml
1.
appServlet
/
我已经在servlet-context.xml中声明了组件扫描

<context:component-scan base-package="ph.project.p3.conf" />

您需要在Spring配置文件中添加
。否则,应用程序将不会扫描包结构以在应用程序上下文中查找和注册bean

语法:

例如:
您可以尝试添加一个
@组件
注释。这样,自动布线就可以工作。

Hi@AnilSatija,感谢您的回复。正如我在前面的问题中所述,我已经在servlet-context.xml上声明了组件扫描,并且在root-context.xml上也尝试了它。我还尝试过“ph.project.p3”删除.conf,但仍然出现相同的错误,无法自动连接授权配置。您是如何解决此问题的?