4

I want to make test in Spring 3.2 + Hibernate 4 + javaconfig

Content of build.gradle is the next:

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 content is the next:

@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 content is the next:

@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 content is the next:

# 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

Product content is the next:

@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 content is the next:

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 content is the next:

@Service
public class ProductService extends AbstractService<Product>
{
    @Autowired
    private ProductDao dao;

    public ProductService()
    {
        super();
    }

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

Result of gradle clean test:

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
4

3 回答 3

2

我与您分享我的 Hibernate java 配置示例。我希望你喜欢它:

前置要求:pom.xml

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.0.6.RELEASE</spring.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>

        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.9.Final</version>
        </dependency>

        <!-- POSTGRESQL -->
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901-1.jdbc4</version>
        </dependency>

    </dependencies>

第 1 步:application.properties

################### DataSource Configuration ##########################
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost/curso_db
jdbc.username=alumno
jdbc.password=alumno

#################### Hibernate Configuration ##########################
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=true

packagesToScan=com.curso.online.model

第 2 步:Config.java

package com.curso.online;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@PropertySources(value = { @PropertySource("classpath:/application.properties") })
@ComponentScan(basePackages = "com.curso.online")
public class Config {

    @Value("${jdbc.driverClassName}")
    private String KEY_DRIVER_CLASS;

    @Value("${jdbc.url}")
    private String KEY_JDBC_URL;

    @Value("${jdbc.username}")
    private String KEY_JDBC_USERNAME;

    @Value("${jdbc.password}")
    private String KEY_JDBC_PASSWORD;

    @Value("${hibernate.dialect}")
    private String KEY_HIBERNATE_DIALECT;

    @Value("${hibernate.show_sql}")
    private String KEY_HBERNATE_SHOW_SQL;

    @Value("${packagesToScan}")
    private String KEY_ENTITIES_PKG;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
        factory.setDataSource(dataSource());
        factory.setPackagesToScan(KEY_ENTITIES_PKG);
        factory.setHibernateProperties(hibernateProperties());
        return factory;
    }

    public Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.dialect", KEY_HIBERNATE_DIALECT);
        properties.setProperty("hibernate.show_sql", KEY_HBERNATE_SHOW_SQL);
        return properties;
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(
            SessionFactory sessionFactory) {
        return new HibernateTransactionManager(sessionFactory);
    }

    @Bean
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(KEY_DRIVER_CLASS);
        dataSource.setUrl(KEY_JDBC_URL);
        dataSource.setUsername(KEY_JDBC_USERNAME);
        dataSource.setPassword(KEY_JDBC_PASSWORD);
        return dataSource;
    }

}
于 2014-09-04T05:37:12.007 回答
2

根据堆栈跟踪:

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:

当我发现这样的问题时,我消除了以下内容:

  1. bean 没有被组件扫描注释或覆盖。你定义了 @ComponentScan(basePackages = {"com.example.persistence"})
    它覆盖了你的包 com.example.persistence.dao.impl.ProductDao
    看这里没问题。
  2. 确保注释正确:ProductDao 已注释 @Repository
    看起来不错。
  3. 确保 com.example.persistence.dao.impl.ProductDao 在类路径中。您正在执行命令gradle clean test,这意味着您的代码将在测试执行之前被编译(因为测试取决于编译任务)。
    如果它编译我猜 ProductDao 在你的类路径中
  4. 确保 JUnit 具有引导 spring 应用程序上下文所需的配置。
    @ContextConfiguration
    类指向您的配置类。loader 属性未定义,因此 spring 将使用默认的 DelegatingSmartContextLoader(您的配置中没有 @WebAppConfiguration 且版本 >3.2)。
    在我看来很好。

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

希望这可以帮助

于 2013-11-18T06:43:12.937 回答
1

在我的测试套件中,我的 Spring Hibernate 项目遇到了类似的问题。

我可以解决这个问题的唯一方法是通过使用它的超类作为它的类型(AbstractService在你的情况下)来自动装配类,然后使用 a@Qualifier告诉 Spring 我指的是哪个子类。在您的情况下,它看起来像这样:

@Qualifier("ProductService")
@Autowired
private AbstractService productService;

这很痛苦,因为在测试函数中,每次使用属于子类的方法时都必须将productService对象转换为。ProductService因此,如果您已经使用其他解决方案解决了这个问题,请与我们分享。

于 2014-05-16T15:45:04.300 回答