我们可以将@Autow的注释用作构造函数参数吗?
就我而言,我正在尝试使用环境
类来读取属性。我尝试了两个选择。
选项1-我尝试使用@Autowired
enviornment
类,如下示例所示。
@Service("idmHelper")
public class IdmHelper {
@Autowired
Environment env;
public IdmHelper() {
env.getProperty('property-name')
...
}
}
在这里给出nullpointerexception 。因为
env
为null。
选项2-我尝试使用@autowired
作为参数,如下示例所示。
@Service("idmHelper")
public class IdmHelper {
public IdmHelper(@Autowired Environment env) {
env.getProperty('property-name')
...
}
}
此处env> env
将获得一个对象和我可以访问属性。但是我遇到以下错误;
我是Spring-Boot的新手错误。我怀疑这与启动构造函数有关。如果我错了,请纠正我。
这是我的完整代码; full-code-of-indm-helper-class
In my case I am trying to use Environment
class to read properties. I tried two options.
Option 1 - I tried to use @Autowired
the Enviornment
class as shown in the following example.
@Service("idmHelper")
public class IdmHelper {
@Autowired
Environment env;
public IdmHelper() {
env.getProperty('property-name')
...
}
}
Here it gives a NullPointerException
. Becuase env
is null.
Option 2 - I tried to use @Autowired
inside the constructor as an argument, as shown in the following example.
@Service("idmHelper")
public class IdmHelper {
public IdmHelper(@Autowired Environment env) {
env.getProperty('property-name')
...
}
}
Here the env
will get an object and I can access the properties. But I am getting the following error;
I am new to spring-boot, can someone explain why I am getting this error. I doubt that it is something to do with initiating the constructor. Please correct me If I am wrong.
This is my full code;
Full-code-of-idm-helper-class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
其他解决方案:
Try:
Other solution :