【急】简单SSH注入失败问题

asirt_ 发布于 2016/10/19 17:13
阅读 948
收藏 0

很简单的ssh框架整合,搭建好后项目部署没有出错,运行后尝试添加一条数据出现
java.lang.NullPointerException, null
spring没有帮我注入,导致accountAction 的accountService为空
找了好久都没有找到问题的根源 以下代码

IAccount.java【接口】

package org.asirt.dao;
import org.asirt.entity.account;

/**
 * @author asirt
 *
 */
public interface IAccount {

public boolean saveAccount(account account);
}

accountDAO.java [实现类]

package org.asirt.dao.imlp;


import org.asirt.dao.IAccount;
import org.asirt.entity.account;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;


public class accountDAO  implements IAccount{
private SessionFactory sessionFactory;


public SessionFactory getSessionFactory() {
return sessionFactory;
}


public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}




@Override
public boolean saveAccount(account account) {
Session session = sessionFactory.openSession();
Transaction ts = session.beginTransaction();
session.save(account);
ts.commit();
session.close();
return true;
}
}

accountAction.java [Action类]

package org.asirt.action;


import org.apache.catalina.User;
import org.asirt.dao.IAccount;
import org.asirt.entity.account;


import com.opensymphony.xwork2.ActionSupport;


public class accountAction extends ActionSupport {
/**

*/
private static final long serialVersionUID = -453090639825021968L;

private account account;
private IAccount accountService;

public String go_add() throws Exception{
return"go_add";
}
public String add() throws Exception{
System.out.println(account.getName());
System.out.println(getAccountService());
if(account!=null){
getAccountService().saveAccount(account);
System.out.println("hah");
}
return"success";
}


public account getAccount() {
return account;
}


public void setAccount(account account) {
this.account = account;
}
public IAccount getAccountService() {
return accountService;
}
public void setAccountService(IAccount accountService) {
this.accountService = accountService;
}

}

applicationContext.xml [配置文件]

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-4.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
     http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">


<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://localhost:3306/asirt_bbs?useUnicode=true&amp;characterEncoding=utf-8">
</property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>


<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
</props>
</property>


<property name="mappingResources">
<list>
<value>org/asirt/entity/account.hbm.xml</value>
</list>
</property>
</bean>




<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>


<tx:annotation-driven transaction-manager="transactionManager" />




<bean id="accountImpl" class="org.asirt.dao.imlp.accountDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>


<bean id="accountAction" class="org.asirt.action.accountAction">
<property name="accountService" ref="accountImpl"></property>
</bean>
</beans>


struts.xml[配置文件]


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
<package name="default" extends="struts-default">


<action name="account-go_add" class="org.asirt.action.accountAction" method="go_add">
<result name="go_add">add.html</result>
</action>

<action name="account-add" class="org.asirt.action.accountAction" method="add">
<result name="success">success.html</result>
</action>
</package>


</struts>


配置好后我访问http://asirt:8080/ssh/account-add?account.name=asd

后台输入【部署项目是成功的,运行后 始终没有注入,找了好久了都没有发现问题!!求大神支招】

信息: Server startup in 9746 ms
asd
null
17:10:49.606 [http-nio-8080-exec-3] ERROR org.apache.struts2.dispatcher.DefaultDispatcherErrorHandler - Exception occurred during processing request: [java.lang.NullPointerException, null]







加载中
0
颓废的幻想者
颓废的幻想者
注入的实例和你实际使用不是一个实例导致 不打印一下 hashcode this.hashcode 看下在注入set方法中值多少 调用时 当前类值是多少 就明白了
0
求是科技
求是科技
https://my.oschina.net/u/2312022/blog/743014
0
天天堂堂
在web.xml文件里配置了struts2的filter了吗?还有ssh整合的时候还需要在web.xml文件里配置一个监听器来初始化spring容器
0
a
asirt_

引用来自“myjack”的评论

注入的实例和你实际使用不是一个实例导致 不打印一下 hashcode this.hashcode 看下在注入set方法中值多少 调用时 当前类值是多少 就明白了
<bean id="accountImpl" class="org.asirt.dao.imlp.accountDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>


<bean id="accountAction" class="org.asirt.action.accountAction">
<property name="accountService" ref="accountImpl"></property>

</bean>


是这里出现了问题吗?

颓废的幻想者
颓废的幻想者
这是注入的地方 你要看重定向 dispatcher 的地方 你用的是web.xml 定位 还是用的 structs 来分配 请求 用 servlet 和 用 structs 都是会出现这种问题。如果是用spring 自带的 那种方式 就不会出现这中问题例如 @Controller
0
a
asirt_
错误以及找出,类的路径写错了,但是启动的时候没有报错,非常感谢回答的人
返回顶部
顶部