gpt4 book ai didi

spring - 如何在 javaconfig 上正确使用 Spring 3.2 + hibernate 4

转载 作者:行者123 更新时间:2023-12-05 01:06:34 28 4
gpt4 key购买 nike

我想在 Spring 3.2 + Hibernate 4 + javaconfig 中进行测试

build.gradle 的内容如下:

buildscript {
repositories {
mavenCentral()
}

dependencies {
classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:0.9.9'
}
}

apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'war'
apply plugin: 'tomcat'

ext.springVersion = '3.2.4.RELEASE'
ext.springMobileVersion = '1.1.0.RELEASE'
ext.thymeleafVersion = '2.0.19'
ext.aspectJVersion = '1.6.9'
ext.cglibVersion = '2.2'
ext.slf4jVersion = '1.6.1'
ext.servletJstlVersion = '1.2'
ext.servletApiVersion = '3.0.1'
ext.servletJspVersion = '2.1'
ext.junitVersion = '4.11'
ext.tomcatVersion = '7.0.42'
ext.pgsqlVersion = '9.1-901.jdbc4'
ext.mockiteoCoreVersion = '1.9.5'
ext.mailVersion = '1.4.7'
ext.dataBindVersion = '2.2.3'
ext.hbVersion = '4.2.7.Final'
ext.guavaVersion = '15.0'
ext.javassistVersion = '3.18.0-GA'

war {
webInf {
include "src/main/webapp/resources/**"
}
}

dependencies {
compile("org.springframework:spring-context:$springVersion") {
exclude module: 'commons-logging'
}
compile "org.springframework:spring-context-support:$springVersion"
compile "org.springframework:spring-web:$springVersion"
compile "org.springframework:spring-webmvc:$springVersion"
compile "org.springframework.mobile:spring-mobile-device:$springMobileVersion"
compile "org.springframework:spring-tx:$springVersion"
compile "org.springframework:spring-orm:$springVersion"

compile "com.fasterxml.jackson.core:jackson-databind:$dataBindVersion"
compile "javax.mail:mail:$mailVersion"
compile "org.thymeleaf:thymeleaf-spring3:$thymeleafVersion"
compile "org.aspectj:aspectjrt:$aspectJVersion"
compile "cglib:cglib-nodep:$cglibVersion"
compile "javax.inject:javax.inject:1"
compile "org.slf4j:slf4j-api:$slf4jVersion"
compile "org.slf4j:jcl-over-slf4j:$slf4jVersion"
compile "org.slf4j:slf4j-log4j12:$slf4jVersion"
compile "javax.servlet:jstl:$servletJstlVersion"
providedCompile("javax.servlet:javax.servlet-api:$servletApiVersion")
providedCompile("javax.servlet.jsp:jsp-api:$servletJspVersion")

// Persistence
compile "postgresql:postgresql:$pgsqlVersion"
compile "org.hibernate:hibernate-core:$hbVersion"
compile "org.hibernate:hibernate-entitymanager:$hbVersion"
compile "org.apache.tomcat:tomcat-dbcp:$tomcatVersion"
compile "org.javassist:javassist:$javassistVersion"

// Tools
compile "com.google.guava:guava:$guavaVersion"

// TEST
testCompile "junit:junit:$junitVersion"
testCompile "org.mockito:mockito-core:$mockiteoCoreVersion"
testCompile "org.springframework:spring-test:$springVersion"
testCompile "org.hamcrest:hamcrest-core:1.3"
testCompile "org.hamcrest:hamcrest-library:1.3"
testCompile "org.apache.commons:commons-lang3:3.1"

// TOMCAT
tomcat("org.apache.tomcat.embed:tomcat-embed-core:$tomcatVersion",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:$tomcatVersion")
tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:$tomcatVersion") {
exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
}
}

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

task wrapper(type: Wrapper) {
gradleVersion = '1.8'
}

PersistenceConfig 内容是下一个:
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:pgsql.properties" })
@ComponentScan(basePackages = {"com.example.persistence"})
public class PersistenceConfig
{
@Autowired
private Environment env;

@Bean
public DataSource dataSource()
{
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getProperty("jdbc.url"));
dataSource.setUsername(env.getProperty("jdbc.user"));
dataSource.setPassword(env.getProperty("jdbc.pass"));
return dataSource;
}

@Bean
public LocalSessionFactoryBean sessionFactory()
{
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "com.example.persistence.domain" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}

@Bean
public HibernateTransactionManager transactionManager()
{
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation()
{
return new PersistenceExceptionTranslationPostProcessor();
}

Properties hibernateProperties() {
return new Properties() {
{
setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
setProperty("hibernate.globally_quoted_identifiers", "true");
}
};
}
}

ProductServiceTest 内容如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {PersistenceConfig.class})
public class ProductServiceTest
{
private static final Logger LOG = LoggerFactory.getLogger(ProductServiceTest.class);

@Autowired
private ProductService productService;

@Autowired
private ApplicationContext applicationContext;

@Test
public final void listApplicationBeans() throws Exception
{
List<String> beans = Arrays.asList(applicationContext.getBeanDefinitionNames());
for (String bean: beans)
{
LOG.info(String.format("--> App Beans [%s]", bean));
}
}
}

persistence.properties 内容是下一个:
# jdbc
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://some_address:5432/some_database
jdbc.user=someuser
jdbc.pass=somepassword

# hibernate
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-drop

产品内容如下:
@Entity
public class Product implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

private String name;

public long getId()
{
return id;
}

public void setId(long id)
{
this.id = id;
}

public String getName()
{
return name;
}

public void setName(String name) {
this.name = name;
}

public Product(String name)
{
setName(name);
}

@Override
public String toString()
{
StringBuilder builder = new StringBuilder();

builder.append(Product.class.getSimpleName()).append(" [\n");
builder.append("id = ").append(id).append("\n");
builder.append("name = ").append(name).append("\n");
builder.append("]");

return builder.toString();
}

}

ProductDao 内容是下一个:
import org.springframework.stereotype.Repository;
import com.example.persistence.dao.common.AbstractDao;
import com.example.persistence.domain.Product;
@Repository
public class ProductDao extends AbstractDao<Product>
{
public ProductDao()
{
super();
setClazz(Product.class);
}
}

ProductService 内容如下:
@Service
public class ProductService extends AbstractService<Product>
{
@Autowired
private ProductDao dao;

public ProductService()
{
super();
}

@Override
protected IOperation<Product> getDao()
{
return dao;
}
}

的结果gradle 清洁测试 :
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:105)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:74)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:80)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:47)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:103)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:66)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.persistence.dao.impl.ProductDao com.example.persistence.service.impl.ProductService.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.persistence.dao.impl.ProductDao] 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.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
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:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:120)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
... 44 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.persistence.dao.impl.ProductDao com.example.persistence.service.impl.ProductService.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.persistence.dao.impl.ProductDao] 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:514)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
... 60 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.persistence.dao.impl.ProductDao] 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:988)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
... 62 more

最佳答案

根据堆栈跟踪:

Could not autowire field: private com.example.persistence.dao.impl.ProductDao
com.example.persistence.service.impl.ProductService.dao; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException:

当我发现这样的问题时,我消除了以下内容:
  • bean 没有被组件扫描注释或覆盖。
    你定义了
    @ComponentScan(basePackages = {"com.example.persistence"})
    它涵盖了您的包 com.example.persistence.dao.impl.ProductDao
    看看这里就好了。
  • 确保注解正确:ProductDao 被注解为@Repository
    看起来还可以。
  • 确保 com.example.persistence.dao.impl.ProductDao 在类路径中。
    您正在执行命令 gradle 清洁测试 ,这意味着您的代码将在测试执行之前被编译(因为测试取决于编译任务)。
    如果它编译,我猜 ProductDao 在你的类路径中
  • 确保 JUnit 具有引导所需的配置
    Spring 应用程序上下文。
    @ContextConfiguration
    类指向您的配置类。
    loader 属性未定义,因此 spring 将使用默认的 DelegatingSmartContextLoader(您的配置中没有 @WebAppConfiguration 且版本 >3.2)。
    对我来说看上去很好。

  • 不幸的是,我找不到任何问题,一切看起来都很好(我希望这份 list 对某人有所帮助)。
    所以我猜想问题出在缺少细节的某个地方;就像您必须在不同的包下对 ProductDao 进行分类,并且您在 ProductService 中使用了错误的包或错误的接口(interface)等。

    希望这可以帮助

    关于spring - 如何在 javaconfig 上正确使用 Spring 3.2 + hibernate 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20039333/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com