如何在CamelBlueprintTestSupport中加载spring上下文xml文件以及如何在spring上下文xml中获取数据源对象
如何在CamelBlueprintTestSupport中加载spring上下文xml文件,以及如何在spring上下文xml中获取数据源对象,问题是我在com.hotel.common.persistence.dao.DAOFactory.getDAO(DAOFactory.java:27)上遇到了类似java.lang.NullPointerException的异常我的假设是DAOFactory类没有得到映射器类。对吗 我已经和你分享了我的示例代码 这是我的测试课 公共类ContextRouteGetAvailabilityXmlTest扩展了CamelBlueprintTest支持{如何在CamelBlueprintTestSupport中加载spring上下文xml文件以及如何在spring上下文xml中获取数据源对象,spring,apache-camel,jbossfuse,Spring,Apache Camel,Jbossfuse,如何在CamelBlueprintTestSupport中加载spring上下文xml文件,以及如何在spring上下文xml中获取数据源对象,问题是我在com.hotel.common.persistence.dao.DAOFactory.getDAO(DAOFactory.java:27)上遇到了类似java.lang.NullPointerException的异常我的假设是DAOFactory类没有得到映射器类。对吗 我已经和你分享了我的示例代码 这是我的测试课 公共类ContextRou
@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));
}
@覆盖
受保护的字符串getBlueprintDescriptor(){
返回“OSGI-INF/blueprint/context bean.xml,OSGI-INF/blueprint/context config.xml,OSGI-INF/blueprint/context route getAvailability.xml”;
}
@凌驾
受保护的void addServicesOnStartup(映射服务){
BasicDataSource数据源=新的BasicDataSource();
setDriverClassName(“com.mysql.jdbc.Driver”);
setUrl(“jdbc:mysql://my ip:3306/测试);
datasource.setUsername(“测试”);
setPassword(“test1”);
setConnectionProperties(“useUnicode=yes;characterEncoding=utf8;”;
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 Blueprint不支持Spring XML文件,只支持Blueprint XML文件。因此,您无法使用
CamelBlueprintTestSupport
加载SpringXML文件。相反,您可以通过驼峰测试弹簧模块使用常规弹簧
如果您在OSGi上运行,那么SpringXML文件(SpringDM)将被弃用和EOL,并且不应使用。相反,您应该将Spring XML文件迁移到OSGI Blueprint文件。通过这种方式,我尝试加载db,并与您共享了我的DAOFactory.java文件
<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" />
公营道场{
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;
}
通过这种方式,我尝试加载数据库,并与您共享了我的DAOFactory.java文件