【问题标题】:Spring AutoWired and SessionFactorySpring AutoWired 和 SessionFactory
【发布时间】:2011-10-04 22:26:41
【问题描述】:

这是我的例外

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();
}


 }

我该如何解决这个问题?

【问题讨论】:

    标签: hibernate spring sessionfactory


    【解决方案1】:

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

    这可能是因为com.cetin.services.Imp.MyServiceImp 不在 Spring 应用程序上下文中。

    为此类创建&lt;bean id="..." ....&gt; 初始化,并使用Setter 注入注入SessionFactory。

    否则将其标记为@Repository 并使其对component-scan 可见

    【讨论】:

      猜你喜欢
      • 2018-09-25
      • 2013-06-07
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 2011-12-28
      • 2017-04-11
      相关资源
      最近更新 更多