java spring 启动_基于Spring启动的Java应用程序不工作

我有基于Spring Booad的Java应用程序。我使用java.util.properties从src/main/resources(默认路径)中的application.properties文件读取属性。我刚刚定义了getter和setter来读取道具。

代码如下:

public class PropertyReader {

String host;

public String getHost() {

Properties properties = new Properties();

try {

File file = ResourceUtils.getFile("classpath:application.properties");

InputStream in = new FileInputStream(file);

properties.load(in);

} catch (IOException e) {

}

return host = properties.getProperty("spring.mysql.host");

}

public void setHost(String host) {

this.host = host;

}

}

现在,在另一个类中,只需创建这个类的对象并尝试调用get host()方法来获取主机IP地址。

PropertyReader pr = new PropertyReader();

String host = pr.getHost();

PoolProperties p = new PoolProperties();

p.setUrl("jdbc:mysql://" + host + "/ci");

正在获取以下异常:

Caused by: java.net.UnknownHostException: null

at java.net.InetAddress.getAllByName0(InetAddress.java:1280) ~[na:1.8.0_151]

at java.net.InetAddress.getAllByName(InetAddress.java:1192) ~[na:1.8.0_151]

at java.net.InetAddress.getAllByName(InetAddress.java:1126) ~[na:1.8.0_151]

at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:188) ~[mysql-connector-java-5.1.45.jar!/:5.1.45]

at com.mysql.jdbc.MysqlIO.(MysqlIO.java:300) ~[mysql-connector-java-5.1.45.jar!/:5.1.45]

... 44 common frames omitted

不使用properties类,如果我只是硬编码IP地址,它绝对可以正常工作。不知道代码中有什么问题,因此属性读取器无法工作。

遵循application.properties内容:

spring.mysql.host=35.154.83.162

更新::::

下面是更新的代码:

@Component

@Configuration

public class UnitDBHelper {

@Autowired

private Environment env;

public UnitDBHelper() {

String host = env.getProperty("spring.mysql.host");

PoolProperties p = new PoolProperties();

InputStream input = null;

p.setUrl("jdbc:mysql://" + host + "/ci");

}

}

获取NPE异常:

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.

2018-05-30 15:42:29.532 ERROR 9870 --- [ main] o.s.boot.SpringApplication : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'unitDBHelper' defined in URL [jar:file:/tmp/unitdbamqpservice-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/infy/ci/unitdbamqpservice/UnitDBHelper.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.infy.ci.unitdbamqpservice.UnitDBHelper$$EnhancerBySpringCGLIB$$24a2dca6]: Constructor threw exception; nested exception is java.lang.NullPointerException

Caused by: java.lang.NullPointerException: null

at com.infy.ci.unitdbamqpservice.UnitDBHelper.(UnitDBHelper.java:39) ~[classes!/:0.0.1-SNAPSHOT]

at com.infy.ci.unitdbamqpservice.UnitDBHelper$$EnhancerBySpringCGLIB$$24a2dca6.() ~[classes!/:0.0.1-SNAPSHOT]

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_151]

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_151]

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_151]

at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_151]

at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]

... 27 common frames omitted

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值