我刚开始玩春运,我正在安装一台新服务器。我的公司在不同的文件中分离配置(例如: jdbc.properties、smtp.properties等)。所有配置都被放入文件夹"property- files“中(作为”webapp“文件夹的兄弟),并放入具有给定应用程序名称的专用文件夹中;例如,如果我的应用程序被调用:”美妙-服务器“,那么我的所有配置文件都将是in:"@TomcatFolder/property-files/wonderful-server/".。
我的想法是使用绝对文件路径访问属性文件,如:"file:${catalina.home}/property-files#{server.servlet.context-path}/smtp.properties".但是,如果我试图从@Configuration类访问"server.servlet.context-path“,我将获得null。
我试着加入application.properties:
server.servlet.context-path=/wonderful-server并添加到我的@Configuration类中:
@Value("${server.servlet.context-path=/wonderful-server}") String contextPath;但是当spring启动时,contextPath包含null。如果我使用#而不是$,情况也是一样的。
然后,我尝试将我的@SpringBootApplication类的主要内容放入其中:
System.setProperty("server.servlet.context-path", "/formx-server");并在我的@Configuration类中使用:
String contextPath = System.getProperty("server.servlet.context-path");但是当spring启动时,contextPath包含null。如果我使用:
@Value("#{systemProperties['server.servlet.context-path']}") private String contextPath;或者:
@Value("#{server.servlet.context-path}") private String contextPath;我的@configuration类非常简单,例如:
@Configuration
public class EmailConfig {
@Bean
public JavaMailSender getJavaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
try {
Properties props = PropertiesLoaderUtils.loadProperties( new FileSystemResource(System.getProperty("catalina.home")+"/property-files/wonderful-server/smtp.properties"));
mailSender.setHost(props.getProperty("mail.host"));
mailSender.setPort(Integer.parseInt(props.getProperty("mail.port")));
mailSender.setUsername(props.getProperty("mail.username"));
mailSender.setPassword(props.getProperty("mail.password"));
Properties properties = mailSender.getJavaMailProperties();
properties.put("mail.transport.protocol", props.getProperty("mail.transport.protocol"));
properties.put("mail.smtp.auth", props.getProperty("mail.smtp.auth"));
properties.put("mail.smtp.starttls.enable", props.getProperty("mail.smtp.starttls.enable"));
properties.put("mail.debug", props.getProperty("mail.debug"));
} catch (IOException e) {
// e.printStackTrace();
LOG.error("Error to send email: "+e.getMessage());
}
return mailSender;
}
}在这个类中,我使用了一个带有静态上下文路径的绝对路径,我尝试使用它作为变量。
提前:感谢大家宝贵的时间。
发布于 2019-07-01 10:02:51
您使用的是=而不是:
内部属性:
server.servlet.context-path=/wonderful-server但内部配置:
默认值跟随在:之后
@Value("${server.servlet.context-path:/wonderful-server}") String contextPath;https://stackoverflow.com/questions/56833593
复制相似问题