gpt4 book ai didi

java - 通过@Resource创建bean时出错

转载 作者:行者123 更新时间:2023-12-01 04:36:58 25 4
gpt4 key购买 nike

我厌倦了使用 spring+hibernate+maven+postgreSQL 构建一个小型应用程序,但是当我厌倦在我的 Controller 和服务类中注入(inject)依赖项时,它会产生注入(inject)依赖项的问题,这里是代码和日志,请提供您的有建议解决这个接线问题吗?

mvc-dispature-servlet.xml

<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"
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-3.0.xsd">


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



<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/bms" />
<property name="username" value="postgres" />
<property name="password" value="*******" />
</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.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>

<property name="annotatedClasses">
<list>
<value>com.bms.Domain.Book</value>
</list>
</property>

</bean>

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

Controller class BooksEntryController.java

@Controller
@RequestMapping(value = "/dashboard/*")
public class BooksEntryController {

@Resource
private BookFormService bookFormServiceImp;

@RequestMapping(value = "/booksentryform", method = RequestMethod.GET)
public ModelAndView viewBooksEntryForm(Model model) {
model.addAttribute("test", new Book());
return new ModelAndView("bookregistrationform");
}

@RequestMapping(value = "/addbookdetails", method = RequestMethod.POST)
public String addBook(@ModelAttribute("test") Book book, Model model) {

if (book != null) {
bookFormServiceImp.addBooks(book);
return "bookaddsuccessfully";

} else {
throw new NullPointerException();
}

}
}


Service interface BookFormService.java

public interface BookFormService {
void addBooks(Book book);
}

ServiceImp class BookFormServiceImp.java

@Service("bookFormServiceImp")

public class BookFormServiceImp implements BookFormService {

@Resource
private BookFormDao bookFormDaoImp ;

public void addBooks(Book book) {
bookFormDaoImp.addBook(book);
// TODO Auto-generated method stub

}

}


Dao interface BookFormDao .java

public interface BookFormDao {
public void addBook(Book book); }

DaoImp class BookFormDaoImp.java

@Repository("bookFormDaoImp")
public class BookFormDaoImp implements BookFormDao {
@Resource
private SessionFactory sessionFactory;

private Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}

public void addBook(Book book) {
// TODO Auto-generated method stub
getCurrentSession().save(book);

}

}

这是日志:

2013-06-20 00:35:51.528:INFO:/bms:Initializing Spring root WebApplicationContext
Jun 20, 2013 12:35:51 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Jun 20, 2013 12:35:51 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Thu Jun 20 00:35:51 IST 2013]; root of context hierarchy
Jun 20, 2013 12:35:51 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
Jun 20, 2013 12:35:52 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2df8f8: defining beans [booksEntryController,bookFormDaoImp,bookFormServiceImp,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,dataSource,sessionFactory,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Jun 20, 2013 12:35:52 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2df8f8: defining beans [booksEntryController,bookFormDaoImp,bookFormServiceImp,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,dataSource,sessionFactory,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Jun 20, 2013 12:35:52 AM org.springframework.web.context.ContextLoader initWebApplicationContext
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'booksEntryController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookFormServiceImp': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookFormDaoImp': Injection of resource dependencies failed; nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider;
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
...................................
....................................

最佳答案

您可能没有使用正确的sessionFactory类。如果您使用的是 Hibernate 4,请使用 LocalSessionFactoryBean 而不是 AnnotationSessionFactoryBean

替换

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

关于java - 通过@Resource创建bean时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17200814/

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