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

Spring-读取属性(java.lang.IllegalArgumentException:无法解析字符串值“${name}”中的占位符'name')

宣煜
2023-03-14

我看到下面的错误,当我试图读取属性从类路径在我的Spring项目中使用的@属性源注释。我已经粘贴了我的代码下面,请你帮助让我知道我错过了什么?谢谢。

警告:在上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.BeanCreation异常:创建名称为“客户仓库”的bean时出错:自动构建依赖项注入失败;嵌套异常java.lang.IllegalArgument异常:无法解析字符串值“${name}”中的占位符“name”

线程“main”组织中出现异常。springframework。豆。工厂BeanCreationException:创建名为“customerRepository”的bean时出错:自动连线依赖项的注入失败;嵌套的例外是java。lang.IllegalArgumentException:无法解析字符串值“${name}”中的占位符“name”

AppConfig。JAVA

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;

import com.vivek.service.CustomerService;
import com.vivek.service.CustomerServiceImpl;

@Configuration
@ComponentScan("com.vivek")
@PropertySource("app.properties")
public class AppConfig {

    @Bean
    public static PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer(){
        PropertyPlaceholderConfigurer p = new PropertyPlaceholderConfigurer();
        //p.setIgnoreUnresolvablePlaceholders(true);
        return p;
    }

    @Bean(name="customerService")
    @Scope("prototype")
    public CustomerService getCustomerService(){
        CustomerServiceImpl service = new CustomerServiceImpl();
        //service.setRepository(getCustomerRepository());
        return service;
    }

/*  @Bean(name="customerRepository")
    public CustomerRepository getCustomerRepository(){
        CustomerRepository repository = new HibernateCustomerRepository();
        return repository;
    }*/
}

应用JAVA

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.vivek.service.CustomerService;
import com.vivek.service.CustomerServiceImpl;

public class Application {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        CustomerService service = context.getBean("customerService",CustomerService.class);
        System.out.println(service);
        CustomerService service1 = context.getBean("customerService",CustomerService.class);
        System.out.println(service1);

        System.out.println(service.findAll().get(0).getFirstName());
        System.out.println(service.findAll().get(0).getLastName());
    }

}

HibernateCustomerRepository。JAVA

package com.vivek.repository;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.vivek.model.Customer;

@Repository("customerRepository")
public class HibernateCustomerRepository implements CustomerRepository {

    /* (non-Javadoc)
     * @see com.vivek.repository.CustomerRepository#findAll()
     */
    @Value("${name}")
    private String name;

    @Override
    public List<Customer> findAll(){
        List<Customer> customerList = new ArrayList<Customer>();

        Customer customer = new Customer();
        customer.setFirstName(name);
        customer.setLastName("Shah");

        customerList.add(customer);

        return customerList;
    }
}

CustomerServiceImpl。JAVA

package com.vivek.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.vivek.model.Customer;
import com.vivek.repository.CustomerRepository;
import com.vivek.repository.HibernateCustomerRepository;

public class CustomerServiceImpl implements CustomerService {

    /* (non-Javadoc)
     * @see com.vivek.service.CustomerService#findAll()
     * 
     */

    private CustomerRepository repository;

    @Autowired
    public void setRepository(CustomerRepository repository) {
        this.repository = repository;
    }

    @Override
    public List<Customer> findAll(){
        CustomerRepository repository = new HibernateCustomerRepository();
        return repository.findAll();
    }



}

app.properties

name=Arnold

共有1个答案

王伯寅
2023-03-14

可能您需要在spring配置文件中声明PropertySourcesPlaceholderConfigurer。

 <bean 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>config.properties</value>
    </property>
 </bean>
 类似资料:
  • 问题内容: 我正在尝试使用文件中的属性,但它似乎不起作用。 这是我的代码: 此类使用批注获取属性。它也被声明为Spring Service并链接到我的文件: 使用,我将此文件链接到我的文件: 这确实有意义,对吗? 但是,当我尝试启动项目时,Tomcat抛出此异常: 或者,简而言之: 编辑: 这是我的web.xml: 我的infraContext.xml被导入到另一个名为applicationCon

  • 问题内容: 我有我的配置: 我得到错误 我知道这可能缺少属性文件,但是我在类路径中恰好有它。有什么不见了? 我的web.xml: 问题答案: 你的应用程序中可能有多个。尝试在超类的方法上设置一个断点,看看在应用程序启动时是否多次调用了该断点。如果不止一个,则可能需要查看配置属性,以便你的应用程序可以正常启动。

  • 问题内容: 我对春天还很陌生,所以请问这是一个愚蠢的问题。当我尝试启动程序时,出现以下错误:。执行以下代码时,将引发错误: 资源文件夹中存在一个名为的属性文件,其中包含主机和端口的信息。我不确定在哪里定义(如果有的话)。也许甚至没有定义,这就是问题所在。我需要将其更改为类似的东西还是缺少其他内容? 问题答案: 您没有正确读取属性文件。propertySource应该将参数传递为:或。将注释更改为:

  • 我的数据库配置类: 和AppConfig: } 嗨,我上面有个错误,我不知道怎么修复,你能帮我吗?在添加这个bean之前,我的项目运行良好: }

  • 我想让运行中的Spring(引导)应用程序的所有属性占位符及其解析值都可用于进程监控。在第一步中,可以将它们写入日志,或者创建一个类似于application.pid文件的'resolved.properties'文件。应该考虑使用属性占位符(隐式/显式)的所有属性。 动机:在操作过程中通常很难知道解析属性的值。系统属性或命令行参数是“可见的”,但代码中隐藏的默认值(如)很难找到。我希望能够回答“

  • 我有个例外 我也尝试过使用JarLoader和PropertiesLauncher,但运气并不好。 我确实在application.properties中定义了属性sysm.client.api.path,但为了更好地衡量,我还将它作为-d参数-dsysm.client.api.path=my-path添加到命令行中。 注意:IntelliJ没有将其作为a-jar运行;相反,它在一个大型类路径命令