将Spring概要文件与基于Spring web的项目一起使用时未得到解决
在application.properties中:spring.profiles.active=DEV 在dev config文件中:提到了所有mongo连接属性 并添加了如下配置java文件将Spring概要文件与基于Spring web的项目一起使用时未得到解决,spring,spring-mvc,spring-boot,Spring,Spring Mvc,Spring Boot,在application.properties中:spring.profiles.active=DEV 在dev config文件中:提到了所有mongo连接属性 并添加了如下配置java文件 @Configuration @PropertySource("classpath:userIdentity_Dev.properties") @Profile("DEV") public class UserIdentityConfigDev { } 运行应用程序时,spring事件探查器没有得
@Configuration
@PropertySource("classpath:userIdentity_Dev.properties")
@Profile("DEV")
public class UserIdentityConfigDev
{
}
运行应用程序时,spring事件探查器没有得到解决
接收到堆栈下的跟踪
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userIdentityService': Unsatisfied dependency expressed through field 'userIdentityBusiness'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userIdentityBusiness': Unsatisfied dependency expressed through field 'userIdentityRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userIdentityRepositoryImpl': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'mongodb.userIdentity.host' in string value "${mongodb.userIdentity.host}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
表示未解析${mongodb.userIdentity.host}属性
为项目创建war和jar文件时,不会解析spring概要文件这是主类:
`@springbootplication(exclude={DataSourceAutoConfiguration.class,MongoAutoConfiguration.class,MongoDataAutoConfiguration.class})
@PropertySource(“类路径:application.properties”)
公共类应用程序启动扩展SpringBootServletInitializer
{
公共静态void main(字符串[]args)
}`
以下是属性文件:
## MongoDB Connection Properties-----------------
MongoDB数据库
mongodb.userIdentity.database=userIdentity\u CS
isConnectionStringUsed为true,则应用程序根据connectionString创建连接,否则将使用MongoDB单服务器属性。
mongodb.userIdentity.isConnectionStringUsed=false
带有身份验证的连接字符串
mongodb.connectionString=mongodb://sa:Test%40123@SPT-CPU-0259:27017,SPT-CPU-0173:27017/管理员?复制集=监视
无身份验证的连接字符串
mongodb.userIdentity.connectionString=mongodb://localhost:27017/?replicaSet=surveillens
MongoDB单服务器属性。
mongodb.userIdentity.host=localhost
mongodb.userIdentity.port=27017
身份验证属性
mongodb.userIdentity.isAuthenticationEnable=false
mongodb.userIdentity.userName=sa
mongodb.userIdentity.password=Test@123
mongodb.userIdentity.authDB=admin
用户标识的集合名称
mongodb.userIdentity.collectionName=CreditScore
其他属性-----------------------
userIdentity.ValidKeySet=电子邮件;电话号码_身份证
userIdentity.logsFolder=。/IdentityLogs/
userIdentity.insertBatchSize=100
下面是使用所有这些属性的file.java文件
`
@配置
公共抽象类MongoDbRepository{
private Class<T> clazz;
private static MongoClient mongoClient = null;
private static MongoDatabase mongoDatabase = null;
private static ObjectMapper mapper = null;
@Value("${mongodb.userIdentity.host}")
private String mongoHost;
@Value("${mongodb.userIdentity.port}")
private int mongoPortNumber;
@Value("${mongodb.userIdentity.database}")
private String mongoDatabaseName;
@Value("${mongodb.userIdentity.userName}")
private String mongoUserName;
@Value("${mongodb.userIdentity.authDB}")
private String mongoAuthDB;
@Value("${mongodb.userIdentity.password}")
private String mongoPassword;
@Value("${mongodb.userIdentity.isAuthenticationEnable}")
private boolean mongoIsAuthEnable;
@Value("${mongodb.userIdentity.isConnectionStringUsed}")
private boolean mongoIsConnectionStringUsed;
@Value("${mongodb.userIdentity.connectionString}")
private String mongoConnectionString;
public final void setClazz(Class<T> clazzToSet)
{
this.clazz = clazzToSet;
}
/**
* Instantiates a new mongo base repository.
* @throws Exception
*/
public MongoDbRepository()
{
//Trigger MongoDB Connection initialization
if(mongoClient == null)
{
prepareMongoConnection();
}
else
{
// Trigger any method to check MongoDB client is connected
mongoClient.getAddress();
}
// Trigger ObjectMapper initialization
if(mapper == null)
prepareObjectMapper();
}
/**
* Instantiates a new mongoDB connection.
* @throws Exception
*/
private void prepareMongoConnection()
{
if (mongoConnectionString != null && !mongoConnectionString.isEmpty())
{
boolean isConnectionStringUsed = mongoIsConnectionStringUsed;
if(isConnectionStringUsed)
{
MongoClientURI clientUri = new MongoClientURI(mongoConnectionString);
mongoClient = new MongoClient(clientUri);
}
else
{
if(mongoIsAuthEnable)
{
MongoCredential credential = MongoCredential.createCredential(mongoUserName, mongoAuthDB, mongoPassword.toCharArray());
mongoClient = new MongoClient( new ServerAddress(mongoHost, mongoPortNumber), Arrays.asList(credential));
}
else
mongoClient = new MongoClient(mongoHost, mongoPortNumber);
}
// Trigger any method to check MongoDB client is connected
mongoClient.getAddress();
// Get Database from mongoClient.
mongoDatabase = mongoClient.getDatabase(mongoDatabaseName);
}
}
/**
* Get an objectMapper.
*/
private void prepareObjectMapper()
{
mapper = CommonFunctions.getObjectMapper();
}
/**
* Get the MongoDB collection object from MongoDB.
*
* @param collectionName is Name of a MongoDB collection
* @return Collection object
* @throws Exception
*/
private MongoCollection<Document> getCollection(String collectionName) throws Exception
{
if(mongoClient == null)
prepareMongoConnection();
return mongoDatabase.getCollection(collectionName);
}
/* ------- Find functions ------- */
/**
* Find one documents from mongoDB collection.
*
* @param collectionName the collection name
* @param query the query document - set to empty document means no query filtering.
*
* @return entityObj the entity Object
* @throws Exception the exception
*/
public T findOne(String collectionName, Object query) throws Exception
{
if(clazz == null)
throw new NullPointerException("ST224 - Generic class is null - set the generic class before perform MongoDB operation");
MongoCollection<Document> collection = getCollection(collectionName);
Document mongoDoc = collection.find(convertToBsonDocument(query)).first();
String jsonStr = mapper.writeValueAsString(mongoDoc);
T entityObj = mapper.readValue(jsonStr, clazz);
return entityObj;
}
私有类clazz;
私有静态MongoClient MongoClient=null;
私有静态MongoDatabase MongoDatabase=null;
私有静态对象映射器映射器=null;
@值(${mongodb.userIdentity.host}”)
私有字符串mongoHost;
@值(${mongodb.userIdentity.port}”)
私有国际Mongoport编号;
@值(${mongodb.userIdentity.database}”)
私有字符串mongoDatabaseName;
@值(${mongodb.userIdentity.userName})
私有字符串mongoUserName;
@值(${mongodb.userIdentity.authDB}”)
私有字符串mongouthdb;
@值(${mongodb.userIdentity.password})
私有字符串mongoPassword;
@值(${mongodb.userIdentity.isAuthenticationEnable})
私有布尔MongoiseEnable;
@值(${mongodb.userIdentity.isConnectionStringUsed}”)
私有布尔MongoiseConnectionString;
@值(${mongodb.userIdentity.connectionString}”)
私有字符串mongoConnectionString;
公共最终无效集合(类别集合)
{
this.clazz=clazzToSet;
}
/**
*实例化一个新的mongo基础存储库。
*@抛出异常
*/
公共MongoDbRepository()
{
//触发MongoDB连接初始化
if(mongoClient==null)
{
prepareMongoConnection();
}
其他的
{
//触发检查MongoDB客户端是否已连接的任何方法
mongoClient.getAddress();
}
//触发器ObjectMapper初始化
if(映射器==null)
prepareObjectMapper();
}
/**
*实例化一个新的mongoDB连接。
*@抛出异常
*/
私有void prepareMongoConnection()
{
if(mongoConnectionString!=null&&!mongoConnectionString.isEmpty())
{
布尔值isConnectionStringUsed=mongoIsConnectionStringUsed;
如果(使用断开连接字符串)
{
MongoClientURI=新的MongoClientURI(mongoConnectionString);
mongoClient=新的mongoClient(clientUri);
}
其他的
{
如果(启用)
{
MongoCredential credential=MongoCredential.createCredential(mongoUserName、mongoAuthDB、mongoPassword.toCharArray());
mongoClient=新的mongoClient(新服务器地址(mongoHost,mongoPortNumber),Arrays.asList(凭证));
}
其他的
mongoClient=新的mongoClient(mongoHost、mongoPortNumber);
}
//触发检查MongoDB客户端是否已连接的任何方法
mongoClient.getAddress();
//从mongoClient获取数据库。
mongoDatabase=mongoClient.getDatabase(mongoDatabaseName);
}
}
/**
*获取对象映射器。
*/
私有void prepareObjectMapper()
{
mapper=CommonFunctions.getObjectMapper();
}
/**
*从MongoDB获取MongoDB集合对象。
*
*@param collectionName是MongoDB集合的名称
*@return集合对象
*@抛出异常
*/
私有MongoCollection getCollection(String collectionName)引发异常
{
if(mongoClient==null)
prepareMongoConnection();
返回mongoDatabase.getCollection(collectionName);
}
/*------查找函数------*/
/**
*从mongoDB集合中查找一个文档。
*
*@param collectionName集合名称
*@param query查询文档-设置为空文档表示没有查询过滤。
*
*@return entityObj实体对象
*@抛出异常
*/
public T findOne(字符串集合名称,对象查询)引发异常
{
if(clazz==null)
抛出新的NullPointerException(“ST224-泛型类为null-在执行MongoDB操作之前设置泛型类”);
MongoCollection collection=getCollection(collectionName);
Document mongoDoc=collection.find(convertToBsonDocument(query)).first();
字符串jsonStr=mapper.writeValueAsStri
private Class<T> clazz;
private static MongoClient mongoClient = null;
private static MongoDatabase mongoDatabase = null;
private static ObjectMapper mapper = null;
@Value("${mongodb.userIdentity.host}")
private String mongoHost;
@Value("${mongodb.userIdentity.port}")
private int mongoPortNumber;
@Value("${mongodb.userIdentity.database}")
private String mongoDatabaseName;
@Value("${mongodb.userIdentity.userName}")
private String mongoUserName;
@Value("${mongodb.userIdentity.authDB}")
private String mongoAuthDB;
@Value("${mongodb.userIdentity.password}")
private String mongoPassword;
@Value("${mongodb.userIdentity.isAuthenticationEnable}")
private boolean mongoIsAuthEnable;
@Value("${mongodb.userIdentity.isConnectionStringUsed}")
private boolean mongoIsConnectionStringUsed;
@Value("${mongodb.userIdentity.connectionString}")
private String mongoConnectionString;
public final void setClazz(Class<T> clazzToSet)
{
this.clazz = clazzToSet;
}
/**
* Instantiates a new mongo base repository.
* @throws Exception
*/
public MongoDbRepository()
{
//Trigger MongoDB Connection initialization
if(mongoClient == null)
{
prepareMongoConnection();
}
else
{
// Trigger any method to check MongoDB client is connected
mongoClient.getAddress();
}
// Trigger ObjectMapper initialization
if(mapper == null)
prepareObjectMapper();
}
/**
* Instantiates a new mongoDB connection.
* @throws Exception
*/
private void prepareMongoConnection()
{
if (mongoConnectionString != null && !mongoConnectionString.isEmpty())
{
boolean isConnectionStringUsed = mongoIsConnectionStringUsed;
if(isConnectionStringUsed)
{
MongoClientURI clientUri = new MongoClientURI(mongoConnectionString);
mongoClient = new MongoClient(clientUri);
}
else
{
if(mongoIsAuthEnable)
{
MongoCredential credential = MongoCredential.createCredential(mongoUserName, mongoAuthDB, mongoPassword.toCharArray());
mongoClient = new MongoClient( new ServerAddress(mongoHost, mongoPortNumber), Arrays.asList(credential));
}
else
mongoClient = new MongoClient(mongoHost, mongoPortNumber);
}
// Trigger any method to check MongoDB client is connected
mongoClient.getAddress();
// Get Database from mongoClient.
mongoDatabase = mongoClient.getDatabase(mongoDatabaseName);
}
}
/**
* Get an objectMapper.
*/
private void prepareObjectMapper()
{
mapper = CommonFunctions.getObjectMapper();
}
/**
* Get the MongoDB collection object from MongoDB.
*
* @param collectionName is Name of a MongoDB collection
* @return Collection object
* @throws Exception
*/
private MongoCollection<Document> getCollection(String collectionName) throws Exception
{
if(mongoClient == null)
prepareMongoConnection();
return mongoDatabase.getCollection(collectionName);
}
/* ------- Find functions ------- */
/**
* Find one documents from mongoDB collection.
*
* @param collectionName the collection name
* @param query the query document - set to empty document means no query filtering.
*
* @return entityObj the entity Object
* @throws Exception the exception
*/
public T findOne(String collectionName, Object query) throws Exception
{
if(clazz == null)
throw new NullPointerException("ST224 - Generic class is null - set the generic class before perform MongoDB operation");
MongoCollection<Document> collection = getCollection(collectionName);
Document mongoDoc = collection.find(convertToBsonDocument(query)).first();
String jsonStr = mapper.writeValueAsString(mongoDoc);
T entityObj = mapper.readValue(jsonStr, clazz);
return entityObj;
}