我正在尝试集成 Struts 2、Spring 3(用于 DI)、Hibernate 4。 Spring 似乎进行了注入,但是在执行方法中引用注入的对象时,它会引发空指针异常。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/SpringBeans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
applicationcontext.xml 命名为 SpringBeans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">
<!-- Database Configuration -->
<import resource="DataSource.xml"/>
<import resource="HibernateSessionFactory.xml"/>
<!-- Beans Declaration -->
<import resource="EmployeeBean.xml"/>
</beans>
HibernateSessionFactory.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<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="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/example/bean/Employee.hbm.xml</value>
</list>
</property>
</bean>
</beans>
EmployeeBean.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="registerAction" class="com.example.register.action.RegisterAction">
<property name="registerBO" ref="registerBO" />
</bean>
<bean id="registerBO" class="com.example.boimplementation.RegisterBOImpl" >
<property name="employeeDAO" ref="employeeDAO" />
</bean>
<bean id="employeeDAO" class="com.example.daoimplementation.EmployeeDAOImpl" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
Struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.action.extension" value=","/>
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="global" />
<package name="register" namespace="/" extends="struts-default">
<action name="register" class="com.example.register.action.RegisterAction"
method="executeAction">
<result name="success">pages/register.jsp</result>
<result name="error">pages/register.jsp</result>
</action>
<action name="" class="com.example.register.action.RegisterAction"
method="returnMethod">
<result name="success">pages/register.jsp</result>
</action>
</package>
</struts>
动作类
package com.example.register.action;
import com.opensymphony.xwork2.ModelDriven;
import com.example.bean.Employee;
import com.example.bointerface.RegisterBO;
public class RegisterAction implements ModelDriven<Employee>{
RegisterBO registerBO;
String test;
private Employee emp = new Employee();
public void setRegisterBO(RegisterBO registerBO){
//registerBO.registerEmployee(emp);
this.registerBO = registerBO;
test = "Hello!!";
}
public RegisterBO getRegisterBO(){
return registerBO;
}
public String executeAction(){
System.out.println(test);
if(registerBO.registerEmployee(emp))
return "success";
else
return "error";
}
public String returnMethod(){
return "success";
}
@Override
public Employee getModel() {
// TODO Auto-generated method stub
return emp;
}
}
在上述操作中,setRegisterBO() 被调用,但是在 executeAction() 中引用 registerBO 对象时,它抛出空指针异常,并且测试字符串变量也打印空。有人可以告诉我在这里做错了什么。