gpt4 book ai didi

java - 从项目中的不同模块引用 bean?

转载 作者:行者123 更新时间:2023-12-02 02:32:18 26 4
gpt4 key购买 nike

我在模块中声明了一个数据库。我有一个核心业务逻辑模块,需要在其中公开数据库。我将数据库定义为单例和服务。但是,我还需要从核心业务逻辑模块访问数据库。该服务可以工作,但我无法从核心引用 tkn 数据库。

my project

我需要引用在fleet.mt1.core模块中的fleet.mt1.dataserviceimpl模块中定义的TKN数据库。

想知道如何实现这一点?如果 Camel 蓝图/ Spring 中有一种方法可以做到这一点???

fleet.mt1.dataserviceimpl

    <cm:property-placeholder persistent-id="com.ge.digital.fleet.dataservice.impl"/>
<!-- Ensure that only one instance of a class pdxDb is created -->
<!-- Provide a global point of access to the object -->
<bean class="com.ge.digital.fleet.dataservice.impl.db.PDXDatabase"
id="pdxDb" scope="singleton"/>
<bean class="com.ge.digital.fleet.dataservice.impl.db.TKNDatabase"
id="tknDb" scope="singleton"/>
<!-- PDX Data Service Implementation -->
<bean
class="com.ge.digital.fleet.dataservice.impl.PDXDataServiceImpl" id="pdxDataService">
<property name="pdxDatabase" ref="pdxDb"/>
</bean>
<bean
class="com.ge.digital.fleet.dataservice.impl.TKNDataServiceImpl" id="tknDataService">
<property name="tknDatabase" ref="tknDb"/>
</bean>
<!-- PDX Data Service Registration -->
<service depends-on="pdxDb"
interface="com.ge.digital.fleet.dataservice.PDXDataService" ref="pdxDataService"/>
<service depends-on="tknDb"
interface="com.ge.digital.fleet.dataservice.TKNDataService" ref="tknDataService"/>
<bean
class="com.ge.digital.fleet.dataservice.impl.processor.ReplicatedDataProcessor" id="replicatedDataProcessor">
<property name="pdxDatabase" ref="pdxDb"/>
</bean>
<bean
class="com.ge.digital.fleet.dataservice.impl.filter.FTPFileFilter" id="ftpFileFilter"/>
<!-- properties found in com.ge.digital.fleet.dataservice.impl.cfg under etc -->
<bean
class="com.ge.digital.fleet.dataservice.impl.route.ReplicatedDataRouteBuilder"
depends-on="ftpFileFilter" id="replicatedDataRouteBuilder">
<property name="transportSftp" value="${transportSftp}"/>
<property name="transportFs" value="${transportFs}"/>
<property name="username" value="${username}"/>
<property name="host" value="${host}"/>
<property name="port" value="${port}"/>
<property name="pathSftp" value="${pathSftp}"/>
<property name="pathFs" value="${pathFs}"/>
<property name="password" value="${password}"/>
<property name="moveToPath" value="${moveToPath}"/>
<property name="fileArchive" value="${fileArchive}"/>
<property name="readLockFile" value="${readLockFile}"/>
<property name="ftpFileFilter" value="ftpFileFilter"/>
</bean>
<camelContext id="com.ge.digital.fleet.dataServiceImplCamelContext"
trace="false" xmlns="http://camel.apache.org/schema/blueprint">
<routeBuilder ref="replicatedDataRouteBuilder"/>
</camelContext>
</blueprint>

fleet.mt1.core

<reference id="pdxDataService" interface="com.ge.digital.fleet.dataservice.PDXDataService"/>
<bean
class="com.ge.digital.fleet.core.processors.DbAvailableProcessor" id="dbAvailableProcessor">
<property name="dataService" ref="pdxDataService"/>
</bean>

<reference id="tknDataService" interface="com.ge.digital.fleet.dataservice.TKNDataService"/>
<bean class="com.ge.digital.fleet.dataservice.impl.db.TKNDatabase"
id="tknDb" scope="singleton"/>
<bean
class="com.ge.digital.fleet.core.processors.AccessKeyMarsheler" id="accessKeyMarsheler">
<property name="tknDatabase" ref="tknDb"/>
</bean>

这是错误的,我知道,这是我的问题,如何在核心模块中引用它?

<bean class="com.ge.digital.fleet.dataservice.impl.db.TKNDatabase"
id="tknDb" scope="singleton"/>

谢谢你的帮助!我正在为此苦苦挣扎。我尝试过引用 Bean、类,但当然,这是非法语法。

我正在为服务开发另一个接口(interface)来跨模块访问它,我想我希望有一种方法可以在 Spring 中做到这一点,作为引用或依赖项或其他东西。

最佳答案

在等待答案以查看 Spring 是否可以实现这一点时,我继续为方法和接口(interface)创建了包装器,它工作得很好,但是,我很想知道这是否可以在 Spring 中通过引用或实现来完成某物。我需要在 Spring 学习和掩体。

再次感谢。

public class TKNDataServiceImpl implements TKNDataService {

private static final Logger LOG = LoggerFactory.getLogger(TKNDataServiceImpl.class);

private TKNDatabase tknDb = null;

@Override
public boolean isTKNDataServiceAvailable() {
return tknDb.isAvailable();
}

@Override
public TknLocator getTokenLocatorForCustomerKey(String custkey) throws TKNDataServiceUnavailableException,
TKNDataServiceInvalidDataException {

if (null != custkey) {

final String token = tknDb.getAccessToken(custkey);
Timestamp expiry = tknDb.getExpiry(custkey);
LOG.info("ACCESS TOKEN: " + token);
LOG.info("EXPIRY: " + expiry);
TknLocator loc = new TknLocator();
loc.setAccess_token(token);
loc.setexpires_in(expiry);

return loc;
}

return null;
}

@Override
public boolean CustomerKeyExistsInTokenDb(String custkey) throws TKNDataServiceInvalidDataException,
TKNDataServiceUnavailableException {

boolean keyExists = tknDb.doesCustKeyExist(custkey);
return keyExists;
}

@Override
public void TknDbAddRow(String custkey, String access_token, int expires_in) {
if (null != custkey) {
tknDb.addRow(custkey, access_token, expires_in);
}
}

@Override
public void TknReplicationComplete() {
tknDb.replicationComplete();
}

@Override
public void TknDropRow(String custkey) {
if (null != custkey) {
tknDb.dropRow(custkey);
}
}

public Timestamp TknGetExpiry(String custkey) throws TKNDataServiceUnavailableException,
TKNDataServiceInvalidDataException {

return(tknDb.getExpiry(custkey));
}

public String TknGetAccessToken(String custkey) throws TKNDataServiceUnavailableException,
TKNDataServiceInvalidDataException {

return(tknDb.getAccessToken(custkey));
}


public void settknDatabase(TKNDatabase tknDb) {
this.tknDb = tknDb;
}

}

关于java - 从项目中的不同模块引用 bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57223644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com