【发布时间】:2016-08-02 11:24:01
【问题描述】:
对不起,我重复我的问题,因为错误没有得到纠正。
我将service 和dao 层作为接口。我没有制作任何hibernate cfg xml文件,但我在应用程序上下文下编写了所有hibernate配置。
这只是一个测试程序。
当我试图从实体类中获取用户名和密码时,会话工厂总是指向 null。如果您需要更具体的代码,只需提及即可。
应用上下文:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:component-scan base-package="com.company"/>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>/WEB-INF/*.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}"
p:username="${jdbc.username}" p:password="${jdbc.password}">
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.company.entity"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.Dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</prop>
</props>
</property>
<property name="entityInterceptor">
<bean class="com.company.interceptor.AuditInterceptor"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*DaoImpl</value>
</property>
<property name="interceptorNames">
<value>transactionInterceptor</value>
</property>
</bean>
<import resource="spring-security.xml"/>
</beans>
用户道实现:
@Repository
public class UserDaoImpl extends AbstractDao implements UserDao {
public boolean test(){
int id=1;
boolean result;
try{
User user=null;
user=(User) this.sessionFactory.getCurrentSession().get(User.class,id);
System.out.println(user.getUsername());
System.out.println(user.getPassword());
result=true;
return result;
}catch(NullPointerException n){
System.out.println("inside null pointer exception handler");
n.printStackTrace();
result=false;
return result;
}
用户服务:
@Service
public class UserServiceImpl implements UserService {
public boolean test(){
UserDao userDao=new UserDaoImpl();
return userDao.test();
}
}
用户控制器:
@Controller
public class LoginController {
private Logger logger;
@Autowired
UserService userService;
@RequestMapping(value="/login.do",method=RequestMethod.POST)
public String login(HttpServletRequest request,HttpServletResponse response){
boolean result=userService.test();
if(result=true){
return "profile";
}else{
return "home";
}
}
抽象道:
public class AbstractDao{
@Autowired
protected SessionFactory sessionFactory;
}
【问题讨论】:
-
你的
abstractdao不是spring bean,因此你怎么能autowiresessionfactory 呢? -
那怎么改呢??
-
尝试在 AbstractDao 中添加@Repository 或者你可以使用
HibernateDaoSupport或HIbernateTemplate -
对不起,我只是将它命名为抽象 dao.. 它是为检索 sessionFactory 对象而创建的一个类。我之前删除了自动连线,但没有发生任何事情,...这里重复相同的错误是我的堆栈跟踪:
-
java.lang.NullPointerException at com.company.daoImpl.UserDaoImpl.MastersList(UserDaoImpl.java:35) at com.company.serviceImpl.UserServiceImpl.MastersList(UserServiceImpl.java:18) at com. company.controller.LoginController.login(LoginController.java:44) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke( DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:483)
标签: java mysql spring hibernate sessionfactory