当前位置: 首页 > 知识库问答 >
问题:

在基于Spring web的项目中使用Spring配置文件时,无法解决该问题

戚成礼
2023-03-14

正在申请中。属性:spring。个人资料。active=DEV,在DEV config文件中:提到了所有mongo连接属性

并添加了配置java文件,如

@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概要文件没有解析

共有1个答案

王才英
2023-03-14

这是主要课程:

`@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,MongoAutoConfiguration.class,MongoDataAutoConfiguration.class})

@PropertySource(“类路径:application.properties”)

公共类应用程序开始扩展SpringBootServlet初始工具{

公共静态void main(字符串[]args)

{

    SpringApplication.run(ApplicationStart.class,args);

}

}`

以下是属性文件:

## MongoDB Connection Properties-----------------

mongodb。用户身份。数据库=用户身份

mongodb.user我dentity.isConnectionStringUSE=false

mongodb.user我dentity.connection字符串=mongodb://localhost:27017/?replicaSet=surveillens

mongodb.user我dentity.host=localhost

mongodb。用户身份。端口=27017

mongodb。用户身份。isAuthenticationEnable=false

mongodb.user我dentity.userName=sa

mongodb。用户身份。密码=Test@123

mongodb.userdentity.authDB=admin

mongodb。用户身份。collectionName=CreditScore

用户身份。ValidKeySet=电子邮件;电话号码_身份证件

用户身份。logsFolder=/识别码/

用户身份。insertBatchSize=100

下面是文件。使用所有这些属性的java文件“@Configuration public abstract class 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;
}

}`

 类似资料:
  • 当我尝试运行应用程序时,它工作正常。但是当我构建签名apk时,出现了错误 失败:构建失败,出现异常。 问题:任务“:app:lintVitalRelease”的执行失败 无法解析配置“:image_picker_android:调试单元测试运行时类路径”的所有项目。无法转换 bcprov-jdk15on-1.68.jar (org.bouncycastle:bcprov-jdk15on:1.68)

  • 我正在用spring boot编写代码,它将csv作为输入,创建mongodb集合并将其插入mongodb。 目前,我坚持在基于spring boot的代码中使用mongodb。在MongoRepository接口上使用save方法时,我收到NullPointerException。 可能这个问题是由于不正确的配置在application.yml文件下面是mongodb特定的变化在applicat

  • 问题内容: 我想在页面中显示Web应用程序的当前运行版本。该项目基于Maven,Spring和Wicket。 我想以某种方式获取maven的值并在我的spring XML文件中使用它,类似于我使用Spring PropertyPlaceholderConfigurer读取属性文件以获取我在应用程序中使用的设置的方式。 如果我在Spring配置中将maven 用作变量,则可以执行以下操作: 我怎样才

  • 本文向大家介绍解决无法配置SQL2005问题,包括了解决无法配置SQL2005问题的使用技巧和注意事项,需要的朋友参考一下 问题:点击“SQL Server Configuration Manager”却显示“无法连接到WMI 提供程序 请注意,你只能使用SQL Server 配置管理器来管理SQL Server 2005服务器。找不到指定的模块。[0x8007007e]” 解决方法: 1、到sy

  • 我需要创建基于特定环境属性文件的WAR文件。 因此我创建了2个属性文件, > application.dev.properties null