2013-04-29 367 views
0

我在一個項目中使用Spring Data JPA(1.3.0.RELEASE)和Spring(3.2.2.RELEASE)並面臨一個奇怪的問題。我正在使用基於xml的配置,如下所述。春季和春季數據JPA不協同工作

<context:annotation-config/> 
<context:component-scan base-package="x.y.z.services"/> 

使用此配置掃描用@Component,@Service和@Named註釋裝飾的類。

<jpa:repositories base-package="x.y.z.repo"/> 

使用此配置掃描所有擴展JpaRepository的接口。這些接口按以下方式注入Service類中。

@Service 
public class UserServiceImpl implements UserService { 

    private UserRepository userRepository; 

    @Inject 
    public void setUserRepository(UserRepository userRepository) { 
      this.userRepository = userRepository; 
    } 

    @Override 
    public List<User> listUsers() { 
      return userRepository.findAll(); 
    } 
} 

此配置按預期工作,沒有任何問題。但是當我添加下面的配置時,我得到了UserRepository的BeanCreationException。

<bean id="securityRealm" class="x.y.z.Realm"> 
     <property name="userService"> 
      <bean class="x.y.z.services.UserServiceImpl"/> 
     </property> 
    </bean> 

而且,這裏是Realm和UserRepository的Java代碼。

public class Realm extends AuthorizingRealm implements IRealm { 

    private UserService userService; 

    @Inject 
    public void setUserService(UserService userService) { 
     this.userService = userService; 
    } 

    @Override 
    protected AuthenticationInfo doGetAuthenticationInfo(
    AuthenticationToken token) throws AuthenticationException { 
     return null; 
    } 

} 

public interface UserRepository extends JpaRepository<User, String> { 

} 

根據上述配置,Spring能夠爲userService創建bean,但無法創建UserRepository bean。

我可以通過掃描x.y.z.Realm並使用@Service註釋裝飾它來獲取此錯誤。但這對我的應用來說將是一個非常大的限制和設計問題。

AFAICT,Spring無法爲UserRepository創建bean,因爲它的實現類不可用並且必須由jpa:repositories配置提供。我可以看到Spring和Spring Data JPA沒有聯合工作。

有人可以幫我解決這個問題。以下是這種懷疑的堆棧追蹤。

2013-04-30 21:44:04.745:INFO:/web:Initializing Spring root WebApplicationContext 
    2013-04-30 21:44:07,009 [ERROR] [main] [org.springframework.web.context.ContextLoader] - Context initialization failed 
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter' defined in class path resource [META-INF/Test-web/security-config.xml]: Cannot resolve reference to bean 'securityManager' while setting bean property 'securityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testShiroRealm': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: x.y.z.core.service.UserService x.y.z.core.security.testShiroRealm.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void x.y.z.core.service.internal.UserServiceImpl.setUserRepository(x.y.z.repo.UserRepository); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [x.y.z.repo.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
     at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329) 
     at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1393) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1134) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) 
     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198) 
     at org.springframework.context.support.AbstractApplicationContext.registerBeantesttProcessors(AbstractApplicationContext.java:753) 
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) 
     at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) 
     at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) 
     at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) 
     at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:764) 
     at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:406) 
     at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:756) 
     at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:242) 
     at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1221) 
     at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:699) 
     at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:454) 
     at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59) 
     at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:90) 
     at org.eclipse.jetty.server.Server.doStart(Server.java:263) 
     at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59) 
     at runjettyrun.Bootstrap.main(Bootstrap.java:80) 
    ... 34 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void x.y.z.core.service.UserServiceImpl.setUserRepository(x.y.z.repo.UserRepository); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [x.y.z.repo.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:601) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) 
    ... 45 more 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [x.y.z.repo.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:986) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:856) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:558) 
    ... 47 more 
+0

你可以顯示Realm和UserRepository類嗎? – 2013-04-29 18:09:09

+0

嗨Sotirios ..編輯與提到的類的問題。 – 2013-04-30 06:16:55

+1

嘗試使用'。你能發佈堆棧跟蹤嗎? – 2013-04-30 12:25:23

回答