Java spring mvc自动连线sessionFactory提供空对象
在Java spring mvc自动连线sessionFactory提供空对象,java,spring,spring-mvc,autowired,sessionfactory,Java,Spring,Spring Mvc,Autowired,Sessionfactory,在spring中,mvcautowired sessionFactory提供空对象。但同样的程序对我有效,但这次不行。请看一下我的代码 spring-servlet.xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan
spring中,mvc
autowired sessionFactory
提供空对象。但同样的程序对我有效,但这次不行。请看一下我的代码
spring-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Add support for component scanning -->
<context:component-scan base-package="com.loveTodo.springPractice" >
</context:component-scan>
<!-- Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Step 1: Define Database DataSource / connection pool -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false" />
<property name="user" value="hbstudent" />
<property name="password" value="hbstudent" />
<!-- these are connection pool properties for C3P0 -->
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="30000" />
</bean>
<!-- Step 2: Setup Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="com.loveTodo.springPractice.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Step 4: Enable configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="myTransactionManager" />
</beans>
DAO接口
package com.loveTodo.springPractice.dao.inter;
public interface SpringFormLoginDAOInter
{
boolean authencateUser(String userName,String password);
}
以及DAO实现类
package com.loveTodo.springPractice.dao.imp;
import java.util.List;
import javax.transaction.Transactional;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.loveTodo.springPractice.dao.inter.SpringFormLoginDAOInter;
import com.loveTodo.springPractice.entity.StudentLogin;
@Repository
public class SpringFormLoginDAOImpl implements SpringFormLoginDAOInter
{
@Autowired
private SessionFactory sessionFactory;
@Transactional
@Override
public boolean authencateUser(String userName, String password)
{
Session currentSession=sessionFactory.getCurrentSession();
System.out.println(currentSession);
boolean userStatus=false;
try {
if(userName !=null && password !=null)
{
System.out.println("before");
List<StudentLogin> list=currentSession.createQuery("from StudentLogin where userName = :userName and password = :password ",StudentLogin.class).setParameter(1, userName)
.setParameter(2, password).list();
if(!list.isEmpty())
{
userStatus=true;
}
System.out.println("after");
}
}
catch (Exception e) {
e.printStackTrace();
}
return userStatus;
}
}
我在谷歌上搜索了很多次,但都没能找到问题所在,我是spring的新手,请帮我解决一下删除这行
SpringFormLoginDAOInter daoObj=new SpringFormLoginDAOImpl();
并在控制器中自动连接您的SpringFormLoginDAOInter
删除此行
SpringFormLoginDAOInter daoObj=new SpringFormLoginDAOImpl();
并在控制器中自动连接您的
SpringFormLoginDAOInter
在控制器中,我看到:
SpringFormLoginDAOInter daoObj=new SpringFormLoginDAOImpl();
dao必须使用自动布线。这就是使用Spring的全部要点——使用依赖项注入
将要进行的更改:
@Repository
添加到SpringFormLoginDAOInter
类上方PS:您需要注意命名约定:)在控制器中,我看到以下内容:
SpringFormLoginDAOInter daoObj=new SpringFormLoginDAOImpl();
dao必须使用自动布线。这就是使用Spring的全部要点——使用依赖项注入
将要进行的更改:
@Repository
添加到SpringFormLoginDAOInter
类上方PS:您需要注意命名约定:)没有重复项没有重复项,但我得到的是
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“transactionManager”的bean:没有为限定符“transactionManager”找到匹配的PlatformTransactionManager bean-既没有限定符匹配,也没有bean匹配名字匹配
在从DAO中删除@Transactional注释后,是否可以尝试删除该注释?或者尝试在xml文件的步骤3和步骤4中将事务管理器名称从“myTransactionManager
”更改为“transactionManager
”创建服务层并执行声明性事务(@Transactional)在服务层中,但我得到了这个org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“transactionManager”的bean:没有为限定符“transactionManager”找到匹配的PlatformTransactionManager bean-限定符和bean名称都不匹配
在从DAO中删除@Transactional注释后,是否可以尝试删除该注释?或者尝试在xml文件的步骤3和步骤4中将事务管理器名称从“myTransactionManager
”更改为“transactionManager
”创建服务层并执行声明性事务(@Transactional)在服务层