各位正在使用Spring-MVc和hibernate以及Spring Transaction
创建一个Web应用程序当我将@Repository添加到我的类时,它会产生错误: 错误是:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'numberGeneratorImpl' defined in file [E:\JAVA\BankEE\out\artifacts\BankEE_war_exploded\WEB-INF\classes\model\bl\NumberGeneratorImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:633)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:602)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:521)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:462)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1216)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1135)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1031)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4957)
at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5284)
at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5279)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.checkDaoConfig(HibernateDaoSupport.java:123)
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1572)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean
现在我将向您展示我的应用类和界面:
这是INumberGenerator接口:
public interface INumberGenerator {
public String returnNumber();
}
这是NumberGeneratorImpl(在这个类中因为@Repository给我错误):
@Repository
public class NumberGeneratorImpl extends HibernateDaoSupport implements INumberGenerator {
@Override
@Transactional
public String returnNumber() {
List<String> list = getHibernateTemplate().find("select n.number from NumberGenerator n ");
Integer newNumber = Integer.parseInt(list.get(0))+1;
getHibernateTemplate().bulkUpdate("update NumberGenerator n set n.number=?",newNumber.toString());
return list.get(0);
}
}
这是我的控制器(Spring-mvc):
@Controller
public class MainController {
@Autowired
INumberGenerator numberGenerator;
@RequestMapping(//something)
public String test(//request and response)
{
numberGenerator.returnNumber();
}
最后这是我的mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
"default-autowire="byName">
<mvc:annotation-driven/>
<tx:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="model.bl"/>
<context:component-scan base-package="controller"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
<bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/bankee"/>
<property name="username" value="root"/>
<property name="password" value="myjava123"/>
</bean>
<bean id="localSessionFactoryBean" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="driverManagerDataSource"/>
<property name="mappingResources">
<list>
//hbm definitions
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="numberGenerator" class="model.bl.NumberGeneratorImpl">
<property name="sessionFactory" ref="localSessionFactoryBean"/>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="localSessionFactoryBean" />
</bean>
</beans>
任何人都可以告诉我@Repository有什么问题??? 当我删除@Repository
时,错误将消失答案 0 :(得分:2)
我认为你的mvc-dispatcher-servlet.xml文件中遗漏了以下行。
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="localSessionFactoryBean"/>
</bean>
答案 1 :(得分:1)
你有一个component-scan
可以检索所有的@Repository,实际上问题在于你的spring上下文中没有的bean“numberGeneratorImpl”。您必须从组件扫描中排除它,否则您有2个NumberGeneratorImpl.class bean
numberGeneratorImpl
numberGenerator
你有两种方式..
第一个解决方案
添加您的上下文
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="localSessionFactoryBean"/>
</bean>
删除以下声明
<bean id="numberGenerator" class="model.bl.NumberGeneratorImpl">
<property name="sessionFactory" ref="localSessionFactoryBean"/>
</bean>
并修改
@Repository
@Qualifier("numberGenerator")
public class NumberGeneratorImpl extends HibernateDaoSupport implements INumberGenerator {
第二个解决方案
从component-scan
<context:component-scan base-package="model.bl">
<!--other filters-->
<!--.......-->
<context:exclude-filter expression="model\.bl\.NumberGeneratorImpl" type="regex" />
</context:component-scan>
我希望它有用。
添加.. bean hibernateTemplate
不是必需的,当您使用HibernateDaoSupport时,只需要sessionFactory
或hibernateTemplate
中的一个。您还可以为localSessionFactoryBean
创建别名。
<alias name="localSessionFactoryBean" alias="sessionFactory" />