Java tutorial
/* * Copyright (c) 2016 UBG - ? * * This software is the confidential and proprietary information of * UBG. You shall not disclose such confidential information * and shall use it only in accordance with the terms of the license * agreement you entered into with www.7qjf.com. */ package com.common.bean; import org.apache.commons.beanutils.MethodUtils; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.datasource.DataSourceUtils; import javax.sql.DataSource; import java.sql.Connection; /** * ?? * Created by zhujiawen on 2018/1/2 15:42 */ public class SourceTemplate { private static ApplicationContext ac = null; /** * ?? */ private synchronized static void init() { if (ac == null) { ac = new ClassPathXmlApplicationContext(new String[] { "spring-mybatis.xml" }); } } /** * ?Spring?bean * * @param beanName * @return */ public static Object getBean(String beanName) { // web.xml? if (SpringContextManager.isInitialized()) { return SpringContextManager.getBean(beanName); } else {// ?? web.xml if (ac == null) { init(); } return ac.getBean(beanName); } } /** * ??Bean * * @param cls * @return */ public static final <T> T getBean(Class<T> cls) { T result = null; if (SpringContextManager.isInitialized()) { result = SpringContextManager.getBean(cls); } else {// ?? web.xml if (ac == null) { init(); } result = ac.getBean(cls); } return result; } /** * ?spring?bean,? * * @param <T> * @param clazz * @param beanName * @return */ public static <T> T getBean(Class<T> clazz, String beanName) { return (T) getBean(beanName); } // public static HibernateTemplate getHibernateTemplate() { // return (HibernateTemplate) getBean("hibernateTemplate"); // } /** * ? * <p> * "???dataSource?,???" */ @Deprecated public static Connection getConn() throws Exception { Connection conn = DataSourceUtils.getConnection((DataSource) getBean("localDataSource")); return conn; } private static String DB_TYPE = null; // ? public final static String DB_TYPE_ORACLE = "1"; public final static String DB_TYPE_DB2 = "2"; public final static String DB_TYPE_INFORMIX = "3"; public final static String DB_TYPE_SYBASE = "4"; public final static String DB_TYPE_SQLSERVER = "5"; public final static String DB_TYPE_MYSQL = "6"; /** * ?? <br> * SourceTemplate.DB_TYPE_* * * @return */ public static String getDBType() { if (DB_TYPE == null) { Object sessionFactory = SourceTemplate.getBean("sessionFactory"); try { Object settings = MethodUtils.invokeMethod(sessionFactory, "getSettings", null); Object dialect = MethodUtils.invokeMethod(settings, "getDialect", null); String dialectStr = dialect.toString().toUpperCase(); if (dialectStr.indexOf("oracle") > 0) { DB_TYPE = DB_TYPE_ORACLE; } else if (dialectStr.indexOf("db2") > 0) { DB_TYPE = DB_TYPE_DB2; } else if (dialectStr.indexOf("INFORMIX") > 0) { DB_TYPE = DB_TYPE_INFORMIX; } else if (dialectStr.indexOf("SYBASE") > 0) { DB_TYPE = DB_TYPE_SYBASE; } else if (dialectStr.indexOf("SQLSERVER") > 0) { DB_TYPE = DB_TYPE_SQLSERVER; } else if (dialectStr.indexOf("MYSQL") > 0) { DB_TYPE = DB_TYPE_MYSQL; } } catch (Exception e) { throw new RuntimeException("Don't get the DB Type."); } } return DB_TYPE; } }