[英]Springboot Unable to access a resource inside a dependent jar's classpath
I have a spring boot JAR MyMain.jar, which has dependent jars inside BOOT-INF/lib. 我有一个弹簧启动JAR MyMain.jar,它在BOOT-INF / lib中有依赖的jar。
I am trying to access a property file inside the BOOT-INF/lib/MyDep.jar/abcd.properties. 我正在尝试访问BOOT-INF / lib / MyDep.jar / abcd.properties中的属性文件。
I tried the below code. 我尝试了以下代码。
InputStream in = new ClassPathResource("abcd.properties").getInputStream();
System.out.println("InputStream : "+in);
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
This works perfect inside my Eclipse IDE. 这在我的Eclipse IDE内完美运行。 but when I run it on command line as jar it doesn't print anything.
但是当我在命令行中将其作为jar运行时,它什么也不会打印。
org.springframework.boot.loader.jar.ZipInflaterInputStream@214c265e org.springframework.boot.loader.jar.ZipInflaterInputStream@214c265e
readLine() gives null during command line run. 在命令行运行期间,readLine()给出null。
Could anyone please help ! 谁能帮忙!
Alternatively, It's work for me. 或者,它对我有用。
Create this class at the app project 在应用程序项目中创建此类
@Configuration
@ComponentScan("yourpackage")
public class AppConfig {
@Configuration
@PropertySource("common.properties")
static class default{}
}
If you want to read a config file by different profiles (-Dspring.profiles.active) 如果要通过其他配置文件(-Dspring.profiles.active)读取配置文件
@Configuration
@ComponentScan("yourpackage")
public class AppConfig {
@Profile("alpha")
@Configuration
@PropertySource("common-alpha.properties")
static class Alpha{}
@Profile("staging")
@Configuration
@PropertySource("common-staging.properties")
static class Staging{}
@Profile("production")
@Configuration
@PropertySource("common-production.properties")
static class Production{}
}
You can use spring @Autowired annotation as below, but make sure you annotate your class with @Component or similar ones. 您可以按如下方式使用spring @Autowired批注,但请确保使用@Component或类似的类对类进行批注。
@Autowired
Environment env;
You can get the property in your properties file 您可以在属性文件中获取属性
env.getProperty("property")
i hope it helps. 我希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.