Announcement Announcement Module
Collapse
No announcement yet.
@Autowired problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Autowired problem

    Was wondering if someone can help me with an 'Injection of autowired dependencies failed':

    INFO: Initializing Spring FrameworkServlet 'spring'

    In ViewDVDController()

    Oct 26, 2011 11:50:07 AM org.apache.catalina.core.ApplicationContext log
    SEVERE: StandardWrapper.Throwable
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'viewDVDController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationEAttachment xception: Could not autowire field: private springmvc.application.inventory.impl.InventoryMana gerImpl springmvc.presentation.ViewDVDController.manager; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [springmvc.application.inventory.impl.InventoryMana gerImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Aut owired(required=true)}

    InventoryManagerImpl class:
    Code:
    package springmvc.application.inventory.impl;
    
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.RowCallbackHandler;
    import org.springframework.jdbc.core.support.JdbcDaoSupport;
    import springmvc.application.inventory.DVDDetails;
    import springmvc.application.inventory.DVDInfo;
    import springmvc.application.inventory.InvalidDvdIdException;
    import springmvc.application.inventory.InventoryManager;
    
    public class InventoryManagerImpl extends JdbcDaoSupport implements InventoryManager {
    
    	@Override
    	public void addDvd(DVDDetails details) throws InvalidDvdIdException {
    		// TODO Auto-generated method stub
    	}
    	@Override
    	public DVDDetails getDetails(String dvdID) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	@Override
    	public List<DVDInfo> getAll() {
    		String sql = "SELECT DVD_TITLE_ID, TITLE FROM dvdtitle";
    		final List<DVDInfo> result = new ArrayList<DVDInfo>();
    		//obtain JdbcTemplate
    		JdbcTemplate template = getJdbcTemplate();
    		
    		//Execute sql statemtn using RowCallbackHandler
    		template.query(sql, new RowCallbackHandler() { public void processRow(ResultSet rs) throws SQLException { DVDInfo info = populate(rs);  result.add(info); } }  );
    		return Collections.unmodifiableList(result);
    	}
    
    	private DVDInfo populate(ResultSet rs) throws SQLException {
    		DVDInfo info = new DVDInfo();
    		info.setID(rs.getString("DVD_TITLE_ID"));
    		info.setTitle(rs.getString("TITLE"));
    		
    		return info;
    	}
    }
    ViewDVDController:

    Code:
    package springmvc.presentation;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    //import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
    //import springmvc.application.inventory.DVDDetails;
    import springmvc.application.inventory.DVDInfo;
    import springmvc.application.inventory.InventoryManager;
    import springmvc.application.inventory.impl.InventoryManagerImpl;
    //import javax.servlet.http.HttpServletRequest;
    //import javax.servlet.http.HttpServletResponse;
    import java.util.List;
    
    @Controller
    public class ViewDVDController extends MultiActionController {
      public ViewDVDController() {
    	  System.out.println("In ViewDVDController()");  
      }
      
     @Autowired                    // ?
      private InventoryManager manager;
      
      public InventoryManager getManager() {
    	  return manager;
      }
      
      public void setManager(InventoryManager manager) {
    	  this.manager = manager;
      }
      
     @RequestMapping (value="viewAll.view", method=RequestMethod.GET)
     public ModelAndView viewAll() throws Exception {
    	 System.out.println("In ViewDVDController.viewAll()");
    	 List<DVDInfo> all = manager.getAll();
    	 return new ModelAndView("ListAll", "catalog", all);
     }
    }
    web.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xmlns="http://java.sun.com/xml/ns/javaee" 
             xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      
      <display-name>SpringMVCExample</display-name>
      <!-- todo Declare the DispatcherServlet.  Beware of the name! Another file will tell you
      what name to use! -->
        
      <servlet>
         <servlet-name>spring</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      </servlet>
      
      <servlet-mapping>
         <servlet-name>spring</servlet-name>
         <url-pattern>/inventory/*</url-pattern>
      </servlet-mapping>
      
      
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list>
    </web-app>
    spring.properties:

    Code:
    app.jdbc.driverClassName=org.apache.derby.jdbc.ClientDriver
    app.jdbc.url=jdbc:derby://localhost:1527/springclass
    app.jdbc.username=sa
    app.jdbc.password=password
    spring-servlet:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.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">
       
       <mvc:annotation-driven/>     
       
       <context:component-scan base-package="springmvc" />
      
    
     <bean id="viewResolver"  class=
                     "org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix"  value="/views/inventory/" />
          <property name="suffix"  value=".jsp" />               
                     
     </bean>
    
    </beans>

    jdbc-context.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:tx="http://www.springframework.org/schema/tx"
         
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans.xsd
    	http://www.springframework.org/schema/tx 
    		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    <context:property-placeholder location="/WEB-INF/spring.properties" />
         
        <!-- Enable annotation style of managing transactions -->
     <tx:annotation-driven transaction-manager="transactionManager" />
        
    
     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
    
          <property name="driverClass" value="${app.jdbc_driverClass}"/>
          <property name="jdbcUrl" value="${app.jdbc_url}"/>
          <property name="user" value="${app.jdbc_username}"/>
          <property name="password" value="${app.jdbc_password}"/>
          <property name="acquireIncrement" value="5"/>
          <property name="idleConnectionTestPeriod" value="60"/>
          <property name="maxPoolSize" value="100"/>
          <property name="maxStatements" value="50"/>
          <property name="minPoolSize" value="10"/>
    </bean>
     
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
              <property name="dataSource-ref" ref="dataSource" />
    </bean>
    </beans>
    applicationContext.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <import resource="jdbc-context.xml" />
    Attachment
    Attached Files
    Last edited by kezkez; Oct 26th, 2011, 01:05 PM.

  • #2
    Have you tried annotating InventoryManagerImpl as a @Repository?

    Comment


    • #3
      Put @Repository at InventoryManagerImpl but now getting jdbcTemplate problem:

      Error creating bean with name 'InventoryManager' defined in file [C:\Program Files\springsource\vfabric-tc-server-developer-2.6.0.RELEASE\spring-insight-instance\wtpwebapps\SpringMVCExample2\WEB-INF\classes\springmvc\application\inventory\impl\I nventoryManagerImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required


      Code:
      @Repository("InventoryManager")
      public class InventoryManagerImpl extends JdbcDaoSupport implements InventoryManager {
      
          @Override
          public void addDvd(DVDDetails details) throws InvalidDvdIdException {
              // TODO Auto-generated method stub
      
          }
      
          @Override
          public DVDDetails getDetails(String dvdID) {
              // TODO Auto-generated method stub
              return null;
          }
      
          @Override
          public List<DVDInfo> getAll() {
              String sql = "SELECT DVD_TITLE_ID, TITLE FROM dvdtitle";
              final List<DVDInfo> result = new ArrayList<DVDInfo>();
              //obtain JdbcTemplate
              JdbcTemplate template = getJdbcTemplate();
              
              //Execute sql statemtn using RowCallbackHandler
              template.query(sql, new RowCallbackHandler() { public void processRow(ResultSet rs) throws SQLException { DVDInfo info = populate(rs);  result.add(info); } }  );
              return Collections.unmodifiableList(result);
          }
      
          private DVDInfo populate(ResultSet rs) throws SQLException {
              DVDInfo info = new DVDInfo();
              info.setID(rs.getString("DVD_TITLE_ID"));
              info.setTitle(rs.getString("TITLE"));
              
              return info;
          }
      }

      Comment

      Working...
      X