Usually read the source or something without purpose, so very little to see what source code, mainly compared to look hard, so the general work just find a template copy.
Above nonsense, cut ————————————————————————————————————————————————————————————
The DataSource usage in other projects has recently been imitated using the following code:
1 @Bean2 @Primary3@ConfigurationProperties (prefix = "Datasource.from")4 PublicDataSource DataSource () {5 returndatasourcebuilder.create (). build ();6 }7 8 @Autowired9@Qualifier ("DataSource")Ten PrivateDataSource DataSource; One A@Bean (name= "Sqlsessionfactory") - PublicSqlsessionfactory Sqlsessionfactory ()throwsException { -Sqlsessionfactorybean Bean =NewSqlsessionfactorybean (); the Bean.setdatasource (dataSource); -Bean.setconfiglocation (NewClasspathresource ("Mybatisconfig.xml")); - returnBean.getobject (); -}
"No supported DataSource type found" was reported when the initialization was run, and the database type was not specified by looking up the online data.
So, in the configuration of the addition of datasource.from.type=oracle, run or report the same error. So, to the night when the brain sober point, looked at the next datasource initialization part of the source code.
The main is the third code is as follows:
1 Private Static Final String[] Data_source_type_names = {"Org.apache.tomcat.jdbc.pool.DataSource", "Com.zaxxer.hikari.HikariDataSource", "Org.apache.commons.dbcp.BasicDataSource", "Org.apache.commons.dbcp2.BasicDataSource"};
Public extends Datasource> Findtype () { if (thisnull) { return This . Type; } for (String name:data_source_type_names) { try { return this. classLoader); } Catch (Exception ex) {}
1 Privateclass<?extendsDatasource>GetType () {2class<?extendsdatasource> type =Findtype ();3 if(Type! =NULL) {4 returntype;5 }6 Throw NewIllegalStateException ("No supported DataSource type found");7}
1 Public DataSource Build () {2 extends datasource> type = getType (); 3 DataSource result = (DataSource) beanutils.instantiate (type); 4 maybegetdriverclassname (); 5 bind (result); 6 return result; 7 }
Read the above code to understand why the error, that is, in the creation of DataSource did not find Javax.sql.DataSource subclasses, that is, must introduce the Data_source_type_names enumeration involved in the class jar package, Also note the introduction of the corresponding database JDBC Jar package.
Above, good lucky!
Spring uses datasoure injection parameters times No supported DataSource type found