I am getting ‘Field required a bean of type that could not be found.’ error Field emailConfig in com.javavogue.demo.service.EmailService required a bean of type com.javavogue. demo.service. EmailConfig that could not be found.The injection point has the following annotations:– @org.springframework.beans.factory.annotation.Autowired(required=true)
Related Tutorials :
- Spring boot tutorials
- How to create rest web service in spring boot
- How to change spring boot default portĀ
I have EmailService and EmailCofig Class as below :
package com.javavogue.demo.service;
import org.springframework.boot.context.properties.ConfigurationProperties;
public class EmailConfig {
private String username;
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "EmailConfig [username=" + username + ", email=" + email + "]";
}
}
package com.javavogue.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class EmailService {
@Autowired
private EmailConfig emailConfig;
public void sendEmail(){
System.out.println("EmailCOnfig ==="+ emailConfig.toString());
}
}
Solution :
Use @ServiceĀ Or @Component anotation In EmailConfig Class Above .
package com.javavogue.demo.service;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Service
public class EmailConfig {
private String username;
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "EmailConfig [username=" + username + ", email=" + email + "]";
}
}
Full Error Trace As below
Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled.
2020-04-18 12:26:04.552 ERROR 2008 — [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field emailConfig in com.javavogue.demo.service.EmailService required a bean of type ‘com.javavogue.demo.service.EmailConfig’ that could not be found.
The injection point has the following annotations:
– @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type ‘com.javavogue.demo.service.EmailConfig’ in your configuration.