Вопрос

Я хочу сделать тест весной 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");
            }
        };
    }
}

Контент для тестирования продукта - это следующий:

@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 clean на чистоту:

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
Это было полезно?

Решение

Я наткнулся на подобную проблему с моим весенним гибернатом проекта в моих тестовых люксах.

Единственный способ решить, что проблема заключалась в том, чтобы автоматизировать класс, используя его супер класс в качестве типа (AbstractService в вашем случае), а затем используйте @Qualifier, чтобы рассказать пружину, к которому я имею в виду.Это будет выглядеть что-то подобное в вашем случае:

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

Это больно, как тогда в функции тестирования вы должны лишить объект productService для GeneracodicCode каждый раз, когда вы используете метод, который принадлежит ребенку.Так что, если вы уже решили это, используя другое решение, пожалуйста, поделитесь этим с нами.

Другие советы

Согласно stacktrace:

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. Компонент не снабжен комментариями или не охвачен программой component scan.Вы определили @ComponentScan(базовые пакеты = {"com.example.persistence"})
    Он охватывает ваш пакет com.example.persistence.dao.impl.ProductDao
    Здесь все в порядке.
  2. Убедитесь в правильности аннотаций:ProductDao имеет аннотацию @Repository
    Выглядит нормально.
  3. Убедитесь, что com.example.persistence.dao.impl.ProductDao находится в пути к классу.Вы выполняете команду тест gradle clean на чистоту, что означает, что ваш код будет скомпилирован перед выполнением теста (поскольку тест зависит от задачи компиляции).
    Если он компилируется, я предполагаю, что ProductDao находится в вашем пути к классу
  4. Убедитесь, что JUnit имеет необходимую конфигурацию для начальной загрузки контекст приложения Spring.
    @ContextConfiguration @Контекстная конфигурация
    класс указывает на ваш класс конфигураций.атрибут loader не был определен, поэтому spring будет использовать DelegatingSmartContextLoader по умолчанию (в вашей конфигурации нет @WebAppConfiguration и версии >3.2).
    По-моему, выглядит неплохо.

К сожалению, я не могу найти ни одной проблемы, и все выглядит нормально (я надеюсь, что этот контрольный список кому-нибудь поможет).
Так что я бы предположил, что проблема где-то в недостающих деталях;например, у вас есть класс ProductDao в разных пакетах, и вы используете неправильный в ProductService или неправильный интерфейс и т.д.

Надеюсь, это поможет

Я делюсь с вами пример моей настройки 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;
    }

}
.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top