gpt4 book ai didi

java - 在 Spring 事务管理器中创建 bean 时出错

转载 作者:行者123 更新时间:2023-11-30 08:19:45 25 4
gpt4 key购买 nike

大家好,我正在使用 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 接口(interface):

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

这是我的 Controller (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 时错误将消失

最佳答案

我认为您的 mvc-dispatcher-servlet.xml 文件中缺少下面一行。

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="localSessionFactoryBean"/>
</bean>

关于java - 在 Spring 事务管理器中创建 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26586226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com