spring - create an instance of DataSource and use inside constructor -
in current spring-boot project, have class:
@component public class oauthtokenstore extends jdbctokenstore { public oauthtokenstore() { super(...); } }
the atribute super(...)
should valid datasource. have configuration in application.properties
:
# jdbc.x spring.datasource.driverclassname=com.mysql.jdbc.driver spring.datasource.url=jdbc:mysql://localhost/lojacms spring.datasource.username=root spring.datasource.password= # hibernate.x spring.jpa.database-platform=org.hibernate.dialect.mysql5dialect spring.jpa.dialect=org.hibernate.dialect.mysql5dialect spring.jpa.show-sql=false spring.jpa.hibernate.ddl-auto=validate
is there way create instance of datasource , use in constructor?
you need inject datasource
bean can pass oauthtokenstore
constructor using @inject
.
@component public class oauthtokenstore extends jdbctokenstore { @inject public oauthtokenstore(datasource ds) { super(ds); } }
if don't yet have bean of type datasource
defined in application you'll need one:
@configuration public class config { @bean public drivermanagerdatasource datasource() { drivermanagerdatasource datasource = new drivermanagerdatasource(); datasource.setdriverclassname("com.mysql.jdbc.driver"); datasource.seturl("jdbc:mysql://localhost/lojacms"); datasource.setusername("root"); datasource.setpassword(""); return datasource; } }
Comments
Post a Comment