0

currently my Spring Boot application uses a JNDI lookup for the datasource via the Web.xml that works fine:

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

Now I wanted to change the xml configuration to a Java based configuration. How can I transfer the above resource ref into Java Code? Here is my configuration class:

@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;
}

I use Spring Boot 1.5.2 with Eclipselink 2.6.4.

I tried several things. Like adding following method into the class:

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

fails to start with an error:

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

Can someone told me how to add the resource ref into my config file? Thanks for the help.

Here is my POM.xml file as well:

<?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>

dokukaefer
  • 51
  • 1
  • 8
  • 1
    It should be enough to just have `spring.datasource.jndi-name=java:comp/env/jdbc/DefaultDB` in your application.properties to configure a JNDI data source. Try that first. – Strelok Apr 03 '17 at 15:32
  • Also you still need the entry in the `web.xml` (or a `web-fragment.xml`) if you are deploying to a container. If you are using an embedded container you shouldn't need a JNDI lookup as that would make things just more complex then needed. – M. Deinum Apr 03 '17 at 19:53
  • I read that one can configure an application without the xml files. Just as annotations in Java Classes. But I don't know my fault. I also tried it with the spring.datasource.jndi-name=java:comp/env/jdbc/DefaultDB - ends with: Name [jdbc/DefaultDB] is not bound in this Context. Unable to find [jdbc]... The platform where I want to deploy my application is SAP HCP (if that helps to solve the problem). – dokukaefer Apr 05 '17 at 12:28
  • I did all advices like in this post but it still does not work http://stackoverflow.com/q/27479263/6769391 – dokukaefer Apr 05 '17 at 12:48

0 Answers0