Reputation: 241
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
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?
I tried using it but its throwing exception,so can we autowire a component which internally uses @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}"
Upvotes: 0
Views: 72
Reputation: 3862
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
@Value("${cp.user.actiontype}")
set the property cp.user.actiontype
in your .properties or .yml file
Upvotes: 1