java - Configuring data source URL in Spring without XML -
i have simple web mvc application using spring boot communicates database; db h2 , has been in memory until now. want change that, , use jdbc:h2:file:...
url.
up until now, have not needed add xml configure application, , i'd prefer stay way if possible. can't figure out how specify different jdbc url. obtained , inspected data source passing @bean
method:
org.apache.tomcat.jdbc.pool.datasource@745e6f01{connectionpool[ defaultautocommit=null; defaultreadonly=null; defaulttransactionisolation=-1; defaultcatalog=null; driverclassname=org.h2.driver; maxactive=100; maxidle=100; minidle=10; initialsize=10; maxwait=30000; testonborrow=false; testonreturn=false; timebetweenevictionrunsmillis=5000; numtestsperevictionrun=0; minevictableidletimemillis=60000; testwhileidle=false; testonconnect=false; password=********; url=jdbc:h2:mem:testdb;db_close_delay=-1;db_close_on_exit=false; username=sa; validationquery=null; validationquerytimeout=-1; validatorclassname=null; validationinterval=30000; accesstounderlyingconnectionallowed=true; removeabandoned=false; removeabandonedtimeout=60; logabandoned=false; connectionproperties=null; initsql=null; jdbcinterceptors=null; jmxenabled=true; fairqueue=true; useequals=true; abandonwhenpercentagefull=0; maxage=0; uselock=false; datasource=null; datasourcejndi=null; suspecttimeout=0; alternateusernameallowed=false; commitonreturn=false; rollbackonreturn=false; usedisposableconnectionfacade=true; logvalidationerrors=false; propagateinterruptstate=false; ignoreexceptiononpreload=false; }
(newlines mine)
the setup of bean seems rather intricate, want interfere little possible - replace default jdbc url.
how can configure individual properties spring create datasource? preferably in java, if there concise xml way i'm happy well. want avoid adding 100 lines of boilerplate equivalent url=...
a datasource
auto configured spring boot you. influence how , there several properties can set. prefixed spring.datasource
, list take @ spring boot reference guide full list.
in case add following application.properties
file
spring.datasource.url=jdbc:h2:file:...
this tell spring boot use url instead of default.
as h2 considered in-memory database , not regular database, when using jpa lead database dropped when application stopped. fix add following
spring.jpa.hibernate.ddl-auto=update
to specify dialect add following
spring.jpa.database-platform=org.hibernate.dialect.h2dialect
or simpler
spring.jpa.database=h2
Comments
Post a Comment