[英]sessionFactory bean : null pointer exception while using setter injection
I am getting null pointer exception. 我收到空指针异常。 The setter injection is not working I guess.
我猜二传手的注射不能用。 I tried to look into server logs but not being able to find the root cause.
我试图查看服务器日志,但无法找到根本原因。 Any help would be appreciated.
任何帮助,将不胜感激。
applicationContext.xml
: applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<!-- Database Configuration -->
<import resource="DataSource.xml"/>
<import resource="HibernateSessionFactory.xml"/>
<!-- Beans Declaration -->
<import resource="UsersBean.xml"/>
</beans>
DataSource.xml
: DataSource.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">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test;create=true" />
<property name="username" value="root" />
<property name="password" value="deflection" />
</bean>
</beans>
UsersBean.xml
: UsersBean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean id="mainAction" class="com.riteshsangwan.integration.MainAction">
</bean>
</beans>
ModelClass: 型号类别:
package com.riteshsangwan.integration;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class ModelClass implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private long mid;
private String name;
private String email;
public long getMid() {
return mid;
}
public void setMid(long mid) {
this.mid = mid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
MainAction.java
: MainAction.java
:
package com.riteshsangwan.integration;
import com.opensymphony.xwork2.ActionSupport;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class MainAction extends ActionSupport
{
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public String execute(){
ModelClass m=new ModelClass();
m.setName("Ritesh");
m.setEmail("sangwan.ritesh@yahoo.in");
Session s=sessionFactory.openSession();
s.beginTransaction();
s.save(m);
s.getTransaction().commit();
s.close();
return SUCCESS;
}
}
EDITS: EDITS:
HibernateSessionFactory.xml
: 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">
<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" scope="singleton">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="packagesToScan" value="com.riteshsangwan.integration" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>
pom.xml
: pom.xml
:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.16</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.7.SP1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.7.SP1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
struts.xml
: struts.xml
:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="main" class="com.riteshsangwan.integration.MainAction">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
As far as your actions bean managed by spring try 至于您的动作bean由春季管理尝试
<constant name="struts.objectFactory" value="spring" />
which is used by default in struts-plugin.xml
of the spring plugin. 默认在spring插件的
struts-plugin.xml
中使用。
in the action config use bean id as a value for class
attribute 在操作配置中,使用bean id作为
class
属性的值
<action name="main" class="mainAction">
As far as you are using Spring annotations include in the spring config 就您使用的Spring注释而言,包含在spring配置中
<context:component-scan base-package="com.riteshsangwan.integration"/>
also make sure the Struts spring plugin is deployed. 还请确保已部署Struts spring插件 。 Add the following dependency to
pom.xml
: 将以下依赖项添加到
pom.xml
:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.16</version>
</dependency>
Detailed explanation with examples you could find here . 您可以在此处找到带有示例的详细说明。
The version of xsd schema is not the same as in pom.xml, you should update spring config to the version you use. xsd模式的版本与pom.xml中的版本不同,您应该将spring config更新为您使用的版本。
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
is available for spring 3.2. 适用于3.2版弹簧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.