2016-05-16 60 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

相關問題