[英]Can we Autowired below class into Configuration class in springboot
I have a Component class and a config class,so can we autowire component class which uses @value internally,I tried using it but it throws exception,Can anyone please help me understanding 我有一个Component类和config类,所以我们可以在内部使用@value自动装配组件类吗,我尝试使用它,但是会抛出异常,任何人都可以帮助我理解
@Component
public class UserAction {
@Value("${cp.user.name}")
private String userName;
@Value("${cp.user.actiontype}")
private String actionType;
@Value("${cp.user.designation}")
protected Designation designation;
public void show() {
System.out.println(userName);
System.out.println(actionType);
System.out.println(designation);
}
}
@Configuration
@ComponentScan("com.example")
public class AppConfig {
@Autowired
UserAction userAction;
------
}
So My question is : can I autowire my UserAction
bean into my AppConfig
class? 所以我的问题是:我可以将UserAction
bean自动连接到AppConfig
类中吗? I tried using it but its throwing exception,so can we autowire a component which internally uses @value
: 我尝试使用它,但是它抛出异常,所以我们可以自动装配内部使用@value
的组件@value
?
Unable to start web server; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'AppConfig ': Unsatisfied dependency expressed through field 'userAction'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userAction': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'cp.user.actiontype' in value "${cp.user.actiontype}"
Yes you can Autowire
beans in Configuration class, but the issue here is that a property in your bean has no value set in properties/yml
file 是的,您可以在Configuration类中自动Autowire
bean,但是这里的问题是bean中的properties/yml
在properties/yml
文件中没有设置值
@Value("${cp.user.actiontype}")
set the property cp.user.actiontype
in your .properties or .yml file 在.properties或.yml文件中设置属性cp.user.actiontype
语法是yml的错误,现在可以正常使用,谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.