• Register
0 votes
217 views

Problem :

I have created entities, services and JUnit tests for services using Spring and Hibernate for my project. All of these are working. After that I added spring-mvc to it to make this web application but while trying to make Controller with @Autowired annotation, I am getting errors from Glassfish during deployment as follows.

Error:

SEVERE:   Exception while loading the app

SEVERE:   Undeployment failed for context /WebMail

SEVERE:   Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public pl.com.radzikowski.webmail.service.MailManager pl.com.radzikowski.webmail.controller.HomeController.mailManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.com.radzikowski.webmail.service.MailManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

I have also created the interface as follows:

@Component

public interface IMailManager {

added implements:

@Component

@Transactional

public class MailManager extends AbstractManager implements IMailManager {

and changed autowired:

@Autowired

public IMailManager mailManager;

But I am still getting errors as follows

..Could not autowire field: public pl.com.radzikowski.webmail.service.IMailManager pl.com.radzikowski.webmail.controller.HomeController.mailManager...

6 5 3
6,930 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

After doing lot of research I found the correct way to autowire AbstractManager as follows:

@Autowired
@Qualifier(value="mailService")
public MailManager mailManager;

and

@Component("mailService")
@Transactional
public class MailManager extends AbstractManager {
}
9 7 4
38,600 points
0 votes

This is a common exception thrown by the BeanFactory when the user tries to resolve a bean that simply is not defined in the spring context.

Here we will discuss some possible reasons;

Found for dependency:

The most common reason for this exception is simply trying to inject a bean that is not defined.

Example:

For example, BeanB is wiring in a collaborator BeanA;

@Component

public class BeanA {

   @Autowired

    private BeanB dependency;

    //...

}

Now if the dependency BeanB is not defined in the Spring Context, then the bootstrap will fail with the no such bean definition.

Is defined:

Another reason for this exception is the existence of the two beans in the context, instead of one.

Example:

If an interface, IBeanB is implemented by two beans BeanB1 and BeanB2.

@Component

public class BeanB1 implements IBeanB {

    //

}

@Component

public class BeanB2 implements IBeanB {

    //

}

If the Bean A auto wires this interface, Spring will not know which one of the two implementations to inject;

@Component

public class BeanA {

    @Autowired

    private IBeanB dependency;

    ...

}

Again will result of same error;

Solution:

The solution of this problem is to use the Qualifier annotation to specify exactly the name of the bean you want;

@Component

public class BeanA {

    @Autowired

    @Qualifier("beanB2")

    private IBeanB dependency;

    ...

}

 

11 5 2
3,890 points

Related questions

0 votes
1 answer 48 views
48 views
Problem : For many days I am trying to create Spring CRUD application but getting following error org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean with name
asked Nov 13, 2019 peterlaw 6.9k points
0 votes
1 answer 51 views
51 views
Problem : I am very new to Spring 3's annotation and I am getting the exception. Below is my form jsp code: <form:form method="POST" action="login.htm" modelAttribute="login"> .... <form:input path="email" size="30" ... .doWork(AbstractThreadPool.java:532) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:680)
asked Nov 25, 2019 alecxe 7.5k points
0 votes
1 answer 4.9K views
4.9K views
Problem : I am developing the application in Spring, using a Tomcat, Mysql5, Java8... My problem is that I cannot deploy it, due to the "required bean 'entityManagerFactory' not found" problem. I have developed my project with the coworkers but ... a bean named 'entityManagerFactory' that could not be found. Action: Consider defining a bean named 'entityManagerFactory' in your configuration.
asked Jan 25, 2020 jwilliam 3.9k points
1 vote
1 answer 2.7K views
2.7K views
Problem : When I try to compile my spring project, I always face the below error : Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed I have the ... >1.4.1.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> . . .
asked Dec 12, 2019 alecxe 7.5k points
0 votes
2 answers 567 views
567 views
Problem : I am new to the Spring and JPA, wasted 10 days and no result with the searching on internet. I want to save the object to SQL SERVER, my connection is correct but when I write .flush() I get below exception &ldquo;nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress&rdquo;
asked Jan 21, 2020 jwilliam 3.9k points
0 votes
1 answer 299 views
299 views
Problem : I am new to Spring and JPA I want to save object to SQL SERVER, connection is correct but when I write .flush() I get the exception as below javax.persistence.TransactionRequiredException: no transaction is in progress
asked Oct 22, 2019 peterlaw 6.9k points
0 votes
1 answer 2.2K views
2.2K views
Problem : I am trying to use the curl command as follows: curl -u 591bf65f50057469f10b5fd9:0cf17f9b03d056ds0e11e48497e506a2 https://backend.tdk.com/api/devicetypes/59147fd79e93s12e61499ffe/messages But I am getting a JSON response as follows: {"data":[{"device":"18SE62"," ... T>, Object...) in the type RestTemplate is not applicable for the arguments (String, HttpEntity<String>,Class<MessageList>)
asked Dec 10, 2019 alecxe 7.5k points
0 votes
1 answer 471 views
471 views
Problem : I am very new to Spring Boot. I am learning to write code in Spring Boot. I have written my first Spring Boot code as below @RequestMapping(value = "/bar/foo", method = RequestMethod.GET) public MyResponseEntity<foo> foo() { Foo model; ... return ... .core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.3</version> </dependency> How can I fix above Spring Boot exception?
asked Feb 29, 2020 mphil 2.3k points
0 votes
1 answer 4.4K views
4.4K views
Problem : After executing spring boot with h2 database and JPA i am facing below error org.hibernate.tool.schema.spi.commandacceptanceexception: error executing ddl via jdbc statement
asked Nov 12, 2019 peterlaw 6.9k points
0 votes
1 answer 26 views
26 views
Problem: I have already seen many questions asked around the error which I am facing. But available solutions are not working for me. It may be the case that I am yet to thoroughly understand a auto wiring concept. I am able to navigate to the required page, ... least 1 bean which qualifies as autowire candidate. Now I am completely confused. Hope someone can try to help me out with this error.
asked Aug 8, 2020 Raphael Pacheco 4.9k points