我是Spring-Boot应用程序的新手,并尝试在Spring Boot应用程序中读取外部属性文件,该应用程序位于我的资源文件夹中,除了application.properties 之外,但是会出现以下异常,
#include<stdio.h>
#include<cs50.h>
#include<math.h>
int main()
{
float n;
do{ printf("change owed?\n");
n = get_float();
}while (n<0);
int z,a,b,c,d;
z = n*100;
a = z/25;
b = (z%25)/10;
c = ((z%25)%10)/5;
d = (((z%25)%10)%5)/1;
if(z>25)
{
printf("you will have %i quarter's'\n",a);
}
else if(z>10)
{printf("you will have %i dime's'\n",b);
}
else if(z>5)
{printf("you will have %i nickel's'\n",c);
}
else if(z>1)
{printf("you will have %i pennie's'\n",d);
}
{if(a>10)
{
printf("you will have %i dime's'\n",b);
}
else if(a>5)
{printf("you will have %i nickel's'\n",c);
}
else if(a>1)
{printf("you will have %i pennie's'\n",d);
}
{if(b>5)
{printf("you will have %i nickel's'\n",c);
}
else if(b>1)
{printf("you will have %i pennie's'\n",d);
}
{if(c>1)
{printf("you will have %i pennie's'\n",d);
}}}}
printf("you will get %i coin's'\n",a+b+c+d);
}
我的Config类如下,
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.test.config.MyInetgrationApplicationConfig]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/myIntegration.properties]
2017-09-06 17:27:04.866 ERROR 10512 --- [ost-startStop-1] o.s.b.f.s.DefaultListableBeanFactory : Destroy method on bean with name 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory' threw an exception
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@225fa519: startup date [Wed Sep 06 17:27:04 IST 2017]; root of context hierarchy
at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:414) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
我的应用类,
@Configuration
@PropertySource(name="myIntegrationProperties", value ="myIntegration.properties")
public class MyInetgrationApplicationConfig {
/**
* Rest template.
*
* @return the rest template
*/
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Value("${datasource.name}")
private String dataSourceName;
@Value("${datasource.driver}")
private String dataSourceDriver;
@Value("${datasource.url}")
private String dataSourceUrl;
@Value("${datasource.username}")
private String dataSourceUserName;
@Value("${datasource.password}")
private String dataSourcePassword;
@Bean
@Primary
public DataSource dataSource() {
DataSource dataSource = new DataSource();
dataSource.setName(dataSourceName);
dataSource.setUrl(dataSourceUrl);
dataSource.setDriverClassName(dataSourceDriver);
dataSource.setUsername(dataSourceUserName);
dataSource.setPassword(dataSourcePassword);
return dataSource;
}
}
答案 0 :(得分:3)
您需要添加claapath:
@PropertySource("classpath:myIntegration.properties")
myIntegration.properties
文件应放在/ src / main / resources下,以便在运行时classpath
上可用。
答案 1 :(得分:1)
尝试为您的资源文件路径提供类路径引用,如下所示:
10
答案 2 :(得分:0)
我无法使用@Value("${datasource.name}")
获取值,所以我在自动装配Environment
之后尝试使用environment.getProperty("datasource.name")
它为我工作。
以下是示例代码
@Configuration
@PropertySource("classpath:myIntegration.properties")
public class MyInetgrationApplicationConfig {
@Autowired
private Environment environment;
@Bean
@Primary
public DataSource dataSource() {
DataSource dataSource = new DataSource();
dataSource.setName(environment.getProperty("datasource.name"));
dataSource.setUrl(environment.getProperty("datasource.url"));
.
.
.
.
return dataSource;
}
}