Announcement Announcement Module
No announcement yet.
Spring 3 annotation DI issue. NullpointerException Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3 annotation DI issue. NullpointerException

    I am trying to learn spring 3 and working on an application which uses annotation based configuration instead of defining beans in *.xml. I keep getting this error whicxh i am not able to resolve. When used just with 2.5 version by defining beans in applicationContext*.xml works fine but when using annotation it does not work

    org.hibernate.SessionException: Session is closed

    my *-servlet.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans default-lazy-init="false"
    	xmlns="" xmlns:xsi=""
    	xmlns:util="" xmlns:spring=""
    	xmlns:p="" xmlns:context=""
    	<context:annotation-config />
    	<context:component-scan base-package="">
    		<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
    	<context:component-scan base-package="">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    	<context:component-scan base-package="">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    		<property name="alwaysUseFullPath" value="false" />
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" />
    	<bean id="contentNegotiatingViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    		<property name="mediaTypes">
    				<entry key="html" value="text/html" />
    		<property name="viewResolvers">
    				<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
    				<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    					<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    					<property name="prefix" value="/WEB-INF/jsp/" />
    					<property name="suffix" value=".jsp" />
    My Dao Class :-

    public class UserDao {
    	private SessionFactory sessionFactory;
    	public List<User> findUsers(String id) {
    		Session session = sessionFactory.getCurrentSession();
    my applicationContext.xml
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    <bean id="sessionFactory"
    		<property name="dataSource">
    			<ref bean="dataSource" />
    	<bean id="transactionManager"
    		<property name="sessionFactory" ref="sessionFactory" />
    	 <tx:annotation-driven transaction-manager="transactionManager"/>
            <aop:pointcut id="daoOperation" expression="execution(* *..dao.*(..))"/>
            <aop:advisor id="managerTx" advice-ref="txAdvice" pointcut-ref="daoOperation" order="1"/>
          <tx:advice id="txAdvice" transaction-manager="transactionManager">
                <tx:method name="get*" read-only="true"/>
                <tx:method name="find*" read-only="true"/>
                <tx:method name="*"/>

  • #2
    1) You don't need context:annotation-config it is implied by component-scan
    2) Use a single component-scan instead of multiple (also all component-scan elements scan for everything not only what you include, this is because you did not disable the default-filters).
    3) You have duplicated the same view resolver why?
    4) Post the full stack trace, judging from what you post you try to access an object without an open session or transaction.
    5) Transactions should be defined on your service layer NOT the dao/repository layer.


    • #3
      Wow i am stumped. When using this code :-

      Session session = null;
      try {
      session = sessionFactory.getCurrentSession();
      finally {
      if(session != null) {
      was an issue. Once removing the finally block everything works like a charm.
      Last edited by poly; Sep 13th, 2011, 03:40 PM.


      • #4
        Well not really strange as you want spring to manage the session for you (that is why you use spring managed transactions). Which is something you left out in your initial post.

        However the pointers I gave you are still valid and will cleanup your code/configuration...