• Register
0 votes
393 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.9k 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 {
}
38.6k 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;

    ...

}

 

3.9k points

Related questions

1 vote
1 answer 25 views
25 views
Problem: Need a solution : Nosuchbeandefinitionexception: no qualifying bean of type.
asked Apr 29 PkGuy 25.9k points
1 vote
1 answer 82 views
82 views
Problem : What does this mean > @Autowired - No qualifying bean of type found for dependency
asked Apr 23 Abik Dey 4k points
0 votes
1 answer 151 views
151 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 6 views
6 views
Problem: Need an answer for this : Org springframework beans factory beancreationexception error creating bean
asked Jun 17 Chi Omega 168k points
0 votes
1 answer 16 views
16 views
Answer: Having a tough time with this, help please : Org.springframework.beans.factory.beancreationexception: error creating bean with name
asked Jun 13 Chi Omega 168k points
0 votes
1 answer 37 views
37 views
Problem: neither bindingresult nor plain target object for bean name.
asked Feb 17 charles mathews 5.5k points
0 votes
1 answer 106 views
106 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 51 views
51 views
Problem: Please assist if anyone understands the problem: Dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}
asked Jun 10 asha 121k points
0 votes
1 answer 43 views
43 views
Problem: when opening the page that uses both beans, i get the exception: at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) please advise how to fix this exception.
asked Apr 18 ummesalma 29.1k points
0 votes
1 answer 22 views
22 views
Problem: Can anyone tell me the solution : Org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean with name?
asked Jun 11 Aurelia Maja 78.4k points