2017-03-03 3 views
0

XML에서 주석 기반으로 모든 구성을 이동하려고합니다.스프링 콩 xml이 AppConfig에서로드되지 않았습니다.

`의 web.xml ' 파일

AppConfig.java 파일 내가 SRC/메인/자원/config 폴더 (받는다는 웹 프로젝트보기에서 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"?> 
<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> 
입니다 네비게이터 : 일식).

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

에 Spring 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