2016-05-16 58 views
-1

我想部署一个使用持久性API的弹簧应用程序,但是我的配置有一些问题。我不知道为什么DAOS都没有找到...... 也许有与上下文的问题..如何使用JPA配置Spring应用程序?

这是道:

@Component 
public interface AccountDAO extends JpaRepository<Account, String> { 
} 

aplication方面:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <context:component-scan base-package="ro.academy.service" /> 
    <context:component-scan base-package="ro.academy.model.daos" /> 
    <context:annotation-config/> 
</beans> 

另一个配置上下文:

@Configuration 
@EnableTransactionManagement 
@EnableJpaRepositories(basePackages = { 
     "ro.academy.model.daos" 
}) 
public class PersistenceContext { 
    @Bean(destroyMethod = "close") 
    DataSource dataSource(Environment env) { 
     BasicDataSource dataSourceConfig = new BasicDataSource(); 
     dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver")); 
     dataSourceConfig.setUrl(env.getRequiredProperty("db.url")); 
     dataSourceConfig.setUsername(env.getRequiredProperty("db.username")); 
     dataSourceConfig.setPassword(env.getRequiredProperty("db.password")); 

     return dataSourceConfig; 
    } 
} 

shoud我在web xml中配置persistanc上下文?这是Web XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
     version="3.1"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/applicationContext.xml 
     </param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

而且错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ro.academy.model.daos.AccountDAO ro.academy.service.MyService.accountsDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ro.academy.model.daos.AccountDAO] found for dependency: 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:573) 
     at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 
     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) 
     ... 58 more 
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ro.academy.model.daos.AccountDAO] found for dependency: 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) 
     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) 
     ... 60 more 

回答

1

我不把组件在Repository类。相反,从SpringBoot原型,我一直在创造这些类,像这样:

@Component("countryService") 
@Transactional 
public class CountryServiceImpl implements CountryService { 
    private CountryRepository countryRepository; 
    private RegionRepository regionRepository; 

    @Autowired 
    public CountryServiceImpl(CountryRepository countryRepository, RegionRepository regionRepository) { 
     this.countryRepository = countryRepository; 
     this.regionRepository = regionRepository; 
    } 

资源库类没有特别的注释:

public interface CountryRepository extends PagingAndSortingRepository<Country, Long> { 

} 

按照这个模板,它应该为你工作。否则这个答案有帮助的意见:

Unable to Autowire ServiceImpl Class Bean with Service Class Object

相关问题