Announcement Announcement Module
Collapse
No announcement yet.
Spring 3 annotation DI issue. NullpointerException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans default-lazy-init="false"
    	xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:util="http://www.springframework.org/schema/util" xmlns:spring="http://www.springframework.org/schema/beans"
    	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd 
    	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	<context:annotation-config />
    	<context:component-scan base-package="com.foo.web.mvc">
    		<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
    	</context:component-scan>
    
    	<context:component-scan base-package="com.foo.dao">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
        </context:component-scan>
    
    	<context:component-scan base-package="com.foo.service">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        </context:component-scan>
    
    	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    		<property name="alwaysUseFullPath" value="false" />
    	</bean>
    
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" />
    
    	<bean id="contentNegotiatingViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    		<property name="mediaTypes">
    			<map>
    				<entry key="html" value="text/html" />
    			</map>
    		</property>
    		<property name="viewResolvers">
    			<list>
    				<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" />
    				</bean>
    			</list>
    		</property>
    	</bean>
    
    </beans>
    My Dao Class :-

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

  • #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.

    Comment


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

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

      Comment


      • #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...

        Comment

        Working...
        X