Spring AutoWired和SessionFactory

时间:2011-10-04 22:26:41

标签: hibernate spring sessionfactory

这是我的例外

Exception in thread "main" java.lang.IllegalArgumentException: Property 'sessionFactory'       is required
  at   org.springframework.orm.hibernate3.HibernateAccessor.afterPropertiesSet(HibernateAccessor.java:314)
at org.springframework.orm.hibernate3.HibernateTemplate.<init>(HibernateTemplate.java:139)
at com.cetin.services.Imp.MyServiceImp.<init>(MyServiceImp.java:29)
at com.cetin.services.Imp.Program.main(Program.java:10)

这是我的ApplicationContext文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:faces="http://www.springframework.org/schema/faces"
xmlns:int-security="http://www.springframework.org/schema/integration/security"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/integration/security http://www.springframework.org/schema/integration/security/spring-integration-security-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
    http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/faces http://www.springframework.org/schema/faces/spring-faces-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config />
<context:component-scan base-package="com.xxx" />

<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
        value="com.mysql.jdbc.Driver">
    </property>
    <property name="url"
        value="jdbc:mysql://localhost:3306/nodeser">
    </property>
    <property name="username" value="root"></property>
    <property name="password" value="002210"></property>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.MySQLDialect
            </prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>



<!-- Transaction management -->
 <tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>  

我的班级

package com.cetin.services.Imp;



import com.cetin.services.IMyService;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

@Service("myService")
public class MyServiceImp
implements IMyService{

@Override
public String Hello(String msg) {
    return "Hello "+msg;
}

@Autowired(required=true)
private SessionFactory sessionFactory;

private HibernateTemplate hibernateTemplate;

public MyServiceImp()
{   

    hibernateTemplate = new HibernateTemplate(sessionFactory);
}

public int getSize()
{
return hibernateTemplate.find("from Category").size();
}


 }

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

您的会话工厂未注入且为空。

可能是因为com.cetin.services.Imp.MyServiceImp超出了Spring Application Context。

为此类创建<bean id="..." ....>启动,并使用Setter注入注入SessionFactory。

否则将其标记为@Repository并将其显示为component-scan