使用带有纯Java配置的Spring Data JPA时,无法注入存储库

时间:2017-08-04 14:37:27

标签: java spring spring-mvc spring-data-jpa

我正在学习使用Spring Data来访问我的MySQL数据库。由于我是Spring新手并且没有使用Spring Boot,所以我找不到有用的教程。

这是我的build.gradle

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'org.akhikhl.gretty:gretty:+'
    }
}

plugins {
    id('io.spring.dependency-management') version('1.0.3.RELEASE')
    id 'io.franzbecker.gradle-lombok' version '1.10'
}

group 'com.perqin'
version '1.0.0'

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'org.akhikhl.gretty'
apply plugin: 'io.spring.dependency-management'

dependencyManagement {
    imports {
        mavenBom 'org.springframework.data:spring-data-releasetrain:Ingalls-SR6'
    }
}

sourceCompatibility = 1.8

repositories {
    jcenter()
    maven { url 'https://repo.spring.io/libs-release' }
}

dependencies {
    compile 'com.fasterxml.jackson.core:jackson-databind:2.9.0'
    compile 'mysql:mysql-connector-java:8.0.7-dmr'
    compile 'org.hibernate:hibernate-core:5.2.10.Final'
    compile 'org.springframework.data:spring-data-jpa:1.11.6.RELEASE'
    compile 'org.springframework.data:spring-data-rest-webmvc:2.6.6.RELEASE'
    compile 'org.springframework.security:spring-security-config:4.2.3.RELEASE'
    compile 'org.springframework.security:spring-security-web:4.2.3.RELEASE'
    compile 'org.springframework:spring-webmvc:4.3.10.RELEASE'
    testCompile group: 'junit', name: 'junit', version: '4.11'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

这是我的持久性配置:

@Configuration
@EnableJpaRepositories("com.perqin.focus.spring.domain.repositories")
@EnableTransactionManagement
public class PersistenceJpaConfig {
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(Driver.class.getName());
        dataSource.setUsername("perqin");
        dataSource.setPassword("dev123456");
        dataSource.setUrl("jdbc:mysql://localhost:3306/focus_spring?useUnicode=true&characterEncoding=UTF-8");
        return dataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabase(Database.MYSQL);
        vendorAdapter.setGenerateDdl(true);
        vendorAdapter.setShowSql(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan(EntityScanned.class.getPackage().toString());
        factory.setDataSource(dataSource());
        factory.setJpaProperties(jpaProperties());

        return factory;
    }

    @Bean
    PlatformTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
        return transactionManager;
    }

    private Properties jpaProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");  // MySQL5InnoDBDialect   MySQLMyISAMDialect
        properties.put("hibernate.show_sql", "true");
        return properties;
    }
}

    private Properties jpaProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");  // MySQL5InnoDBDialect   MySQLMyISAMDialect
        properties.put("hibernate.show_sql", "true");
        return properties;
    }
}

存储库:

@Repository
public interface UsersRepository extends CrudRepository<User, Long> {
}

但是在启动应用程序后,我得到了这样的输出:

10:27:07 PM: Executing external task 'appRun'...
:prepareInplaceWebAppFolder
:createInplaceWebAppFolder UP-TO-DATE
:compileJava
:processResources
:classes
:prepareInplaceWebAppClasses
:prepareInplaceWebApp
:appRun
22:27:10 INFO  2 Spring WebApplicationInitializers detected on classpath
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
22:27:10 INFO  Initializing Spring root WebApplicationContext
22:27:11 WARN  Failed startup of context o.a.g.JettyWebAppContext@db57326{/focus.spring,file:/home/perqin/workspaces/Focus/Focus-Server-Spring/build/inplaceWebapp/,STARTING}
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usersController': Unsatisfied dependency expressed through field 'usersRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.perqin.focus.spring.domain.repositories.UsersRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[na:na]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[na:na]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[na:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[na:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[na:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[na:na]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[na:na]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[na:na]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[na:na]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[na:na]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[na:na]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[na:na]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[na:na]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443) ~[na:na]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325) ~[na:na]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) ~[na:na]
    at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:800) ~[jetty-server-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:444) ~[jetty-servlet-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:791) ~[jetty-server-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:294) ~[jetty-servlet-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1349) ~[jetty-webapp-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1342) ~[jetty-webapp-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:741) ~[jetty-server-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:505) ~[jetty-webapp-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.akhikhl.gretty.JettyWebAppContext.super$10$doStart(JettyWebAppContext.groovy) [gretty-runner-jetty9-2.0.0.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) [groovy-2.4.11.jar:2.4.11]
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) [groovy-2.4.11.jar:2.4.11]
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213) [groovy-2.4.11.jar:2.4.11]
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuperN(ScriptBytecodeAdapter.java:132) [groovy-2.4.11.jar:2.4.11]
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuper0(ScriptBytecodeAdapter.java:152) [groovy-2.4.11.jar:2.4.11]
    at org.akhikhl.gretty.JettyWebAppContext.doStart(JettyWebAppContext.groovy:44) [gretty-runner-jetty9-2.0.0.jar:na]
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) [jetty-util-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132) [jetty-util-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114) [jetty-util-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61) [jetty-server-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:163) [jetty-server-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) [jetty-util-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132) [jetty-util-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.server.Server.start(Server.java:387) [jetty-server-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114) [jetty-util-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61) [jetty-server-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.server.Server.doStart(Server.java:354) [jetty-server-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) [jetty-util-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.eclipse.jetty.util.component.LifeCycle$start$0.call(Unknown Source) [jetty-util-9.2.22.v20170606.jar:9.2.22.v20170606]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) [groovy-2.4.11.jar:2.4.11]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) [groovy-2.4.11.jar:2.4.11]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117) [groovy-2.4.11.jar:2.4.11]
    at org.akhikhl.gretty.JettyServerManager.startServer(JettyServerManager.groovy:45) [gretty-runner-jetty-2.0.0.jar:na]
    at org.akhikhl.gretty.ServerManager$startServer$0.call(Unknown Source) [gretty-runner-2.0.0.jar:na]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) [groovy-2.4.11.jar:2.4.11]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) [groovy-2.4.11.jar:2.4.11]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125) [groovy-2.4.11.jar:2.4.11]
    at org.akhikhl.gretty.Runner.run(Runner.groovy:117) [gretty-runner-2.0.0.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210) [groovy-2.4.11.jar:2.4.11]
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.call(PogoMetaMethodSite.java:71) [groovy-2.4.11.jar:2.4.11]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) [groovy-2.4.11.jar:2.4.11]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) [groovy-2.4.11.jar:2.4.11]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117) [groovy-2.4.11.jar:2.4.11]
    at org.akhikhl.gretty.Runner.main(Runner.groovy:44) [gretty-runner-2.0.0.jar:na]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.perqin.focus.spring.domain.repositories.UsersRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493) ~[na:na]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[na:na]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[na:na]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[na:na]
    ... 66 common frames omitted
22:27:11 INFO  Jetty 9.2.22.v20170606 started and listening on port 8080
22:27:11 INFO  focus.spring runs at:
22:27:11 INFO    http://localhost:8080/focus.spring
Press any key to stop the server.

我已经提到了许多示例代码,但我找不到任何重要的差异。

供参考,这里是源代码:https://github.com/perqin/Focus-Server-Spring

0 个答案:

没有答案