2017-03-03 78 views
0

我正在嘗試將所有配置從xml移動到基於註釋。spring bean xml未從AppConfig加載

`的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 /主/資源/ config文件夾(Maven的web項目,查看user-bean.xml :eclipse中的導航器)。

<?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) 

任何人都可以指出這裏有什麼問題嗎?

+0

您需要用戶bean.xml導入到配置類。 @import – reos

回答

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/"); 
    } 
} 
+0

非常感謝,我用@ImportResource(「classpath *:user-beans.xml」)試過併爲我工作。 –

+0

請標記接受的答案 – mhshimul