公司有需求将普通项目拆分为微服务,使用springcloud。
项目中有hql,并且为多数据源,尝试用springboot的配置
@EnableJpaRepositories通过该注解扫描带有@Repository注解的类,再而扫描到有@Entity注解的对象 此信息由https://www.cnblogs.com/jpfss/p/11127092.html了解
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManageFactoryPrimary",
transactionManagerRef = "transactionManagerPrimary",
basePackageClasses = GenericDao.class
)
public class EntityManagerConfig {
@Autowired
private MybatisConfig mybatisConfig;
@Primary
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) throws Exception {
return entityManageFactory(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = "entityManageFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManageFactory(EntityManagerFactoryBuilder builder) throws Exception {
DynamicDataSource dynamicDataSource = mybatisConfig.dataSource(mybatisConfig.db1DataSource());
LocalContainerEntityManagerFactoryBean entityManagerFactory = builder.dataSource(dynamicDataSource)
.packages("com.yanhua.consumer.database.model").build();
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
jpaProperties.put("hibernate.physical_naming_strategy", "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
jpaProperties.put("hibernate.connection.charSet", "utf-8");
jpaProperties.put("hibernate.show_sql", "false");
entityManagerFactory.setJpaProperties(jpaProperties);
entityManagerFactory.setPersistenceProviderClass(HibernatePersistenceProvider.class);
//包扫描
// entityManagerFactory.setPackagesToScan({""});
return entityManagerFactory;
}
@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) throws Exception {
return new JpaTransactionManager(entityManageFactory(builder).getObject());
}
}
多数据源读取原本为sql使用,尝试给hql配置读取
@Configuration
//@MapperScan(basePackages = {"com.yanhua.consumer.database.mapper"}, markerInterface = IMyMapper.class)
public class MybatisConfig {
private static final String DRIIVER_CLASSNAME = "driverClassName";
private static final String URL = "url";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
private static final String INITIALSIZE = "initialSize";
private static final String MAXACTIVE = "maxActive";
private static final String MINIDLE = "minIdle";
private static final String MAXWAIT = "maxWait";
private static final String TESTONBORROW = "testOnBorrow";
private static final String VALIDATIONQUERY = "validationQuery";
private static final String TESTWHILEIDLE = "testWhileIdle";
private static ResourceBundle dbconfig = ResourceBundle.getBundle("dbconfig");
/**
* 创建数据源(数据源的名称:方法名可以取为XXXDataSource(),XXX为数据库名称,该名称也就是数据源的名称)
*/
@Bean
public DataSource db1DataSource() throws Exception {
Properties props = new Properties();
props.put(DRIIVER_CLASSNAME, dbconfig.getString("jdbc.driver-class-name"));
props.put(URL, dbconfig.getString("jdbc.url"));
props.put(USERNAME, dbconfig.getString("jdbc.username"));
props.put(PASSWORD, dbconfig.getString("jdbc.password"));
props.put(INITIALSIZE, dbconfig.getString("jdbc.initialSize"));
props.put(MAXACTIVE, dbconfig.getString("jdbc.maxActive"));
props.put(MINIDLE, dbconfig.getString("jdbc.minIdle"));
props.put(MAXWAIT, dbconfig.getString("jdbc.maxWait"));
props.put(TESTONBORROW, dbconfig.getString("jdbc.testOnBorrow"));
props.put(VALIDATIONQUERY, dbconfig.getString("jdbc.validationQuery"));
props.put(TESTWHILEIDLE, dbconfig.getString("jdbc.testWhileIdle"));
return DruidDataSourceFactory.createDataSource(props);
}
/**
* 添加数据源则加 @Qualifier("db2DataSource") DataSource db2DataSource
*
* @param db1DataSource
* @return
*/
@Bean
@Primary
public DynamicDataSource dataSource(@Qualifier("db1DataSource") DataSource db1DataSource) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DatabaseType.db1, db1DataSource);
// targetDataSources.put(DatabaseType.db2, db2DataSource);
DynamicDataSource dataSource = new DynamicDataSource();
dataSource.setTargetDataSources(targetDataSources);
//默认db1设置为数据源
dataSource.setDefaultTargetDataSource(db1DataSource);
return dataSource;
}
/**
* 根据数据源创建SqlSessionFactory
*
* @param
* @return
* @throws Exception
* @Qualifier("db2DataSource") DataSource db2DataSource
*/
@Bean
public SqlSessionFactory sqlSessionFactory(@Qualifier("db1DataSource") DataSource db1DataSource
) throws Exception {
SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
// 指定数据源(这个必须有,否则报错)
fb.setDataSource(this.dataSource(db1DataSource));
// 指定基包
fb.setTypeAliasesPackage(dbconfig.getString("mybatis.typeAliasesPackage"));
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources(dbconfig.getString("mybatis.mapperLocations"));
fb.setMapperLocations(resources);
return fb.getObject();
}
}
目前状态:爆出大量错误
Positive matches:
-----------------
AopAutoConfiguration matched:
- @ConditionalOnClass found required classes 'org.springframework.context.annotation.EnableAspectJAutoProxy', 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice', 'org.aspectj.weaver.AnnotatedElement' (OnClassCondition)
- @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)
AopAutoConfiguration.CglibAutoProxyConfiguration matched:
- @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)
CodecsAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.http.codec.CodecConfigurer' (OnClassCondition)
CodecsAutoConfiguration.JacksonCodecConfiguration matched:
- @ConditionalOnClass found required class 'com.fasterxml.jackson.databind.ObjectMapper' (OnClassCondition)
CodecsAutoConfiguration.JacksonCodecConfiguration#jacksonCodecCustomizer matched:
- @ConditionalOnBean (types: com.fasterxml.jackson.databind.ObjectMapper; SearchStrategy: all) found bean 'jacksonObjectMapper' (OnBeanCondition)
DataSourceTransactionManagerAutoConfiguration matched:
- @ConditionalOnClass found required classes 'org.springframework.jdbc.core.JdbcTemplate', 'org.springframework.transaction.PlatformTransactionManager' (OnClassCondition)
DispatcherServletAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet' (OnClassCondition)
- found 'session' scope (OnWebApplicationCondition)
DispatcherServletAutoConfiguration.DispatcherServletConfiguration matched:
- @ConditionalOnClass found required class 'javax.servlet.ServletRegistration' (OnClassCondition)
- Default DispatcherServlet did not find dispatcher servlet beans (DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition)
DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration matched:
- @ConditionalOnClass found required class 'javax.servlet.ServletRegistration' (OnClassCondition)
- DispatcherServlet Registration did not find servlet registration bean (DispatcherServletAutoConfiguration.DispatcherServletRegistrationCondition)
DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration#dispatcherServletRegistration matched:
- @ConditionalOnBean (names: dispatcherServlet; types: org.springframework.web.servlet.DispatcherServlet; SearchStrategy: all) found bean 'dispatcherServlet' (OnBeanCondition)
EmbeddedWebServerFactoryCustomizerAutoConfiguration matched:
- @ConditionalOnWebApplication (required) found 'session' scope (OnWebApplicationCondition)
EmbeddedWebServerFactoryCustomizerAutoConfiguration.TomcatWebServerFactoryCustomizerConfiguration matched:
- @ConditionalOnClass found required classes 'org.apache.catalina.startup.Tomcat', 'org.apache.coyote.UpgradeProtocol' (OnClassCondition)
ErrorMvcAutoConfiguration matched:
- @ConditionalOnClass found required classes 'javax.servlet.Servlet', 'org.springframework.web.servlet.DispatcherServlet' (OnClassCondition)
- found 'session' scope (OnWebApplicationCondition)
ErrorMvcAutoConfiguration#basicErrorController matched:
- @ConditionalOnMissingBean (types: org.springframework.boot.web.servlet.error.ErrorController; SearchStrategy: current) did not find any beans (OnBeanCondition)
ErrorMvcAutoConfiguration#errorAttributes matched:
- @ConditionalOnMissingBean (types: org.springframework.boot.web.servlet.error.ErrorAttributes; SearchStrategy: current) did not find any beans (OnBeanCondition)
ErrorMvcAutoConfiguration.DefaultErrorViewResolverConfiguration#conventionErrorViewResolver matched:
- @ConditionalOnBean (types: org.springframework.web.servlet.DispatcherServlet; SearchStrategy: all) found bean 'dispatcherServlet'; @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.web.servlet.error.DefaultErrorViewResolver; SearchStrategy: all) did not find any beans (OnBeanCondition)
ErrorMvcAutoConfiguration.WhitelabelErrorViewConfiguration matched:
- @ConditionalOnProperty (server.error.whitelabel.enabled) matched (OnPropertyCondition)
- ErrorTemplate Missing did not find error template view (ErrorMvcAutoConfiguration.ErrorTemplateMissingCondition)
ErrorMvcAutoConfiguration.WhitelabelErrorViewConfiguration#beanNameViewResolver matched:
- @ConditionalOnMissingBean (types: org.springframework.web.servlet.view.BeanNameViewResolver; SearchStrategy: all) did not find any beans (OnBeanCondition)
ErrorMvcAutoConfiguration.WhitelabelErrorViewConfiguration#defaultErrorView matched:
- @ConditionalOnMissingBean (names: error; SearchStrategy: all) did not find any beans (OnBeanCondition)
GenericCacheConfiguration matched:
- Cache org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration automatic cache type (CacheCondition)
GsonAutoConfiguration matched:
- @ConditionalOnClass found required class 'com.google.gson.Gson' (OnClassCondition)
Negative matches:
-----------------
ActiveMQAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition)
AopAutoConfiguration.JdkDynamicAutoProxyConfiguration:
Did not match:
- @ConditionalOnProperty (spring.aop.proxy-target-class=false) did not find property 'proxy-target-class' (OnPropertyCondition)
ArtemisAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory' (OnClassCondition)
AtomikosJtaConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'com.atomikos.icatch.jta.UserTransactionManager' (OnClassCondition)
BatchAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'org.springframework.batch.core.launch.JobLauncher' (OnClassCondition)
BitronixJtaConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'bitronix.tm.jndi.BitronixContext' (OnClassCondition)
CacheAutoConfiguration:
Did not match:
- @ConditionalOnBean (types: org.springframework.cache.interceptor.CacheAspectSupport; SearchStrategy: all) did not find any beans of type org.springframework.cache.interceptor.CacheAspectSupport (OnBeanCondition)
Matched:
- @ConditionalOnClass found required class 'org.springframework.cache.CacheManager' (OnClassCondition)
CacheAutoConfiguration.CacheManagerJpaDependencyConfiguration:
Did not match:
- Ancestor org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration did not match (ConditionEvaluationReport.AncestorsMatchedCondition)
Matched:
- @ConditionalOnClass found required class 'org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean' (OnClassCondition)
挑了一部分贴出来,了解到当我们用debug模式时,控制台就会打印出对应的那些配置类被加载了。而这就是生效的类。在启动报告中有两个部分一部分是Positive matches另一部分是Negative matches
以下就是控制台打印出来的Positive matches信息,自动加载了很多的配置类。还有就是第二部分Negative matches。这里只是展示了Positive部分。使用自动配置的报告可以更加高效的查看出那些自动配置是生效的,那些自动配置是没有被加载。
此信息由https://blog.csdn.net/nihui123/article/details/88561012了解
了解哪些没有被加载,进一步进行处理。