Spring uses datasoure injection parameters times No supported DataSource type found

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.