如何在 CamelBlueprintTestSupport 中加载 spring 上下文 xml 文件以及如何在 spring 上下文 xml 中获取数据源对象

How to load spring context xml file in CamelBlueprintTestSupport and how to get datasource object in spring context xml

如何在 CamelBlueprintTestSupport 中加载 spring 上下文 xml 文件以及如何在 spring 上下文 xml 中获取数据源对象,问题是我遇到了异常像 com.hotel.common.persistence.dao.DAOFactory.getDAO(DAOFactory.java:27) 的 java.lang.NullPointerException 我的假设是 DAOFactory class 没有得到映射器 class es。正确吗?

我已经和你分享了我的示例代码

这是我的测试class

public class ContextRouteGetAvailabilityXmlTest 扩展了 CamelBlueprintTestSupport {

@Override
protected String getBlueprintDescriptor() {
    return "OSGI-INF/blueprint/context-beans.xml,OSGI-INF/blueprint/context-config.xml,OSGI-INF/blueprint/context-route-getAvailability.xml";
}


@Override
protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services){
    BasicDataSource datasource = new BasicDataSource();

    datasource.setDriverClassName("com.mysql.jdbc.Driver");
    datasource.setUrl("jdbc:mysql://my ip:3306/test");
    datasource.setUsername("test");
    datasource.setPassword("test1");
    datasource.setConnectionProperties("useUnicode=yes;characterEncoding=utf8;");

    services.put("dataSourceMySQL", asService(datasource, null));
}

}

这是我的 spring context.xml 文件

<osgi:reference id="dataSourceMySQL" interface="javax.sql.DataSource"/>

<bean id="mybatisConfig" class="org.springframework.core.io.ClassPathResource">
    <constructor-arg value="/META-INF/config/mybatis-config.xml" />
</bean>

<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSourceMySQL" />
    <property name="configLocation" ref="mybatisConfig" />
</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactoryBean" />
</bean>

<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.sia.csl.hotel.common.persistence.mapper" />
    <property name="sqlSessionTemplateBeanName" value="sqlSession" />
</bean>

<bean id="daoFactory"
    class="com.common.persistence.dao.DAOFactory" />

OSGi 蓝图不支持 Spring XML 文件,仅支持蓝图 XML 文件。所以你不能用 CamelBlueprintTestSupport 加载 Spring XML 个文件。相反,您可以通过 camel-test-spring 模块使用常规 spring。

如果您 运行 在 OSGi 上,则 Spring XML 文件 (Spring-DM) 已弃用并停产,应该 被使用。相反,您应该将 Spring XML 文件迁移到 OSGI 蓝图文件。

我尝试通过这种方式加载数据库,并与您分享了我的 DAOFactory.java 文件

public class DAOFactory {

static Logger logger = Logger.getLogger(DAOFactory.class);

private static BundleContext bundleContext;
private static SqlSessionTemplate sqlSessionTemplate;

public static void setBundleContext(BundleContext arg0) {
    bundleContext = arg0;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Object getDAO(Class DaoClassName) throws InvalidSyntaxException {
    logger.debug("getDAO sqlSessionTemplate: "+sqlSessionTemplate);
    if(sqlSessionTemplate == null ) {
        setBundleContext(FrameworkUtil.getBundle(DAOFactory.class).getBundleContext());
        sqlSessionTemplate = getSqlSession() ;
        logger.debug("Received sqlSessionTemplate: "+sqlSessionTemplate);
    }
    return sqlSessionTemplate.getMapper(DaoClassName);
}

@SuppressWarnings("rawtypes")
private static ApplicationContext getApplicationContext() throws InvalidSyntaxException {
        final String filter = "(org.springframework.context.service.name=" + bundleContext.getBundle().getSymbolicName() + ")";
        logger.debug("Symbolic Name: "+filter);
        final ServiceReference[] applicationContextRefs = bundleContext.getServiceReferences(ApplicationContext.class.getName(), filter);
        @SuppressWarnings("unchecked")
        final ApplicationContext applicationContext = (ApplicationContext) bundleContext.getService(applicationContextRefs[0]);
        logger.debug("applicationContext: "+applicationContext);
        return applicationContext;

}