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

无法解析某些属性的占位符

夏侯昊明
2023-03-14

我的数据库配置类:

@EnableTransactionManagement
public class DatabaseConfiguration {

    @Bean(name = "dbDataSource")
    @Profile("!development")
    public DataSource dataSource(@Value("${db.jndi}") String dbJndiName) {
        JndiDataSourceLookup lookup = new JndiDataSourceLookup();
        return lookup.getDataSource(dbJndiName);
    }

    @Profile("development")
    public DataSource dataSource(@Value("${development.db.driver}") String dbDriver,
            @Value("${development.db.url}") String dbUrl,
            @Value("${development.db.username}") String dbUsername,
            @Value("${development.db.password}") String dbPassword) {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource(dbUrl);
        driverManagerDataSource.setDriverClassName(dbDriver);
        driverManagerDataSource.setUsername(dbUsername);
        driverManagerDataSource.setPassword(dbPassword);
        return driverManagerDataSource;
    }

}

和AppConfig:

@Import({DatabaseConfiguration.class, MailSenderConfiguration.class})
public class AppConfig {
@Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
    PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
    propertyPlaceholderConfigurer.setLocation(new ClassPathResource("application.properties"));
    propertyPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
    return propertyPlaceholderConfigurer;
}

}

嗨,我上面有个错误,我不知道怎么修复,你能帮我吗?在添加这个bean之前,我的项目运行良好:

@PropertySource("classpath:mail.properties")
public class MailSenderConfiguration {

@Value("${mail.smtp.host}")
private String mailHost;

@Value("${mail.smtp.port}")
private int mailPort;

@Value("${mail.username}")
private String mailUsername;

@Value("${mail.password}")
private String mailPassword;

@Value("${mail.protocol}")
private String mailProtocol;

@Value("${mail.smtp.auth}")
private boolean mailAuth;

@Value("${mail.smtp.starttls.enable}")
private String mailEnableTls;

@Value("${mail.smtp.debug}")
private boolean mailDebug;

@Bean
public JavaMailSender getMailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

    mailSender.setHost(mailHost);
    mailSender.setPort(mailPort);
    mailSender.setUsername(mailUsername);
    mailSender.setPassword(mailPassword);

    Properties mailProperties = new Properties();
    mailProperties.put("mail.transport.protocol", mailProtocol);
    mailProperties.put("mail.smtp.auth", mailAuth);
    mailProperties.put("mail.smtp.starttls.enable", mailEnableTls);
    mailProperties.put("mail.smtp.debug", mailDebug);
    mailSender.setJavaMailProperties(mailProperties);

    return mailSender;
}

@Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
    PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
    propertyPlaceholderConfigurer.setLocation(new ClassPathResource("mail.properties"));
    propertyPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
    return propertyPlaceholderConfigurer;
}

}

共有1个答案

咸正平
2023-03-14

MailSenderConfiguration中的属性源配置似乎重写了AppConfig中的属性源配置。

您可以从MailSenderConfiguration中删除PropertyPlaceHolderConfigureer bean,并在AppConfig中为“mail.properties”设置附加位置(使用setLocations()方法而不是setLocation()):

@Import({DatabaseConfiguration.class, MailSenderConfiguration.class})
public class AppConfig {
@Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
    PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
    propertyPlaceholderConfigurer.setLocations(new ClassPathResource("application.properties"), new ClassPathResource("mail.properties"));
    propertyPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
    return propertyPlaceholderConfigurer;
}
 类似资料:
  • 问题内容: 我有我的配置: 我得到错误 我知道这可能缺少属性文件,但是我在类路径中恰好有它。有什么不见了? 我的web.xml: 问题答案: 你的应用程序中可能有多个。尝试在超类的方法上设置一个断点,看看在应用程序启动时是否多次调用了该断点。如果不止一个,则可能需要查看配置属性,以便你的应用程序可以正常启动。

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

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

  • 我正在尝试使用骆驼JavaDSL将文件路由到SFTP服务器,如下所示: 但是,当消息到达此终结点时,Camel 会引发以下异常: 我可以看到在堆栈跟踪中打印的交易所上设置了目标目录属性。如果我替换 exchangeProperty(destinationDir) 在具有实际目标目录(tmp/目标/dir 1/)的路由中,它工作正常。问题是,我需要目标目录是动态的。我尝试过在路由中使用( 在调试Ca

  • 我看到下面的错误,当我试图读取属性从类路径在我的Spring项目中使用的@属性源注释。我已经粘贴了我的代码下面,请你帮助让我知道我错过了什么?谢谢。 警告:在上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.BeanCreation异常:创建名称为“客户仓库”的bean时出错:自动构建依赖项注入失败;嵌套异常java.lang.Illega

  • 我正在尝试从命令行读取配置文件。我主要是这样做的: 然后创建了.jar文件,然后进入包含jar的文件夹,并尝试通过执行以下操作来运行它: 我的application.yml文件是我的JAR的同一个文件夹。我还将路径设置为,但错误仍然存在。是不是路径写错了?还是我必须在代码中添加/修改一些东西? 编辑