Error creating bean with name : Unsatisfied dependency expressed - java

I'm getting this error while trying to start my app. I've looked at many similar problems and topics but none seems to help me.
Error creating bean with name 'databaseManager': Unsatisfied dependency
expressed through field 'articleRepo'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
'pl.dzejkobdevelopment.database.repositories.ArticleRepo' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency
annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
#Repository
public interface ArticleRepo extends CrudRepository<Article, Long> {
}
and...
#Service
public class DatabaseManager {
#Autowired
private ArticleRepo articleRepo;
#Autowired
private CommentRepo commentRepo;
#Autowired
private TagRepo tagRepo;
#Autowired
private UserRepo userRepo;
public void addArticle(Article article){
article.getTags().forEach(tag ->addTag(tag));
articleRepo.save(article);
}
public List<Comment> findComments(User user){
return commentRepo.findByCommentAuthor(user);
}
private void addTag(Tag tag){
tagRepo.save(tag);
}
}
and...
#Configuration
//#ComponentScan(basePackages="pl.dzejkobdevelopment.database.repositories")
public class AppConfig {
#Bean
public WebsiteProporties websiteProporties(){
return new WebsiteProporties();
}
#Bean
public StorageProperties storageProporties(){ return new StorageProperties();}
#Bean
public DatabaseManager databaseManager(){ return new DatabaseManager();}
}
}
Uncommenting ComponentScan doesn't help.
EDIT
Changeing ComponentScan for EnableJpaRepositories gives this error:
Error creating bean with name 'databaseManager': Unsatisfied dependency expressed through field 'articleRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'articleRepo': Cannot create inner bean '(inner bean)#14a1d6d' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#14a1d6d': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

Try to use
#EnableJpaRepositories("pl.dzejkobdevelopment.database.repositories")
instead of ComponentScan.

Related

cucumber.runtime.CucumberException: Error creating bean with name

this is my step definition class,
#ContextConfiguration("classpath*:/applicationContext.xml")
public class Setup {
#Autowired
SetupPageObjects setupPageObjects;
#Autowired
private setUp_Helper dataHelper;
#When("xxxxx")
public void public void user_navigates_to_setup_page() {
}
}
class:
#Component
public class SetupPageObjects extends TestCoordinator {
codes()
}
Runner class:
#RunWith(Cucumber.class)
#CucumberOptions(tags = "#Varun",
features = "features",
glue = "gluepath",
monochrome = true
)
#ContextConfiguration("classpath*:/applicationContext.xml")
public class CucumnerContinousITSuite {
}
Error:
cucumber.runtime.CucumberException: Error creating bean with name 'com.application.test.cucumberIntegrationTest.stepdefenitions.Setup': Unsatisfied dependency expressed through field 'SetupPageObjects'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.application.test.cucumberIntegrationTest.pageobjectrepository.SetupPageObjects' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}

MappingMongoConverter not available for autowiring on unit test with #EnableMongoAuditing

I have a spring boot application where I have enabled mongo auditing. The application starts fine and stores mongo documents with all auditing fields (createdDate, updatedDate, etc.). However, when running unit test I get the following exception:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoAuditingHandler': Cannot create inner bean '(inner bean)#115c946b' of type [org.springframework.data.mongodb.config.MongoAuditingRegistrar$MongoMappingContextLookup] while setting constructor argument; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)#115c946b': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.mongodb.core.convert.MappingMongoConverter' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)#115c946b': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.mongodb.core.convert.MappingMongoConverter' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.mongodb.core.convert.MappingMongoConverter' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Here is my main class
#SpringBootApplication
#EnableMongoAuditing
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(PatientstoreApplication.class, args);
}
}
And here is my test class:
#WebMvcTest(TestController.class)
public class TestTest {
#Autowired MockMvc mockMvc;
#MockBean TestService testService;
#Test
public void test() {
}
}
A reproducible example can be found here: https://github.com/jota87r/testapp
My question is: is there any configuration missing? should I manually create a bean of type for MappingMongoConverter for my test cases?
You can mock the MappingMongoConverter.class.
So your test class should be as follows:
#WebMvcTest(TestController.class)
public class TestTest {
#Autowired
MockMvc mockMvc;
#MockBean
TestService testService;
//Mock MappingMongoConverter
#MockBean
private MappingMongoConverter mappingMongoConverter;
#Test
public void test() {
}
I encountered the same situation.
In your #WebMvcTest class, try to add a test configuration like:
#TestConfiguration
static class TestConfig {
#Bean
public MappingMongoConverter mongoConverter() {
return new MappingMongoConverter(mock(DbRefResolver.class), mock(MappingContext.class));
}
}

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
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}"
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

error creating bean with name employeeController. injection of autowired dependencies failed. could not autowire fields

Error From Console
SEVERE: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeController': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ram.service.EmployeeService com.ram.controller.EmployeeController.employeeService;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeService': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ram.dao.EmployeeDao com.ram.service.EmployeeServiceImpl.employeeDao;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeDao': Injection of autowired dependencies failed;
EmployeeController.java
#Controller
public class EmployeeController {
#Autowired
private EmployeeService employeeService;
#Qualifier(value="employeeService")
public void setEmployeeService(EmployeeService employeeService){
this.employeeService = employeeService;
}
#RequestMapping(value = "/employees", method = RequestMethod.GET)
public String listEmployee(Model model) {
model.addAttribute("employee", new Employee());
model.addAttribute("listEmployee", employeeService.listEmployee());
return "employee";
}
This line is most imporatant from the log. Unfortunately, the log is incomplete, so it's unclear which property of EmployeeDao is missing:
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeDao': Injection of autowired dependencies failed;
What fields are you mapping into EmployeeDao? Because one of those cannot be found, perhaps not marked with #Component?

NoSuchBeanDefinitionException: No qualifying bean of type for inner class

I try to add third-party bean to my application:
#Configuration
#ComponentScan(...)
public class ApplicationConfiguration {
#Bean(name = "mqSocket")
public ZMQ.Socket startServer() {
try (ZMQ.Context ctx = ZMQ.context(1);
ZMQ.Socket publisher = ctx.socket(ZMQ.PUB)) {
publisher.bind("tcp://*:5556");
return publisher;
}
}
}
and I try to autowire this like this:
#RestController
public class MyRestController {
#Autowired
private ZMQ.Socket mqSocket;
but it prints following:
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myRestController': Unsatisfied dependency expressed through field 'mqSocket'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.zeromq.ZMQ$Socket' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.zeromq.ZMQ$Socket' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
...
You should add the #Import annotation on your Application like class.
Eg.:
#Import(ApplicationConfiguration.class)
public class Application {
}
Note: cf. #M.Prokhorov comments, the ZMQ.Socket is closed by the try-with-resource statement
You created a bean with name mqSocket so I guess you have to use the #Qualifier annotation; try to change your code in this way:
#RestController
public class MyRestController {
#Autowired
#Qualifier("mqSocket")
private ZMQ.Socket mqSocket;
I hope it's usefull
Angelo

Categories