Guten morgen,
Ich hoffe dass der Post in diesem Unterforum richtig aufgehoben ist. Da ich Spring und Hibernate benutze könnte der Fehler auch in der Hibernate Konfiguration liegen, aber ich vermute eher das ich im Spring Teil einen Fehler gemacht habe.
Ich benutze insgesammt eine Kombination aus Hibernate + Spring + ZK (Ajax Framework für die Web schicht). Habe mir in applicationConfig.xml die Beans definiert und diese sollen durch setter-based Dependency Injection übergeben werden.
Allerdings erhalte ich immer NullPointerExceptions, da diese Beans anscheinend nicht instantiiert werden.
Habe meine Konfiguration anhand diverser tutorials schon mehrmals überarbeitet, aber bisher noch keine Lösung gefunden.
Hier mal meine momentane Konfiguration:
applicationConfig.xml
[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.5.xsd
Index of /schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
Index of /schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- HIBERNATE DEFINITIONEN -->
<!-- Hibernate SessionFactory -->
<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" />
<property name="username" value="root" />
<property name="password" value="aqua" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>domain.Standort</value>
<value>domain.Hersteller</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="connection.pool_size">1</prop>
<prop key="current_session_context_class">thread</prop>
<prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Es wird setter-based dependency injection benutzt -->
<!-- Daos, die Klasse verweist auf die Implementation, nicht auf das Interface -->
<bean id="HerstellerDao" class="dao.HerstellerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="StandortDao" class="dao.StandortDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- Service -->
<bean id="HerstellerService" class="service.HerstellerServiceImpl">
<property name="herstellerDao" ref="HerstellerDao"></property>
</bean>
<bean id="StandortService" class="service.StandortServiceImpl">
<property name="standortDao" ref="StandortDao"></property>
</bean>
<!-- Controller -->
<bean id="StandortController" class="controller.StandortController" scope="prototype">
<property name="standortService" ref="StandortService"></property>
</bean>
<bean id="HerstellerController" class="controller.HerstellerController" scope="prototype">
<property name="herstellerService" ref="HerstellerService"></property>
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<!-- Transaction Manager, alle mit @Transactional gekennzeichneten Klassen werden damit aktiviert -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- a PlatformTransactionManager is still required -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- (this dependency is defined somewhere else) -->
<property name="dataSource" ref="dataSource"/>
</bean>
<aop:config>
<aop:advisor pointcut="execution(* *..Service.*(..))" advice-ref="txAdvice"/>
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="insert*" />
<tx:method name="update*" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
</beans>
[/XML]
web.xml (Bin mir nicht sicher ob die wichtig ist)
[XML]
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Stammdatenpflege</display-name>
<!-- AB HIER SPRING -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- ENDE SPRING DEFINITIONEN -->
<listener>
<description>
Used to cleanup when a session is destroyed</description>
<display-name>ZK Session cleaner</display-name>
<listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
</listener>
<servlet>
<description>
The ZK loader for ZUML pages</description>
<servlet-name>zkLoader</servlet-name>
<servlet-class>
org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
<init-param>
<param-name>update-uri</param-name>
<param-value>/zkau</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<description>
The asynchronous update engine for ZK</description>
<servlet-name>auEngine</servlet-name>
<servlet-class>
org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>zkLoader</servlet-name>
<url-pattern>*.zul</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>zkLoader</servlet-name>
<url-pattern>*.zhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>auEngine</servlet-name>
<url-pattern>/zkau/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>index.zul</welcome-file>
</welcome-file-list>
</web-app>
[/XML]
Hier mein HerstellerController, dieser benötigt eine Instanz von HerstellerService, die durch setter-based Dependency Injection gesetzt werden sollte. An dieser Stelle bekomme ich dann die NullPointerException, da HerstellerService immer null ist und somit in getAlleHersteller() die Exception fliegt.
HerstellerService:
HerstellerDao
Hersteller
Dieses Problem beschäftigt mich schon seit einigen Tagen und ich habe leider keine Idee wo mein Fehler liegt. Daher habe ich mehr als nur die Konfigurationsdateien hier eingefügt, evtl. gibt das ja jemandem Aufschluss über das Problem.
Wenn ich den HerstellerService übergehe und stattdessen direkt auf das Dao zugreife ist dieses NULL, das injecten scheint daher allgemein nicht zu funktionieren, weshalb ich glaube dass der Fehler in der applicationContext.xml liegen muss.
Vielen Dank schonmal für eure Hilfe!
Ich hoffe dass der Post in diesem Unterforum richtig aufgehoben ist. Da ich Spring und Hibernate benutze könnte der Fehler auch in der Hibernate Konfiguration liegen, aber ich vermute eher das ich im Spring Teil einen Fehler gemacht habe.
Ich benutze insgesammt eine Kombination aus Hibernate + Spring + ZK (Ajax Framework für die Web schicht). Habe mir in applicationConfig.xml die Beans definiert und diese sollen durch setter-based Dependency Injection übergeben werden.
Allerdings erhalte ich immer NullPointerExceptions, da diese Beans anscheinend nicht instantiiert werden.
Habe meine Konfiguration anhand diverser tutorials schon mehrmals überarbeitet, aber bisher noch keine Lösung gefunden.
Hier mal meine momentane Konfiguration:
applicationConfig.xml
[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.5.xsd
Index of /schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
Index of /schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- HIBERNATE DEFINITIONEN -->
<!-- Hibernate SessionFactory -->
<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" />
<property name="username" value="root" />
<property name="password" value="aqua" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>domain.Standort</value>
<value>domain.Hersteller</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="connection.pool_size">1</prop>
<prop key="current_session_context_class">thread</prop>
<prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Es wird setter-based dependency injection benutzt -->
<!-- Daos, die Klasse verweist auf die Implementation, nicht auf das Interface -->
<bean id="HerstellerDao" class="dao.HerstellerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="StandortDao" class="dao.StandortDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- Service -->
<bean id="HerstellerService" class="service.HerstellerServiceImpl">
<property name="herstellerDao" ref="HerstellerDao"></property>
</bean>
<bean id="StandortService" class="service.StandortServiceImpl">
<property name="standortDao" ref="StandortDao"></property>
</bean>
<!-- Controller -->
<bean id="StandortController" class="controller.StandortController" scope="prototype">
<property name="standortService" ref="StandortService"></property>
</bean>
<bean id="HerstellerController" class="controller.HerstellerController" scope="prototype">
<property name="herstellerService" ref="HerstellerService"></property>
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<!-- Transaction Manager, alle mit @Transactional gekennzeichneten Klassen werden damit aktiviert -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- a PlatformTransactionManager is still required -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- (this dependency is defined somewhere else) -->
<property name="dataSource" ref="dataSource"/>
</bean>
<aop:config>
<aop:advisor pointcut="execution(* *..Service.*(..))" advice-ref="txAdvice"/>
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="insert*" />
<tx:method name="update*" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
</beans>
[/XML]
web.xml (Bin mir nicht sicher ob die wichtig ist)
[XML]
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Stammdatenpflege</display-name>
<!-- AB HIER SPRING -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- ENDE SPRING DEFINITIONEN -->
<listener>
<description>
Used to cleanup when a session is destroyed</description>
<display-name>ZK Session cleaner</display-name>
<listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
</listener>
<servlet>
<description>
The ZK loader for ZUML pages</description>
<servlet-name>zkLoader</servlet-name>
<servlet-class>
org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
<init-param>
<param-name>update-uri</param-name>
<param-value>/zkau</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<description>
The asynchronous update engine for ZK</description>
<servlet-name>auEngine</servlet-name>
<servlet-class>
org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>zkLoader</servlet-name>
<url-pattern>*.zul</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>zkLoader</servlet-name>
<url-pattern>*.zhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>auEngine</servlet-name>
<url-pattern>/zkau/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>index.zul</welcome-file>
</welcome-file-list>
</web-app>
[/XML]
Hier mein HerstellerController, dieser benötigt eine Instanz von HerstellerService, die durch setter-based Dependency Injection gesetzt werden sollte. An dieser Stelle bekomme ich dann die NullPointerException, da HerstellerService immer null ist und somit in getAlleHersteller() die Exception fliegt.
Java:
package controller;
import java.util.List;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Window;
import org.zkoss.zul.Listbox;
import service.HerstellerService;
import domain.Hersteller;
public class HerstellerController extends GenericForwardComposer{
HerstellerService herstellerService;
Hersteller current;
Window win;
Listbox box;
public Hersteller getCurrent() {
return current;
}
public void setCurrent(Hersteller current) {
this.current = current;
}
public void setHerstellerService(HerstellerService herstellerService)
{
this.herstellerService = herstellerService;
}
public List getAlleHersteller() {
return herstellerService.getAlleHersteller();
}
public void onClick$add() {
Hersteller newHersteller = new Hersteller(current.getNummer(),
current.getName());
//check ob es den Hersteller schon gibt momentan noch aus, da man evtl. auch Abkürzungen speichern möchte, z.B. [2,VW]
//check if the Hersteller already exists, indicated by the same Nummer
//if (!checkHersteller(newHersteller.getNummer()))
//{
//insert into database if the Hersteller is new
herstellerService.saveOrUpdate(newHersteller, newHersteller.getNummer(), newHersteller.getName());
//}
}
public void onClick$update() {
if (box.getSelectedItem() != null) {
Hersteller updateHersteller = (Hersteller) box.getSelectedItem().getValue();
//check ob es den Hersteller schon gibt momentan noch aus, da man evtl. auch Abkürzungen speichern möchte, z.B. [2,VW]
//check if the Hersteller already exists, indicated by the same Nummer
//if (!checkHersteller(updateHersteller.getNummer()))
//{
//commit changes into database if there is no conflict
herstellerService.saveOrUpdate(updateHersteller, updateHersteller.getNummer(), updateHersteller.getName());
//}
}
}
//private boolean checkHersteller(int nummer) {
//if (herstellerDAO.findByNummer(nummer) != null)
//{
//alert("Dieser Standort existiert bereits");
//return true;
//}
//return false;
//}
public void onClick$delete() {
if (box.getSelectedItem() != null) {
herstellerService.delete((Hersteller) box.getSelectedItem().getValue());
}
}
}
HerstellerService:
Java:
package service;
import dao.HerstellerDao;
import domain.Hersteller;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class HerstellerServiceImpl implements HerstellerService{
private HerstellerDao herstellerDao;
public void setHerstellerDao(HerstellerDao herstellerDao) {
this.herstellerDao = herstellerDao;
}
public Hersteller getHerstellerById(Long id)
{
return herstellerDao.findById(id);
}
public Hersteller getByNummer(int nummer){
return herstellerDao.findByNummer(nummer);
}
public List getAlleHersteller() {
return herstellerDao.findAll();
}
public void saveOrUpdate(Hersteller hersteller, int nummer, String name) {
herstellerDao.saveOrUpdate(hersteller, nummer, name);
}
public void delete(Hersteller hersteller) {
herstellerDao.delete(hersteller);
}
}
HerstellerDao
Java:
package dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import domain.Hersteller;
@Repository
public class HerstellerDaoImpl implements HerstellerDao{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
public void saveOrUpdate(Hersteller einHersteller, int nummer, String name)
{
einHersteller.setNummer(nummer);
einHersteller.setName(name);
sessionFactory.getCurrentSession().saveOrUpdate(einHersteller);
}
public void delete(Hersteller einHersteller) {
sessionFactory.getCurrentSession().delete(einHersteller);
}
public Hersteller findById(Long id) {
return (Hersteller) sessionFactory.getCurrentSession().get(Hersteller.class, id);
}
public Hersteller findByNummer(int nummer) {
List list = sessionFactory.getCurrentSession().createQuery("FROM Hersteller WHERE nummer = ?").setParameter(0, nummer).list();
if (!list.isEmpty()) {
return (Hersteller) list.get(0);
}
return null;
}
public List findAll() {
return sessionFactory.getCurrentSession().createQuery("FROM Hersteller").list();
}
}
Hersteller
Java:
package domain;
import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
@Entity
@Table(name="hersteller")
public class Hersteller implements Serializable{
private int hersteller_id;
private int nummer;
private String name;
protected Collection<Standort> standorte = new ArrayList<Standort>();
public Hersteller() {}
public Hersteller(int nummer, String name) {
setNummer(nummer);
setName(name);
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="hersteller_id")
public int getHersteller_id() {
return hersteller_id;
}
@Column(name="nummer")
public int getNummer() {
return nummer;
}
@Column(name="name")
public String getName() {
return name;
}
private void setHersteller_id(int hersteller_id) {
this.hersteller_id = hersteller_id;
}
public void setNummer(int nummer) {
this.nummer = nummer;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy="hersteller", fetch = FetchType.EAGER)
public Collection<Standort> getStandorte() {
return standorte;
}
public void setStandorte(Collection<Standort> coll) {
standorte = coll;
}
public void addStandort(Standort standort) {
standorte.add(standort);
}
public void removeStandort(Standort standort) {
standorte.remove(standort);
}
}
Dieses Problem beschäftigt mich schon seit einigen Tagen und ich habe leider keine Idee wo mein Fehler liegt. Daher habe ich mehr als nur die Konfigurationsdateien hier eingefügt, evtl. gibt das ja jemandem Aufschluss über das Problem.
Wenn ich den HerstellerService übergehe und stattdessen direkt auf das Dao zugreife ist dieses NULL, das injecten scheint daher allgemein nicht zu funktionieren, weshalb ich glaube dass der Fehler in der applicationContext.xml liegen muss.
Vielen Dank schonmal für eure Hilfe!