2011-10-04 112 views
0

这里是我的异常弹簧自动装配和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

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

它可能发生是因为com.cetin.services.Imp.MyServiceImp超出了Spring应用程序上下文。

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

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