Note that there are some explanatory texts on larger screens.

plurals
  1. PO@Autowired - No qualifying bean of type found for dependency
    text
    copied!<p>I've started my project by creating entities, services and JUnit tests for services using Spring and Hibernate. All of this works great. Then I've added spring-mvc to make this web application using many different step-by-step tutorials, but when I'm trying to make Controller with @Autowired annotation, I'm getting errors from Glassfish during deployment. I guess that for some reason Spring doesn't see my services, but after many attempts I still can't handle it.</p> <p>Tests for services with </p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:/beans.xml"}) </code></pre> <p>and</p> <pre><code>@Autowired MailManager mailManager; </code></pre> <p>works properly.</p> <p>Controllers without @Autowired too, I can open my project in web browser without trouble.</p> <p>/src/main/resources/beans.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"&gt; &lt;context:property-placeholder location="jdbc.properties" /&gt; &lt;context:component-scan base-package="pl.com.radzikowski.webmail"&gt; &lt;context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /&gt; &lt;/context:component-scan&gt; &lt;!--&lt;context:component-scan base-package="pl.com.radzikowski.webmail.service" /&gt;--&gt; &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt; &lt;property name="driverClassName" value="${jdbc.driverClassName}" /&gt; &lt;property name="url" value="${jdbc.url}" /&gt; &lt;property name="username" value="${jdbc.username}" /&gt; &lt;property name="password" value="${jdbc.password}" /&gt; &lt;/bean&gt; &lt;!-- Persistance Unit Manager for persistance options managing --&gt; &lt;bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"&gt; &lt;property name="defaultDataSource" ref="dataSource"/&gt; &lt;/bean&gt; &lt;!-- Entity Manager Factory for creating/updating DB schema based on persistence files and entity classes --&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="persistenceUnitManager" ref="persistenceUnitManager"/&gt; &lt;property name="persistenceUnitName" value="WebMailPU"/&gt; &lt;/bean&gt; &lt;!-- Hibernate Session Factory --&gt; &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource"/&gt; &lt;!--&lt;property name="schemaUpdate" value="true" /&gt;--&gt; &lt;property name="packagesToScan" value="pl.com.radzikowski.webmail.domain" /&gt; &lt;property name="hibernateProperties"&gt; &lt;props&gt; &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; &lt;/bean&gt; &lt;!-- Hibernate Transaction Manager --&gt; &lt;bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt; &lt;property name="sessionFactory" ref="sessionFactory"/&gt; &lt;/bean&gt; &lt;!-- Activates annotation based transaction management --&gt; &lt;tx:annotation-driven transaction-manager="txManager"/&gt; &lt;/beans&gt; </code></pre> <p>/webapp/WEB-INF/web.xml</p> <pre><code>&lt;web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="WebApp_ID" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt; &lt;display-name&gt;Spring Web MVC Application&lt;/display-name&gt; &lt;servlet&gt; &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt; &lt;url-pattern&gt;/&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/mvc-dispatcher-servlet.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;/web-app&gt; </code></pre> <p>/webapp/WEB-INF/mvc-dispatcher-servlet.xml</p> <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"&gt; &lt;context:component-scan base-package="pl.com.radzikowski.webmail" use-default-filters="false"&gt; &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /&gt; &lt;/context:component-scan&gt; &lt;mvc:annotation-driven/&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/" /&gt; &lt;property name="suffix" value=".jsp" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>pl.com.radzikowski.webmail.service.AbstractManager</p> <pre><code>package pl.com.radzikowski.webmail.service; import org.apache.log4j.Logger; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; /** * Master Manager class providing basic fields for services. * @author Maciej Radzikowski &lt;maciej@radzikowski.com.pl&gt; */ public class AbstractManager { @Autowired protected SessionFactory sessionFactory; protected final Logger logger = Logger.getLogger(this.getClass()); } </code></pre> <p>pl.com.radzikowski.webmail.service.MailManager</p> <pre><code>package pl.com.radzikowski.webmail.service; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; @Component @Transactional public class MailManager extends AbstractManager { // some methods... } </code></pre> <p>pl.com.radzikowski.webmail.HomeController</p> <pre><code>package pl.com.radzikowski.webmail.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import pl.com.radzikowski.webmail.service.MailManager; @Controller @RequestMapping("/") public class HomeController { @Autowired public MailManager mailManager; @RequestMapping(value = "/", method = RequestMethod.GET) public String homepage(ModelMap model) { return "homepage"; } } </code></pre> <p>Error:</p> <pre><code>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)} </code></pre> <p>Sorry for a lot of code, but I don't know what can cause that error anymore.</p> <p><strong>Added</strong></p> <p>I've created the interface:</p> <pre><code>@Component public interface IMailManager { </code></pre> <p>added implements:</p> <pre><code>@Component @Transactional public class MailManager extends AbstractManager implements IMailManager { </code></pre> <p>and changed autowired:</p> <pre><code>@Autowired public IMailManager mailManager; </code></pre> <p>But it still throws errors (also when I've tried with @Qualifier)</p> <blockquote> <p>..Could not autowire field: public pl.com.radzikowski.webmail.service.IMailManager pl.com.radzikowski.webmail.controller.HomeController.mailManager...</p> </blockquote> <p>I've tried with different combinations of @Component and @Transactional too.</p> <p>Shouldn't I include beans.xml in web.xml somehow?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload