【问题标题】:Spring boot application with custom config does not load on server(websphere) startup具有自定义配置的 Spring Boot 应用程序不会在服务器(WebSphere)启动时加载
【发布时间】:2019-02-07 03:46:51
【问题描述】:

我有一个使用 mybatis 连接到两个数据源的 spring boot 应用程序。 Spring boot 在服务器启动时不加载,我在服务器启动期间检查了日志,但在其中没有看到任何与 Spring Boot 相关的消息。服务器启动后,应用程序状态在 WAS 控制台中显示为 Started。

应用程序在我重新启动之前无法工作,并且在应用程序重新启动期间,我可以在日志中看到 spring boot 相关消息。该应用程序在重新启动后运行良好。

服务器版本:IBM WebSphere Application Server Network Deployment (8.5.5.14) Java 版本:8.0.2.10(IBM WebSphere SDK Java 技术版)

@SpringBootApplication
@ComponentScan(basePackages="org.sample.solve.*")
@MapperScan("org.sample.solve.sa.db.mappers")
public class SampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }
}

配置:

package org.sample.solve.sa;

import .....

@Configuration
public class MybatisDBConfig  {

    private final Logger log = LoggerFactory.getLogger(this.getClass());
    public static final String SQL_SESSION_FACTORY_NAME_1 = "sqlSessionFactory1";
    public static final String SQL_SESSION_FACTORY_NAME_2 = "sqlSessionFactory2";

    public static final String MAPPERS_PACKAGE_TSC      = "org.sample.solve.sa.db.mappers"; // mapper interface package
    public static final String MAPPERS_PACKAGE_SERVICES_DB  = "org.sample.solve.sa.db.services_db.mappers"; // mapper interface package

    @Profile("local")
    @Bean(name = "tscDB")
    @Primary
    @ConfigurationProperties(prefix = "sp.firstDatasource")
    public DataSource dataSource1() {
        DataSource dataSource = null;
        try {
            dataSource = DataSourceBuilder.create().build();
            log.info("tsc datasource");
        }catch(Exception excep) {
            excep.printStackTrace();
        }
        return dataSource;
    }

    @Profile("local")
    @Bean(name = "servicesDB")
    @ConfigurationProperties(prefix = "sp.secondDatasource")
    public DataSource dataSource2() {
        DataSource dataSource = null;
        try {
            dataSource = DataSourceBuilder.create().build();
            log.info("services datasource");
        }catch(Exception excep) {
            excep.printStackTrace();
        }
        return dataSource;
    }


    @Profile({"dev", "fvt", "uat", "prod"})
    @Bean(name = "tscDB")
    @Primary
    public DataSource primaryDataSource() {
        System.out.println("sacpDS_JNDI : jdbc/QAServiceSACP");
        JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
        DataSource dataSource = dataSourceLookup.getDataSource("jdbc/QAServiceSACP");
        return dataSource;
    }

    @Profile({"dev", "fvt", "uat", "prod"})
    @Bean(name = "servicesDB")
    public DataSource secondaryDataSource() {
        System.out.println("servicesDS_JNDI : jdbc/servicesDS");
        JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
        DataSource dataSource = dataSourceLookup.getDataSource("jdbc/servicesDS");
        return dataSource;
    }

    @Bean(name = SQL_SESSION_FACTORY_NAME_1)
    @Primary
    public SqlSessionFactory sqlSessionFactory1(@Qualifier("tscDB") DataSource dataSource1) throws Exception {
        SqlSessionFactory sqlSessionFactory = null;
        try {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setTypeAliasesPackage("org.sample.solve.sa.pojo");
            Resource[] mapperLocations = new Resource[] { new ClassPathResource("SASolveMapper.xml") };
            sqlSessionFactoryBean.setMapperLocations(mapperLocations);
            sqlSessionFactoryBean.setDataSource(dataSource1);
            sqlSessionFactory = sqlSessionFactoryBean.getObject();
            sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
            sqlSessionFactory.getConfiguration().setJdbcTypeForNull(JdbcType.NULL);
        }catch(Exception excep) {
            excep.printStackTrace();
            throw new Exception(excep.getMessage());
        }
        return sqlSessionFactory;
    }

    @Bean(name = SQL_SESSION_FACTORY_NAME_2)
    public SqlSessionFactory sqlSessionFactory2(@Qualifier("servicesDB") DataSource dataSource2) throws Exception {
        SqlSessionFactory sqlSessionFactory = null;
        try {
            SqlSessionFactoryBean diSqlSessionFactoryBean = new SqlSessionFactoryBean();
            diSqlSessionFactoryBean.setTypeAliasesPackage("org.sample.solve.sa.pojo");
            Resource[] mapperLocations = new Resource[] {new ClassPathResource("ServicesDBMapper.xml")};
            diSqlSessionFactoryBean.setMapperLocations(mapperLocations);
            diSqlSessionFactoryBean.setDataSource(dataSource2);
            sqlSessionFactory = diSqlSessionFactoryBean.getObject();
            sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
            sqlSessionFactory.getConfiguration().setJdbcTypeForNull(JdbcType.NULL);
        }catch(Exception excep) {
            excep.printStackTrace();
            throw new Exception(excep.getMessage());
        }
        return sqlSessionFactory;
    }

    @Bean
    @Primary
    public MapperScannerConfigurer mapperScannerConfigurer1() {
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setBasePackage(MAPPERS_PACKAGE_TSC);
        configurer.setSqlSessionFactoryBeanName(SQL_SESSION_FACTORY_NAME_1);
        return configurer;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer2() {
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setBasePackage(MAPPERS_PACKAGE_SERVICES_DB);
        configurer.setSqlSessionFactoryBeanName(SQL_SESSION_FACTORY_NAME_2);
        return configurer;
    }

}

我正在使用 servlet 初始化程序设置活动配置文件。

【问题讨论】:

    标签: spring-boot mybatis websphere-8


    【解决方案1】:

    我在尝试创建计划作业(服务器中的 Spring Boot 应用程序)时也注意到了同样的情况。直到我通过调用休息服务或做其他事情来戳应用程序时,这项工作才开始。不幸的是,我不知道该怎么做才能让它工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-31
      • 2021-07-21
      • 2016-09-01
      • 1970-01-01
      • 2015-02-21
      • 2020-08-14
      • 2018-10-05
      • 1970-01-01
      相关资源
      最近更新 更多