2017-03-03 1 views
0

すべての設定をxmlからAnnotationベースに移動しようとしています。Spring Beans 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ファイルは、私はSRC /メイン/リソース/ configフォルダ(MavenのWebプロジェクト、ビューの下user-bean.xmlを持って

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

です:日食のナビゲーター)。

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

に春のXML設定ファイルをインポートします。 @import – reos

答えて

1

使用@ImportResourceあなたが設定クラスにユーザーbean.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

関連する問題