Spring Boot,JPA(Eclipselink)JDNI DataSource as JavaConfig

时间:2017-04-03 15:11:24

标签: java spring jpa eclipselink

目前我的Spring Boot应用程序通过Web.xml对数据源使用JNDI查找,工作正常:

<resource-ref>
    <res-ref-name>jdbc/DefaultDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
</resource-ref> 

现在我想将xml配置更改为基于Java的配置。如何将上述资源引用转换为Java代码?这是我的配置类:

@Configuration
@EnableJpaRepositories
@EnableAutoConfiguration
public class NeoConfig extends JpaBaseConfiguration {

protected NeoConfig(DataSource dataSource, JpaProperties properties,
        ObjectProvider<JtaTransactionManager> jtaTransactionManager,
        ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
    super(dataSource, properties, jtaTransactionManager, transactionManagerCustomizers);
} 

@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
    return new EclipseLinkJpaVendorAdapter();
}

@Override
protected Map<String, Object> getVendorProperties() {
    Map<String, Object> properties = new HashMap<>();
    properties.put("eclipselink.ddl-generation", "create-tables");
    properties.put("eclipselink.logging.level", "ALL");
    properties.put("eclipselink.weaving", "false");
    return properties;
}

我使用Spring Boot 1.5.2和Eclipselink 2.6.4。

我尝试了几件事。就像在课堂上添加以下方法一样:

@Bean(destroyMethod="")
public DataSource dataSource() {
    JndiDataSourceLookup dslook = new JndiDataSourceLookup();
    dslook.setResourceRef(true);
    DataSource ds = dslook.getDataSource("java:comp/env/jdbc/DefaultDB");
    return ds;
}

无法启动错误:

The dependencies of some of the beans in the application context form a cycle

有人能告诉我如何将资源引用添加到我的配置文件中吗?谢谢你的帮助。

这也是我的POM.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.info</groupId>
<artifactId>z_app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>Z_Application</name>
<description>z app</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <java.version>1.8</java.version>
    <eclipselink.version>2.6.4</eclipselink.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>hibernate-entitymanager</artifactId>
                <groupId>org.hibernate</groupId>
            </exclusion>
             <exclusion>
                <artifactId>hibernate-core</artifactId>
                <groupId>org.hibernate</groupId>
            </exclusion> 
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.6.4</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

0 个答案:

没有答案