我是 Spring MVC 和 hibernate 的新手,我一直处于低迷状态,所以我希望你们能提供帮助。 每次我运行时都会在创建 bean 时遇到异常错误。我似乎无法在互联网上找到正确的答案, 我似乎找不到我在这里丢失的东西。我希望你们能帮助我。 我的上下文.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:annotation-config/>
<context:component-scan base-package="com.test.cedric." />
<beans:bean id="departmentController" class="com.test.cedric.controller.DepartmentController" />
<beans:bean id="departmentDaoImpl" class="com.test.cedric.dao.DepartmentDaoImpl">
</beans:bean>
<beans:bean id="departmentServiceImpl" class="com.test.cedric.service.DepartmentServiceImpl">
</beans:bean>
</beans:beans>
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
My hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test1</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<property name="hbm2ddl.auto">validate</property>
<mapping resource="com.test.cedric.model.Department"/>
</session-factory>
</hibernate-configuration>
我的服务代码
package com.test.cedric.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.test.cedric.dao.DepartmentDao;
import com.test.cedric.dao.DepartmentDaoImpl;
import com.test.cedric.model.Department;
@Service
public class DepartmentServiceImpl implements DepartmentService{
@Autowired
DepartmentDao dDao;
@Transactional
@Override
public void addDept(Department department) {
dDao.addDept(department);
// TODO Auto-generated method stub
}
@Override
public void delDept(Department department) {
dDao.delDept(department);
// TODO Auto-generated method stub
}
@Override
public void editDept(Department department) {
dDao.editDept(department);
// TODO Auto-generated method stub
}
@Override
public List<Department> getAllDept() {
// TODO Auto-generated method stub
return dDao.getAll();
}
@Override
public Department getDeptById(int dept_id) {
// TODO Auto-generated method stub
return null;
}
}
我的道
package com.test.cedric.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.test.cedric.model.Department;
@Repository
public class DepartmentDaoImpl implements DepartmentDao {
@Autowired
SessionFactory sessionFactory;
@Override
public void addDept(Department department) {
sessionFactory.getCurrentSession().saveOrUpdate(department);
}
@Override
public void delDept(Department department) {
sessionFactory.getCurrentSession().delete(department);
}
@Override
public void editDept(Department department) {
sessionFactory.getCurrentSession().saveOrUpdate(department);
// TODO Auto-generated method stub
}
@SuppressWarnings("unchecked")
@Override
public List<Department> getAll() {
sessionFactory.getCurrentSession().createQuery("From Department");
// TODO Auto-generated method stub
return sessionFactory.getCurrentSession().createQuery("From Department").list();
}
@Override
public Department getDeptById(int dept_id) {
// TODO Auto-generated method stub
return null;
}
}
最佳答案
首先,你有以下内容
<context:component-scan base-package="com.test.cedric." />
<beans:bean id="departmentController" class="com.test.cedric.controller.DepartmentController" />
<beans:bean id="departmentDaoImpl" class="com.test.cedric.dao.DepartmentDaoImpl">
</beans:bean>
<beans:bean id="departmentServiceImpl" class="com.test.cedric.service.DepartmentServiceImpl">
</beans:bean>
component-scan
扫描您声明的包,如果它找到注释 @Component
, @Service
, @Repository
, @Controller
, 或 @Configuration
将为您创建一个 bean 实例。因此,上面您自己的 bean 声明是多余的。摆脱他们。
其次,除非您不显示它,否则您实际上并没有 SessionFactory
bean,它是 Autowiring 的候选者。 <session-factory>
hibernate 配置 中的元素与 Spring 上下文 bean 无关。如果你想连接两者,你可以使用
<bean
id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation">
<value>
classpath:location_of_config_file/hibernate.cfg.xml
</value>
</property>
<property name="hibernateProperties">
<props>
...
</props>
</property>
</bean>
关于java - 注入(inject) Autowiring 的依赖项失败;无法 Autowiring 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18945555/