java - Spring Boot , JNDI lookup database with application.properties -
i create rest service jersey , spring boot , spring data.
my setup java 1.8 , maven 3.3.9 , spring framework 4.3.0.release , jersey 2.23.1 , spring boot 1.3.6.release , javax.ws.rs-api 2.0.1 , hibernate 5.1.0.final
i try build datasource via defining in context.xml
, referencing in application.properties
. see method permdbjpadatasource()
. seems entry spring.datasource.jndi-name=jdbc/permdb
ignored. bean named permdbjpadatasourcefail
created, properties null (database url, db user etc...). can see file application.properties
evaluated, because of spring.main.banner-mode=off
.
the method jndidatasource()
works fine, , can build application way. know if i'm doing wrong or if it's bug.
these sources:
├───java │ └───com │ └───pkg │ └───permdb │ └───service │ │ permdbserviceapplication.java │ │ │ ├───services │ │ permdbowsservice.java │ │ │ └───springboot │ permdbjerseyconfig.java │ permdbservicespringboot.java │ ├───resources │ │ application.properties │ │ log4j2.xml │ │ │ └───meta-inf │ context.xml │ └───webapp
permdbserviceapplication.java
package com.pkg.permdb.service; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.boot.builder.springapplicationbuilder; import org.springframework.boot.context.web.springbootservletinitializer; @springbootapplication public class permdbserviceapplication extends springbootservletinitializer { //private static logger logger = logmanager.getlogger(permdbserviceapplication.class.getname()); @override protected springapplicationbuilder configure(springapplicationbuilder application) { return application.sources(permdbserviceapplication.class); } public static void main(string[] args) { springapplication.run(permdbserviceapplication.class, args); } }
permdbservicespringboot.java
package com.pkg.permdb.service.springboot; import java.io.ioexception; import java.util.hashtable; import java.util.map; import javax.naming.namingexception; import javax.persistence.sharedcachemode; import javax.persistence.validationmode; import javax.sql.datasource; import org.apache.logging.log4j.logmanager; import org.apache.logging.log4j.logger; import org.springframework.context.annotation.*; import org.springframework.jndi.jndiobjectfactorybean; import org.springframework.orm.jpa.jpatransactionmanager; import org.springframework.orm.jpa.localcontainerentitymanagerfactorybean; import org.springframework.orm.jpa.vendor.hibernatejpavendoradapter; import org.springframework.transaction.platformtransactionmanager; import org.springframework.transaction.annotation.enabletransactionmanagement; @configuration @enabletransactionmanagement(mode = advicemode.proxy, proxytargetclass = false) @componentscan(basepackages = "com.pkg.permdb.repositories") public class permdbservicespringboot { private static logger logger = logmanager.getlogger(permdbservicespringboot.class.getname()); @bean(name = "permdbentitymanager") public localcontainerentitymanagerfactorybean permdbentitymanagerfactorybean() throws ioexception { //properties dmprops = dmproperties(); map<string, object> propsmap = new hashtable<>(); logger.debug("erzeuge bean {} ...", localcontainerentitymanagerfactorybean.class.getname()); logger.debug("datasource: {}", jndidatasource()); //propsmap.put("javax.persistence.schema-generation.database.action", // dmprops.getproperty("dm.hibernatedbschemageneration")); hibernatejpavendoradapter hibadapter = new hibernatejpavendoradapter(); //hibadapter.setdatabaseplatform(dmprops.getproperty("dm.hibernatedbdialect")); hibadapter.setdatabaseplatform("org.hibernate.dialect.postgresql9dialect"); localcontainerentitymanagerfactorybean factorybean = new localcontainerentitymanagerfactorybean(); factorybean.setjpavendoradapter(hibadapter); factorybean.setdatasource(jndidatasource()); //factorybean.setpackagestoscan(dmprops.getproperty("dm.userdbmodel")); factorybean.setpackagestoscan("com.pkg.permdb.entities"); factorybean.setsharedcachemode(sharedcachemode.enable_selective); factorybean.setvalidationmode(validationmode.none); factorybean.setpersistenceunitname("permdb"); factorybean.setjpapropertymap(propsmap); logger.debug("bean erzeugt: {}", factorybean); return factorybean; } @bean(name = "permdbjpadatasource") public datasource permdbjpadatasource() throws datasourcelookupfailureexception, ioexception { /* ############################## when these lines executed, entry spring.datasource.jndi-name application.properties not evaluated ############################## */ logger.debug("erzeuge bean {} ...", datasource.class.getname()); jndidatasourcelookup lookup = new jndidatasourcelookup(); string datasourcename = "java:comp/env/jdbc/permdb"; datasource datasource = lookup.getdatasource(datasourcename); logger.debug("bean erzeugt: {}", datasource); return datasource; } @bean(name ="permdbjpadatasourcefail") public datasource jndidatasource() { jndiobjectfactorybean bean = new jndiobjectfactorybean(); bean.setjndiname("java:comp/env/jdbc/permdb"); bean.setproxyinterface(datasource.class); bean.setlookuponstartup(false); try { bean.afterpropertiesset(); } catch (namingexception e) { logger.error("fehler: ", e); } return (datasource) bean.getobject(); } @bean(name = "permdbtransactionmanager") //@bean public platformtransactionmanager transactionmanager() throws ioexception { return new jpatransactionmanager(permdbentitymanagerfactorybean().getobject()); } }
permdbjerseyconfig.java
package com.pkg.permdb.service.springboot; import org.apache.logging.log4j.logmanager; import org.apache.logging.log4j.logger; import org.glassfish.jersey.server.resourceconfig; import org.springframework.stereotype.component; import com.pkg.permdb.service.services.permdbowsservice; @component public class permdbjerseyconfig extends resourceconfig { private static logger logger = logmanager.getlogger(permdbjerseyconfig.class.getname()); public permdbjerseyconfig() { logger.debug("register services"); register(permdbowsservice.class); } }
application.properties
spring.datasource.jndi-name=jdbc/permdb spring.datasource.driver-class-name=org.postgresql.driver spring.main.banner-mode=off #spring.jmx.enabled=false
context.xml
<?xml version='1.0' encoding='utf-8'?> <context> <resource name="jdbc/permdb" auth="container" type="javax.sql.datasource" driverclassname="org.postgresql.driver" url="jdbc:postgresql://my.server.de:5432/oaktree" username="uhtred" password="password" maxtotal="50" maxidle="10" maxwaitmillis="-1"/> </context>
pom.xml module permdbservice, parent see below
<?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"> <parent> <artifactid>permdb</artifactid> <groupid>com.permdb</groupid> <version>0.2.2-snapshot</version> </parent> <modelversion>4.0.0</modelversion> <artifactid>permdbservice</artifactid> <version>0.1.2-snapshot</version> <packaging>war</packaging> <properties> <jersey.version>2.23.1</jersey.version> </properties> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> <dependencymanagement> <dependencies> <dependency> <groupid>org.glassfish.jersey</groupid> <artifactid>jersey-bom</artifactid> <version>${jersey.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <!-- import dependency management spring boot --> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-dependencies</artifactid> <version>1.3.6.release</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement> <dependencies> <dependency> <groupid>com.permdb</groupid> <artifactid>permdbrepositories</artifactid> <version>0.1.0-snapshot</version> <exclusions> <exclusion> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> </exclusion> </exclusions> </dependency> <dependency> <groupid>javax.ws.rs</groupid> <artifactid>javax.ws.rs-api</artifactid> <version>2.0.1</version> <scope>compile</scope> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-jersey</artifactid> <exclusions> <exclusion> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-tomcat</artifactid> </exclusion> </exclusions> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-jpa</artifactid> </dependency> <dependency> <groupid>org.glassfish.jersey.containers</groupid> <artifactid>jersey-container-servlet-core</artifactid> </dependency> <dependency> <groupid>org.postgresql</groupid> <artifactid>postgresql</artifactid> <!--<version>9.4.1208</version>--> </dependency> <!-- ################### logging ####################--> <dependency> <groupid>org.apache.logging.log4j</groupid> <artifactid>log4j-api</artifactid> </dependency> <dependency> <groupid>org.apache.logging.log4j</groupid> <artifactid>log4j-core</artifactid> </dependency> <!-- <dependency> <!– dies wird benötigt, damit das hibernate-logging über log4j2 konfigurierbar ist–> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> </dependency> --> <dependency> <groupid>org.apache.logging.log4j.adapters</groupid> <artifactid>log4j-1.2-api</artifactid> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-api</artifactid> </dependency> </dependencies> </project>
parent 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>com.permdb</groupid> <artifactid>permdb</artifactid> <packaging>pom</packaging> <version>0.2.2-snapshot</version> <parent> <groupid>com</groupid> <artifactid>maventoolparent</artifactid> <version>1.0.0</version> </parent> <modules> <module>permdbdata</module> <module>permdbservice</module> <module>permdbrepositories</module> </modules> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <!-- zum timestamp siehe http://rterp.wordpress.com/2012/03/16/stamping-version-number-and-build-time-in-properties-file-with-maven/ --> <build.timestamp>${maven.build.timestamp}</build.timestamp> <maven.build.timestamp.format>dd.mm.yyyy hh:mm</maven.build.timestamp.format> <log4j.version>2.6.1</log4j.version> <hibernate.version>5.1.0.final</hibernate.version> <spring.version>4.3.0.release</spring.version> </properties> <profiles> <profile> <id>permdb-local</id> <activation> <activebydefault>true</activebydefault> </activation> <properties> </properties> </profile> <profile> <id>permdb-ref</id> <activation> <activebydefault>false</activebydefault> </activation> <properties> </properties> </profile> </profiles> <build> <testresources> <testresource> <directory>src/test/resources</directory> <filtering>true</filtering> </testresource> </testresources> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <excludes> <exclude>meta-inf/context.xml</exclude> </excludes> </resource> </resources> <pluginmanagement> <plugins> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-war-plugin</artifactid> <version>2.5</version> <configuration> <failonmissingwebxml>false</failonmissingwebxml> <!--<filteringdeploymentdescriptors>true</filteringdeploymentdescriptors>--> <webresources> <resource> <directory>src/main/webapp</directory> <includes> <include>**/*.html</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources/meta-inf</directory> <targetpath>/meta-inf</targetpath> <includes> <include>context.xml</include> </includes> <filtering>true</filtering> </resource> </webresources> </configuration> </plugin> </plugins> </pluginmanagement> <plugins> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <compilerargument>-proc:none</compilerargument> </configuration> </plugin> <plugin> <inherited>true</inherited> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-enforcer-plugin</artifactid> <version>1.3</version> <executions> <execution> <id>enforce-maven-3</id> <goals> <goal>enforce</goal> </goals> <configuration> <rules> <requiremavenversion> <version>3.3.9</version> </requiremavenversion> </rules> <fail>true</fail> </configuration> </execution> </executions> </plugin> </plugins> </build> <dependencymanagement> <dependencies> <dependency> <groupid>org.apache.logging.log4j</groupid> <artifactid>log4j-api</artifactid> <version>${log4j.version}</version> <scope>compile</scope> </dependency> <dependency> <groupid>org.apache.logging.log4j</groupid> <artifactid>log4j-core</artifactid> <version>${log4j.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupid>org.apache.logging.log4j.adapters</groupid> <artifactid>log4j-1.2-api</artifactid> <version>latest</version> </dependency> <dependency> <!-- dies wird benötigt, damit das hibernate-logging über log4j2 konfigurierbar ist--> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> <version>1.7.21</version> <exclusions> <exclusion> <groupid>log4j</groupid> <artifactid>log4j</artifactid> </exclusion> </exclusions> </dependency> <dependency> <groupid>org.apache.commons</groupid> <artifactid>commons-lang3</artifactid> <version>3.4</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-framework-bom</artifactid> <version>${spring.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupid>org.postgresql</groupid> <artifactid>postgresql</artifactid> <version>9.4.1208</version> <scope>provided</scope> </dependency> <dependency> <groupid>com.fasterxml.uuid</groupid> <artifactid>java-uuid-generator</artifactid> <version>3.1.4</version> </dependency> <dependency> <groupid>com.ows</groupid> <artifactid>owsmisc</artifactid> <version>1.0.0</version> </dependency> <!-- ################# test ################# --> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </dependencymanagement> </project>
updtae
this output when start applcation:
2016-07-28 11:31:03.802 info 5636 --- [on(3)-127.0.0.1] d.lgn.permdb.service.permdbserviceapplication : starting permdbserviceapplication on gll-h3-w7p139 pid 5636 (d:\catalina-base-idea\webapps\permdb\web-inf\classes\com\pkg\permdb\service\permdbserviceapplication.class started ulrich.knaack in d:\programme\apache-tomcat-8.0.15\bin) 2016-07-28 11:31:03.818 info 5636 --- [on(3)-127.0.0.1] d.lgn.permdb.service.permdbserviceapplication : no active profile set, falling default profiles: default 2016-07-28 11:31:03.880 info 5636 --- [on(3)-127.0.0.1] ationconfigembeddedwebapplicationcontext : refreshing org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@410bf06: startup date [thu jul 28 11:31:03 cest 2016]; root of context hierarchy 2016-07-28 11:31:05.175 info 5636 --- [on(3)-127.0.0.1] f.a.autowiredannotationbeanpostprocessor : jsr-330 'javax.inject.inject' annotation found , supported autowiring 2016-07-28 11:31:05.284 info 5636 --- [on(3)-127.0.0.1] trationdelegate$beanpostprocessorchecker : bean 'org.springframework.transaction.annotation.proxytransactionmanagementconfiguration' of type [class org.springframework.transaction.annotation.proxytransactionmanagementconfiguration$$enhancerbyspringcglib$$fbaaf65b] not eligible getting processed beanpostprocessors (for example: not eligible auto-proxying) 2016-07-28 11:31:05.331 info 5636 --- [on(3)-127.0.0.1] o.s.web.context.contextloader : root webapplicationcontext: initialization completed in 1451 ms 11:31:05.627 [rmi tcp connection(3)-127.0.0.1] debug permdbjerseyconfig - register services 2016-07-28 11:31:05.830 info 5636 --- [on(3)-127.0.0.1] b.a.w.tomcatwebsocketcontainercustomizer : nonembeddedservletcontainerfactory detected. websockets support should native not problem. 2016-07-28 11:31:05.861 info 5636 --- [on(3)-127.0.0.1] o.s.b.c.embedded.filterregistrationbean : mapping filter: 'errorpagefilter' to: [/*] 2016-07-28 11:31:05.861 info 5636 --- [on(3)-127.0.0.1] o.s.b.c.embedded.filterregistrationbean : mapping filter: 'characterencodingfilter' to: [/*] 2016-07-28 11:31:05.861 info 5636 --- [on(3)-127.0.0.1] o.s.b.c.embedded.filterregistrationbean : mapping filter: 'requestcontextfilter' to: [/*] 2016-07-28 11:31:05.861 info 5636 --- [on(3)-127.0.0.1] o.s.b.c.e.servletregistrationbean : mapping servlet: 'com.pkg.permdb.service.springboot.permdbjerseyconfig' [/*] 11:31:05.877 [rmi tcp connection(3)-127.0.0.1] debug permdbservicespringboot - erzeuge bean org.springframework.orm.jpa.localcontainerentitymanagerfactorybean ... 11:31:05.877 [rmi tcp connection(3)-127.0.0.1] debug permdbservicespringboot - erzeuge bean javax.sql.datasource ... 11:31:05.877 [rmi tcp connection(3)-127.0.0.1] debug permdbservicespringboot - bean erzeugt: org.apache.tomcat.dbcp.dbcp2.basicdatasource@49f798a4 11:31:05.986 [rmi tcp connection(3)-127.0.0.1] debug permdbservicespringboot - datasource: org.apache.tomcat.dbcp.dbcp2.basicdatasource@49f798a4 11:31:06.017 [rmi tcp connection(3)-127.0.0.1] debug permdbservicespringboot - bean erzeugt: org.springframework.orm.jpa.localcontainerentitymanagerfactorybean@439cc9b9 2016-07-28 11:31:06.033 info 5636 --- [on(3)-127.0.0.1] j.localcontainerentitymanagerfactorybean : building jpa container entitymanagerfactory persistence unit 'permdb' 2016-07-28 11:31:06.049 info 5636 --- [on(3)-127.0.0.1] o.hibernate.jpa.internal.util.loghelper : hhh000204: processing persistenceunitinfo [ name: permdb ...] 2016-07-28 11:31:06.205 info 5636 --- [on(3)-127.0.0.1] org.hibernate.version : hhh000412: hibernate core {4.3.11.final} 2016-07-28 11:31:06.205 info 5636 --- [on(3)-127.0.0.1] org.hibernate.cfg.environment : hhh000206: hibernate.properties not found 2016-07-28 11:31:06.205 info 5636 --- [on(3)-127.0.0.1] org.hibernate.cfg.environment : hhh000021: bytecode provider name : javassist 2016-07-28 11:31:06.501 info 5636 --- [on(3)-127.0.0.1] o.hibernate.annotations.common.version : hcann000001: hibernate commons annotations {4.0.5.final} 2016-07-28 11:31:06.891 info 5636 --- [on(3)-127.0.0.1] org.hibernate.dialect.dialect : hhh000400: using dialect: org.hibernate.dialect.postgresql9dialect 2016-07-28 11:31:06.906 info 5636 --- [on(3)-127.0.0.1] o.h.e.jdbc.internal.lobcreatorbuilder : hhh000424: disabling contextual lob creation createclob() method threw error : java.lang.reflect.invocationtargetexception 2016-07-28 11:31:07.187 info 5636 --- [on(3)-127.0.0.1] o.h.h.i.ast.astquerytranslatorfactory : hhh000397: using astquerytranslatorfactory 2016-07-28 11:31:08.388 info 5636 --- [on(3)-127.0.0.1] o.s.j.e.a.annotationmbeanexporter : registering beans jmx exposure on startup 2016-07-28 11:31:08.404 info 5636 --- [on(3)-127.0.0.1] o.s.j.e.a.annotationmbeanexporter : bean name 'permdbjpadatasource' has been autodetected jmx exposure 2016-07-28 11:31:08.404 info 5636 --- [on(3)-127.0.0.1] o.s.j.e.a.annotationmbeanexporter : located mbean 'permdbjpadatasource': registering jmx server mbean [org.apache.tomcat.dbcp.dbcp2:name=permdbjpadatasource,type=basicdatasource] 2016-07-28 11:31:08.420 error 5636 --- [on(3)-127.0.0.1] o.s.boot.springapplication : application startup failed org.springframework.jmx.export.unabletoregistermbeanexception: unable register mbean [org.apache.tomcat.dbcp.dbcp2.basicdatasource@49f798a4] key 'permdbjpadatasource'; nested exception javax.management.instancealreadyexistsexception: catalina:type=datasource,host=localhost,context=/permdb,class=javax.sql.datasource,name="jdbc/permdb" @ org.springframework.jmx.export.mbeanexporter.registerbeannameorinstance(mbeanexporter.java:625) ~[spring-context-4.2.7.release.jar:4.2.7.release] @ org.springframework.jmx.export.mbeanexporter.registerbeans(mbeanexporter.java:550) ~[spring-context-4.2.7.release.jar:4.2.7.release] @ org.springframework.jmx.export.mbeanexporter.aftersingletonsinstantiated(mbeanexporter.java:432) ~[spring-context-4.2.7.release.jar:4.2.7.release]
Comments
Post a Comment