spring bean xml没有从AppConfig加载

时间:2017-03-03 20:15:17

标签: spring-mvc

我正在尝试将所有配置从xml移动到基于Annotation。

`web.xml' 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <display-name>Archetype Created Web Application</display-name>

    <welcome-file-list>
        <welcome-file>home.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>sample</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
        </init-param>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>jbr.springmvc.config.AppConfig</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>sample</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

AppConfig.java 文件

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = { "jbr.springmvc" })
public class AppConfig extends WebMvcConfigurerAdapter {
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    System.out.println("inside addResourceHandlers");
    registry.addResourceHandler("/resources/**")
        .addResourceLocations("classpath:/resources/config/");
  }

我在src / main / resources / config文件夹下有user-bean.xml(maven web项目,视图:eclipse中的Navigator)。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="loginController" class="jbr.springmvc.controller.LoginController" />
    <bean id="registrationController" class="jbr.springmvc.controller.RegistrationController" />
    <bean id="userService" class="jbr.springmvc.service.UserServiceImpl" />
    <bean id="userDao" class="jbr.springmvc.dao.UserDaoImpl" />

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="datasource" />
    </bean>

    <bean id="datasource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/myusers" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

</beans>

当我尝试运行该应用程序时,我收到以下错误。

Mar 04, 2017 1:23:32 AM org.springframework.web.context.support.AnnotationConfigWebApplicationContext loadBeanDefinitions
INFO: Successfully resolved class for [jbr.springmvc.config.AppConfig]
Mar 04, 2017 1:23:33 AM org.springframework.web.context.support.AnnotationConfigWebApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'userService': No qualifying bean of type [jbr.springmvc.service.UserService] found for dependency [jbr.springmvc.service.UserService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [jbr.springmvc.service.UserService] found for dependency [jbr.springmvc.service.UserService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Mar 04, 2017 1:23:33 AM org.springframework.web.servlet.DispatcherServlet initServletBean
SEVERE: Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'userService': No qualifying bean of type [jbr.springmvc.service.UserService] found for dependency [jbr.springmvc.service.UserService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [jbr.springmvc.service.UserService] found for dependency [jbr.springmvc.service.UserService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)

有人能指出这里有什么问题吗?

1 个答案:

答案 0 :(得分:1)

使用@ImportResource将Spring XML配置文件导入@Configuration

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = { "jbr.springmvc" })
@ImportResource("classpath:/config/user-bean.xml")
public class AppConfig extends WebMvcConfigurerAdapter {
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    System.out.println("inside addResourceHandlers");
    registry.addResourceHandler("/resources/**")
        .addResourceLocations("classpath:/resources/config/");
  }
}