我的maven项目有3个模块,web, service, common
我项目的某些部分是这样的:
demo-parent:
--web
--src
--main
--java
--Application.java
--resources
--application.properties
--application-mysql.properties
--service
--common
--src
--main
--java
--ErrorCode.java
--resources
--application-errors.properties
在网络模块 Application.java 中,我想从 common 模块 application-errors.properties 中读取内容。
这是我在 common 中的 ErrorCode.java:
@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties
@ConfigurationProperties(locations = "classpath:application-errors.properties")
public class ErrorCode {
private int code;
private String message;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
因为我想在 web 模块中使用这个 application-errors.properties,所以在 web 模块中,在 Application. java:
@SpringBootApplication
@PropertySources({
@PropertySource("application-mysql.properties"),
@PropertySource("application-errors.properties")
})
@EnableConfigurationProperties({ErrorCode.class})
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Application.class);
application.run(args);
}
}
现在,我将 EnableConfigurationProperties 添加到包含 main 方法的 Application.java 中,我可以从 application 中获取键和值-errors.properties 文件,我的问题是:
如果我想在服务等其他模块中调用appliction-errors.propertie,而服务没有main方法,我该怎么办?
我们可以使用 java Properties 类读取属性文件,但我想使用 spring 方式。
欢迎任何建议,在此先感谢。
最佳答案
无需访问 application-error.properties,您可以在其他模块的服务中自动连接 ErrorCode 类(假设 ErrorCode 存在于这些服务的构建类路径中)。
首先定义错误消息配置组件。
@Configuration
@ConfigurationProperties(locations = "classpath:application-error.properties")
public class ErrorCode {
private int code;
private String message;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
在服务中自动连接配置组件
@Component
public class Service {
@Autowired
private ErrorCode errorCode;
public void accessErrorCode() {
System.out.println(errorCode.getCode()); //Print code property
System.out.println(errorCode.getMessage()); //print message property
}
}
在spring应用中启用自动配置
@SpringBootApplication
@EnableAutoConfiguration
@EnableConfigurationProperties
public class Application {
public static void main(String[] args) {
SpringApplication application = new SpringApplicationBuilder(Application.class).web(false).application();
ConfigurableApplicationContext context = application.run(args);
Service s = context.getBean(Service.class);
s.accessErrorCode();
}
}
根据 EnableConfigurationProperties 的 Javadocs,
{@link ConfigurationProperties} beans can be registered in the standard way (for * example using {@link Bean @Bean} methods) or, for convenience, can be specified * directly on this annotation.
因此,所有使用注释 ConfigurationProperties 定义的 beans 都在应用程序上下文中被检测到,并且可以在其他服务中自动连接。
关于java - spring boot 从不同的 maven 模块读取属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33995035/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val