Spring integration 从数据库动态获取sftp连接属性,并在spring集成中获取sftpConnectionFactory
我需要从数据库中获取SFTP连接属性,并使用来自数据库的属性动态创建Spring integration 从数据库动态获取sftp连接属性,并在spring集成中获取sftpConnectionFactory,spring-integration,Spring Integration,我需要从数据库中获取SFTP连接属性,并使用来自数据库的属性动态创建sftpSessionFactory 下面的${sftp.host},${sftp.port},${sftp.user}和${sftp.password}属性值应来自数据库 <bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory"> <prope
sftpSessionFactory
下面的${sftp.host}
,${sftp.port}
,${sftp.user}
和${sftp.password}
属性值应来自数据库
<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<property name="host" value="${sftp.host}"/>
<property name="port" value="${sftp.port}" />
<property name="user" value="${sftp.user}"/>
<property name="password" value="${sftp.password}"/>
</bean>
spring集成上下文.xml
完成上述所有操作后,将不会创建工厂。也不例外。
I guest*integration-conotext.xml未加载。
你能告诉我出了什么问题吗?你的
web.xml
可以选择按模式classpath*:context/application context-*.xml加载任何xml配置。但是Spring集成配置名称看起来像Spring Integration context.xml
从我的角度来看,这无关紧要,最好在这件事上使用
另一方面,请看一下Spring Integration 4.2以来新增的DelegatingSessionFactory
功能,它允许在运行时根据requestMessage
检索目标SessionFactory
还有一点。我使用从数据库加载属性
,并将最后一个属性用作属性占位符
:
<bean id="databaseConfiguration" class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg ref="dataSource"/>
<constructor-arg value="PROPERTIES"/>
<constructor-arg value="PROPERTY"/>
<constructor-arg value="PROPERTYVALUE"/>
</bean>
<bean id="propertiesFromDB" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
p:staticMethod="org.apache.commons.configuration.ConfigurationConverter.getProperties"
p:arguments-ref="databaseConfiguration"/>
<context:property-placeholder properties-ref="propertiesFromDB"/>
如果这足以让您在启动时仅从DB加载一次它们。您的web.xml
可以选择按模式classpath*:context/application context-*.xml加载任何xml配置。但是Spring集成配置名称看起来像Spring Integration context.xml
从我的角度来看,这无关紧要,最好在这件事上使用
另一方面,请看一下Spring Integration 4.2以来新增的DelegatingSessionFactory
功能,它允许在运行时根据requestMessage
检索目标SessionFactory
还有一点。我使用从数据库加载属性
,并将最后一个属性用作属性占位符
:
<bean id="databaseConfiguration" class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg ref="dataSource"/>
<constructor-arg value="PROPERTIES"/>
<constructor-arg value="PROPERTY"/>
<constructor-arg value="PROPERTYVALUE"/>
</bean>
<bean id="propertiesFromDB" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
p:staticMethod="org.apache.commons.configuration.ConfigurationConverter.getProperties"
p:arguments-ref="databaseConfiguration"/>
<context:property-placeholder properties-ref="propertiesFromDB"/>
如果这足以让您在启动时仅从DB加载一次它们。您可以显示您的代码/您已经尝试过的内容吗?您可以显示您的代码/您已经尝试过的内容吗?
@Service
public class EnvironmentLookupService {
private static final Logger logger = LoggerFactory.getLogger(EnvironmentLookupService.class);
@Autowired
@Qualifier("transactionManager")
private PlatformTransactionManager txManager;
@Autowired
private ConfigDao configDao;
private final Map<String, String> config = new HashMap<String, String>();
/**
* Loads all lookups used in xyz from database
*/
@PostConstruct
public void loadLookupData() {
logger.info("loading lookup cache");
new TransactionTemplate(txManager).execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
loadLookups();
}
});
// initialize();
}
/**
* Refreshes the lookup caches
*/
@Scheduled(cron = "${core.lookups.cache.refresh.cron}")
public void refreshCache() {
loadLookupData();
}
/**
* Loads all lookups from database
*/
protected void loadLookups() {
final List<IConfig> list = configDao.findAll();
for (final IConfig property : list) {
config.put(property.getKey(), property.getValue());
}
}
/**
* Will return configuration value for the key provided
*
* @param key
* @return String
*/
public String retriveConfigValue(EConfig key) {
return config.get(key.getValue());
}
}
@Service
public class DynamicSftpChannelResolver {
@Autowired
private EnvironmentLookupService lookupService;
ConfigurableApplicationContext confAppContext;
@PostConstruct
public void setEnvironmentForSftpConnection() {
confAppContext = new ClassPathXmlApplicationContext("classpath*:context/*integration-context.xml");
final StandardEnvironment stdEnvironment = new StandardEnvironment();
final Properties props = new Properties();
props.setProperty(EConfig.SFTP_HOST.getValue(), lookupService.retriveConfigValue(EConfig.SFTP_HOST));
props.setProperty(EConfig.SFTP_PORT.getValue(), lookupService.retriveConfigValue(EConfig.SFTP_PORT));
props.setProperty(EConfig.SFTP_USER.getValue(), lookupService.retriveConfigValue(EConfig.SFTP_USER));
props.setProperty(EConfig.SFTP_PASSWORD.getValue(), lookupService.retriveConfigValue(EConfig.SFTP_PASSWORD));
final PropertiesPropertySource propPropertySource = new PropertiesPropertySource("sftp", props);
stdEnvironment.getPropertySources().addLast(propPropertySource);
confAppContext.setEnvironment(stdEnvironment);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-file="http://www.springframework.org/schema/integration/file"
xmlns:int-sftp="http://www.springframework.org/schema/integration/sftp"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:int-ws="http://www.springframework.org/schema/integration/ws"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/integration/ws http://www.springframework.org/schema/integration/ws/spring-integration-ws.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<tx:annotation-driven transaction-manager="transactionManager" />
<context:property-placeholder />
<!-- <context:property-placeholder ignore-unresolvable="true" location="classpath:*.properties"/> -->
<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<property name="host" value="${sftp.host}"/>
<property name="port" value="${sftp.port}" />
<property name="user" value="${sftp.user}"/>
<property name="password" value="${sftp.password}"/>
</bean>
<int:channel id="ch.file.download"></int:channel >
<int:channel id="ch.file.type.access"></int:channel >
<int:channel id="ch.file.type.excel"></int:channel >
<int:channel id="ch.file.type.csv"></int:channel >
<int:channel id="ch.file.validated"></int:channel>
<int-sftp:inbound-channel-adapter id="sftp.file.client.download"
session-factory="sftpSessionFactory" channel="ch.file.download"
auto-create-local-directory="true" remote-directory="${sftp.remote.directory}"
local-filename-generator-expression="#this"
temporary-file-suffix=".downloading"
local-directory="${sftp.local.directory}"
>
<int:poller fixed-rate="1000"/>
</int-sftp:inbound-channel-adapter>
<bean id="databaseConfiguration" class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg ref="dataSource"/>
<constructor-arg value="PROPERTIES"/>
<constructor-arg value="PROPERTY"/>
<constructor-arg value="PROPERTYVALUE"/>
</bean>
<bean id="propertiesFromDB" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
p:staticMethod="org.apache.commons.configuration.ConfigurationConverter.getProperties"
p:arguments-ref="databaseConfiguration"/>
<context:property-placeholder properties-ref="propertiesFromDB"/>