我需要在应用程序启动时处理一个文件,并根据给定的信息创建一些对象,然后将它们作为 bean 添加到 Camel 上下文中。例如:Mysql DataSource 对象。
public static void load(CamelContext context) throws Exception {
JndiRegistry registry = (JndiRegistry) context.getRegistry();
PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
pc.setLocation("classpath:mysql.properties");
for (String mysql: pc.parseUri("{{mysqlConfigs}}").split(",")) {
String MYSQL_DB_URL = pc.parseUri("{{" + mysql + ".url}}");
String MYSQL_DB_USERNAME = pc.parseUri("{{" + mysql + ".username}}");
String MYSQL_DB_PASSWORD = pc.parseUri("{{" + mysql + ".passwd}}");
String DB_NAME = pc.parseUri("{{" + mysql + ".name}}");
MysqlDataSource mysqlDS = new MysqlDataSource();
mysqlDS.setURL(MYSQL_DB_URL);
mysqlDS.setUser(MYSQL_DB_USERNAME);
mysqlDS.setPassword(MYSQL_DB_PASSWORD);
registry.bind(DB_NAME, (DataSource) mysqlDS);
}
}
代码抛出异常,
org.apache.camel.impl.PropertyPlaceholderDelegateRegistry cannot be cast to org.apache.camel.impl.JndiRegistry
如果我尝试将其转换为 SimpleRegistry,也会出现同样的错误。有没有办法可以通过这种方式实现这一点,或者考虑到不再添加更多上下文,是否应采用任何其他方法。
最佳答案
尝试:
JndiRegistry registry = context.getRegistry(JndiRegistry.class);
关于java - Apache Camel 2.12 : Adding bean to already existing context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32133984/