Announcement Announcement Module
Collapse
No announcement yet.
Autowiring Exception in service layer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowiring Exception in service layer

    can anybody tell me??????????why this exception is occuring??????


    org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Allocate exception for servlet spring
    org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [com.service.TeamService] 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)}
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.raiseNoSuchBeanDefinitionExcept ion(DefaultListableBeanFactory.java:952)

    code for controller is:

    @Controller
    public class LinkController {
    @Autowired
    private TeamService teamService;


    @RequestMapping(value="/team")
    public ModelAndView addingTeam(@ModelAttribute Team team) {
    ModelAndView modelAndView = new ModelAndView("home");
    teamService.save(team);
    String message = "Team was successfully added.";
    modelAndView.addObject("message", message);
    return modelAndView;
    }
    @RequestMapping(value = "/teamList", method = RequestMethod.GET)
    public String list(Team team, Model model) {
    List<Team> hotels = teamService.getTeamList(team);
    model.addAttribute(hotels);
    return "pages/listTeam";
    }


    }
    Service layer code is:

    package com.serviceimpl;

    import java.util.List;

    import com.dao.TeamDao;
    import com.model.Team;
    import com.service.TeamService;

    public class TeamServiceImpl implements TeamService {

    public void setTeamDao(TeamDao teamDao) {
    this.teamDao = teamDao;
    }

    private TeamDao teamDao;

    @Override
    public void save(Team team) {
    System.out.print("Service Save Call");
    teamDao.save(team);
    System.out.print("Service End Call");

    }

    @Override
    public List getTeamList(Team team) {
    // TODO Auto-generated method stub
    System.out.print("Service getTeamList Call");
    List<Team> teams=teamDao.getTeamList(team);
    System.out.print("Service getTeamList Call");
    return teams;
    }

    }


    configuration File :


    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.springframework.org/schema/p"
    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/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <context:component-scan base-package="com.model" />
    <!--Start Database connection -->
    <!-- Deploys a in-memory "booking" datasource populated -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
    <property name="url" value="jdbc:jtds:sqlserver://127.0.0.1:1433/booking" />
    <property name="username" value="sa" />
    <property name="password" value="" />
    </bean>

    <!-- End Database Connection -->

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="annotatedClasses">
    <value>com.model.Team</value>
    </property>

    </bean>

    <bean id="teamDao" class="com.daoimpl.TeamDaoImpl">
    <property name="sessionFactory" ref="sessionFactory"></property> </bean>



    <!--Start View Resolver -->
    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.Intern alResourceViewResolver"
    prefix="/WEB-INF/pages/" p:suffix=".jsp" />
    <!-- End View Resolver -->


    </beans>

    please tell,what is the exact prob?
    Last edited by navi; May 30th, 2013, 05:47 AM.

  • #2
    Please use [ code]][/code ] tags when posting.

    please tell,what is the exact prob?
    The stacktrace allready tells you that... There is no instance to be found of a TeamService ...

    The class implementing this interface isn't instantiated in your xml nor is it detected by component scanning so no instance will ever be created.

    Comment


    • #3
      Actually i am new in Spring,and trying to learn.
      i have post me code,please check and revert ?what is exact prob here??????
      Model class code:
      [code]
      package com.model;

      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import javax.persistence.Table;

      @Entity
      @Table(name = "Team")
      public class Team {
      @Id
      @GeneratedValue
      private Integer id;
      private String name;
      private Integer rating;
      public Integer getId() {
      return id;
      }
      public void setId(Integer id) {
      this.id = id;
      }
      public String getName() {
      return name;
      }
      public void setName(String name) {
      this.name = name;
      }
      public Integer getRating() {
      return rating;
      }
      public void setRating(Integer rating) {
      this.rating = rating;
      }

      }
      [/code ]
      Dao class
      Code:
      package com.dao;
      
      import java.util.List;
      
      import com.model.Team;
      
      public interface TeamDao {
      public void save(Team team);
      public List<Team> getTeamList(Team team);
      }
      DaoImpl class Code:
      Code:
      package com.daoimpl;
      
      import java.util.List;
      
      import org.hibernate.Session;
      import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
      
      import com.dao.TeamDao;
      import com.model.Team;
      
      public class TeamDaoImpl extends HibernateDaoSupport implements TeamDao {
      
      	@Override
      	public void save(Team team) {
      		// TODO Auto-generated method stub
      		System.out.print("Start Team Save method");
      		Session session = getSession();
      		session.save(team);
      		System.out.print("End Team Save method");
      	}
      
      	@Override
      	public List<Team> getTeamList(Team team) {
      		System.out.print("Start Team getTeamList method");
      		Session session = getSession();
      		List<Team> teams= session.createCriteria(Team.class).list();
      		System.out.print("End Team getTeamList method");
      		return teams;
      	}
      
      }
      Service class code:

      Code:
      package com.service;
      
      import java.util.List;
      
      import com.model.Team;
      
      public interface TeamService {
      	public void save(Team team);
      	public List<Team> getTeamList(Team team);
      }
      ServiceImpl Code:

      Code:
      package com.serviceimpl;
      
      import java.util.List;
      
      import com.dao.TeamDao;
      import com.model.Team;
      import com.service.TeamService;
      
      public class TeamServiceImpl implements TeamService {
      
      	public void setTeamDao(TeamDao teamDao) {
      		this.teamDao = teamDao;
      	}
      
      	private TeamDao teamDao;
      	
      	@Override
      	public void save(Team team) {
      		System.out.print("Service Save Call");
      		teamDao.save(team);
      		System.out.print("Service End Call");
      		
      	}
      
      	@Override
      	public List<Team> getTeamList(Team team) {
      		// TODO Auto-generated method stub
      		System.out.print("Service getTeamList Call");
      		List<Team> teams=teamDao.getTeamList(team);
      		System.out.print("Service getTeamList Call");
      		return teams;
      	}
      
      }
      Controller code:
      Code:
      package com.model;
      
      import java.util.List;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Controller;
      import org.springframework.stereotype.Service;
      import org.springframework.ui.Model;
      import org.springframework.web.bind.annotation.ModelAttribute;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      import org.springframework.web.servlet.ModelAndView;
      
      import com.service.TeamService;
      
      @Controller
      public class LinkController {
      
      	private TeamService teamService;
      
       
      	@RequestMapping(value = "/team", method = RequestMethod.GET)
      	public void addingTeam(Team team, Model model) {
      		teamService.save(team);
      		model.addAttribute("team", team);
      	}
      
      	@RequestMapping(value = "/teamList", method = RequestMethod.GET)
      	public String list(Team team, Model model) {
      		List<Team> hotels = teamService.getTeamList(team);
      		model.addAttribute(hotels);
      		return "/listTeam";
      	}
      
      }
      configuration File:
      Code:
      <beans xmlns="http://www.springframework.org/schema/beans"
      	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
      	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      	 xmlns:tx="http://www.springframework.org/schema/tx"
      	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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
      
      	<!-- Instructs Spring to perfrom declarative transaction management on annotated classes -->
      	<tx:annotation-driven />
      	<context:component-scan base-package="com.model" />
      	<!--Start Database connection  -->
      	<!-- Deploys a in-memory "booking" datasource populated -->
      	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      		<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
      		<property name="url" value="jdbc:jtds:sqlserver://127.0.0.1/booking" />
      		<property name="username" value="sa" />
      		<property name="password" value="" />
      	</bean>
      
      	<!-- End Database Connection -->	
      	
      	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      	<property name="dataSource" ref="dataSource"></property>
      	<property name="annotatedClasses">
      	<value>com.model.Team</value>
      	</property>
       
      	</bean>
      	
      	<bean id="teamDao" class="com.daoimpl.TeamDaoImpl">
      	<property name="sessionFactory" ref="sessionFactory"></property>	</bean>
      	
      	<bean id="teamService" class="com.serviceimpl.TeamServiceImpl">
      	<property name="teamDao" ref="teamDao"></property>
      	</bean>
      	
      	<!--Start View Resolver -->
      	<bean id="viewResolver"
      		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      		p:prefix="/WEB-INF/pages/" p:suffix=".jsp" />
      	<!-- End View Resolver -->
      
      
      </beans>
      jsp pages:
      Code:
      <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
          pageEncoding="ISO-8859-1"%>
      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Insert title here</title>
      </head>
      <body>
       <form:form action="listTeam" commandName="message">
         <table align="center" >
      		<tr>
      			<td>name :</td>
      			<td>
      			 
      			<form:input path="name" />
      			</td>
      		</tr>
      		<tr>
      			<td>rating :</td>
      			<td>
      		 
      			<form:input path="rating" /></td>
      		</tr>		
      		<tr>
      			<td></td>
      			<td><input type="submit" value="Submit" /></td>
      		</tr>
      	</table>
        </form:form> 
      </body>
      </html>
      Wat is missing here?Exception is :

      EVERE: Servlet.service() for servlet [spring] in context with path [/Test_CRUD] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
      java.lang.NullPointerException
      at com.model.LinkController.addingTeam(LinkController .java:25)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.springframework.web.bind.annotation.support.Ha ndlerMethodInvoker.invokeHandlerMethod(HandlerMeth odInvoker.java:176)
      at org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.invokeHandlerMethod(An notationMethodHandlerAdapter.java:436)
      at org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.handle(AnnotationMetho dHandlerAdapter.java:424)
      at org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:923)
      at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:852)
      at org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:882)
      at org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:778)
      at javax.servlet.http.HttpServlet.service(HttpServlet .java:621)
      at javax.servlet.http.HttpServlet.service(HttpServlet .java:728)
      at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:123)
      at org.apache.catalina.authenticator.AuthenticatorBas e.invoke(AuthenticatorBase.java:472)
      at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:99)
      at org.apache.catalina.valves.AccessLogValve.invoke(A ccessLogValve.java:936)
      at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:407)
      at org.apache.coyote.http11.AbstractHttp11Processor.p rocess(AbstractHttp11Processor.java:1004)
      at org.apache.coyote.AbstractProtocol$AbstractConnect ionHandler.process(AbstractProtocol.java:589)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProce ssor.run(JIoEndpoint.java:310)
      at java.util.concurrent.ThreadPoolExecutor.runWorker( Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run (Unknown Source)
      at java.lang.Thread.run(Unknown Source)
      Last edited by navi; May 31st, 2013, 03:56 AM.

      Comment


      • #4
        The problem is that you should first get an understanding of what spring does and how autowiring works.

        In short you removed the @Autowired annotation from the field in the controller how should spring know how to put the TeamService in there?!

        Comment


        • #5
          Thanks for reply ,i had remove autowired Annotation.its work fine for me.Any reference for understanding Spring freamework???????

          Comment


          • #6
            Hello

            Spring Framework Reference Documentation

            Comment

            Working...
            X