java - spring boot 1.2.5 @Configuration @Autowired annotation null? -
primarydatasourceproperties.java
application.properties
content, right.
datasourceconfig.java
inside method @bean ... primarydatasource ()
no primarydatasourceproperties
, primarydatasourceproperties
null, @autowired injected, problem
may ask reason?
btw:
demoapplication.java
try joinenableautoconfiguration
, primarydatasourceproperties.java
try @component
the result no different
here code:
resources/application.properties
datasource.primary.driver-class-name=com.mysql.jdbc.driver datasource.primary.url=jdbc:mysql://localhost:3306/demo?characterencoding=utf8 datasource.primary.username=root datasource.primary.password=
com.demo.config.primarydatasourceproperties.java
@configurationproperties(prefix="datasource.primary") public class primarydatasourceproperties { private string driverclassname; private string url; private string username; private string password; public string getdriverclassname() { return driverclassname; } public void setdriverclassname(string driverclassname) { this.driverclassname = driverclassname; } public string geturl() { return url; } public void seturl(string url) { this.url = url; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } }
com.demo.config.datasourceconfig.java
@configuration public class datasourceconfig { @autowired private primarydatasourceproperties primarydatasourceproperties; @bean @primary public datasource primarydatasource() { // primarydatasourceproperties null ? } }
com.demo.demoapplication.java
@springbootapplication public class demoapplication { public static void main(string[] args) throws exception { springapplication.run(demoapplication.class, args); } }
you have add annotation @enableconfigurationproperties
either demoapplication or configuration class. see manual topic.
Comments
Post a Comment