2012-09-04 54 views
2

我見過幾個類似的問題,但沒有一個建議的解決方案對我有幫助。簡介:當我在.xml中創建並注入bean時,它可以工作;但是當我使用@Autowire或@Resource時,它不會。添加@Autowire「當前線程找不到會話」

環境:Spring3,Hibernate4,Tomcat7。

詳細說明:以下設置不工作:

web.xml

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/spring/root-context.xml 
     /WEB-INF/spring/security-context.xml 
     /WEB-INF/spring/users-context.xml 
    </param-value> 
    </context-param> 

root-context.xml

<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/venus" /> 
    <property name="username" value="root" /> 
    <property name="password" value="" /> 
    </bean> 

    <bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="packagesToScan" value="com.airbus.genesis.marte.dal" /> 
    <property name="hibernateProperties"> 
     <props> 
     <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
     <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 
    </bean> 

    <tx:annotation-driven transaction-manager="txManager" /> 
    <bean id="txManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

users-context.xml

<bean id="usersDAO" class="com.airbus.genesis.marte.dal.users.UsersDAO"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

BL對象:

@Service("usersManager") 
@Transactional(readOnly = true) 
public class UsersManager implements IUsersManager { 
    @Autowired 
    @Qualifier("usersDAO") 
    private IUsersDAO usersDAO; 

    @Override 
    public List<User> getUsers() { 
    return usersDAO.getUsers(); 
    } 

} 

DAO對象(注意,@Repository和@Resource被註釋掉):

//@Repository("usersDAO") 
@Transactional(readOnly = true) 
public class UsersDAO implements IUsersDAO { 

    // @Resource(name = "sessionFactory") 
    private SessionFactory sessionFactory; 

    @Override 
    public List<User> getUsers() { 
    @SuppressWarnings("unchecked") 
    List<User> res = (List<User>) getSessionFactory().getCurrentSession() 
     .createQuery("from User").list(); 
    return res; 
    } 

    public SessionFactory getSessionFactory() { 
    return sessionFactory; 
    } 

    public void setSessionFactory(SessionFactory sessionFactory) { 
    this.sessionFactory = sessionFactory; 
    } 
} 

但下面一個不工作:

users-context.xml

<!-- 
    <bean id="usersDAO" class="com.airbus.genesis.marte.dal.users.UsersDAO"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
--> 

DAO對象(注意@Repository和@Resource不可用nted現在):

@Repository("usersDAO") 
@Transactional(readOnly = true) 
public class UsersDAO implements IUsersDAO { 

    @Resource(name = "sessionFactory") 
    private SessionFactory sessionFactory; 

    @Override 
    public List<User> getUsers() { 
    @SuppressWarnings("unchecked") 
    List<User> res = (List<User>) getSessionFactory().getCurrentSession() 
     .createQuery("from User").list(); 
    return res; 
    } 

    public SessionFactory getSessionFactory() { 
    return sessionFactory; 
    } 

    public void setSessionFactory(SessionFactory sessionFactory) { 
    this.sessionFactory = sessionFactory; 
    } 
} 

org.hibernate.HibernateException: No Session found for current thread升高:如果我使用@Autowire代替@Resource

org.hibernate.HibernateException: No Session found for current thread 
    org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) 
    org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:941) 
    com.airbus.genesis.marte.dal.users.UsersDAO.getUsers(UsersDAO.java:23) 
    com.airbus.genesis.marte.bl.users.UsersManager.getUsers(UsersManager.java:22) 
[...] 

同樣的情況。

我想這是我的一些誤解,但找不到在哪裏。任何想法?

回答

2

的問題很可能是@Repository@Service註釋被拾起的調度-servlet.xml中的配置(你使用context:component-scan?),所以這些豆子在調度servlet上下文,而不是根Web應用程序上下文中創建。

一個好的做法是將服務層對象放到專用包中,並使用特定的包名稱作爲<context:component-scan />base-package限定符(如'com.myproject.services')。您也可以使用過濾器表達式包含和排除的元素在這裏看到的例子:@Service are constructed twiceSpring documentation

見4.10.3節還Difference between applicationContext.xml and spring-servlet.xml in Spring Framework

+0

這是解決方案。 CopyPasteException在我身邊。 –

相關問題