Hello !
I am currently developping an application using Spring MVC, Hibernate and Vaadin.
When I inject a Service in my Application class (with @autowired) everything works fine. The problem is that I cant use this Spring annotation in other classes.
Here is my application class. In this case, I am able to inject my Service and eveything works fine.
public class SubgesteApplication extends Application {
@Autowired
private HospitalService service;
@Override
public void init() {
....
ArrayList<Hospital> hospitals = (ArrayList<Hospital>) service.listHospitals();
Iterator it = hospitals.iterator();
while (it.hasNext()) {
Hospital hospital = (Hospital)it.next();
String labelToShow = "id : " + hospital.getId() + " name : " + hospital.getName() + " adresse : " + hospital.getCity();
System.out.println(labelToShow);
}
}
Now, in my Application class, I am creating a SplitPanel. I would like to inject this service in this new class, instead of the Application class.
public class HospitalSplitPanel extends SplitPanel implements Button.ClickListener, Property.ValueChangeListener, ItemClickListener {
@Autowired
private HospitalService service;
public HospitalSplitPanel() {
super(SplitPanel.ORIENTATION_HORIZONTAL);
ArrayList<Hospital> hospitals = (ArrayList<Hospital>) service.listHospitals();
}
}
Unfortunately this time I get a NullPointerException. When I debug my programm, I can see that my service is null.
Here is my web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- $Id$ -->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>My app</display-name>
<description>My app</description>
<!-- Spring context loader -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring web request listener: makes the current web request available in a private ThreadLocal -->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- Vaadin servlet -->
<servlet>
<servlet-name>subgeste</servlet-name>
<servlet-class>net.sebpx.txvdn.vaadin.util.WebContextApplicationServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>net.sebpx.txvdn.vaadin.SubgesteApplication</param-value>
</init-param>
<init-param>
<param-name>productionMode</param-name>
<param-value>false</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>subgeste</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
And my applicationContext.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- $Id$ -->
<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.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">
<!-- Activate Spring annotation support -->
<context:annotation-config/>
<context:component-scan base-package="net.sebpx.txvdn" />
<!-- Example bean -->
<!-- <bean class="net.sebpx.txvdn.vaadin.MyBean" p:name="foobar"/>
<bean class="net.sebpx.txvdn.controller.ContactController"/> -->
<!-- Vaadin application -->
<bean scope="session" class="net.sebpx.txvdn.vaadin.SubgesteApplication"/>
<bean scope="session" class="net.sebpx.txvdn.vaadin.hospital.HospitalSplitPanel"/>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<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 id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>/WEB-INF/hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
Does someone know how I could resolve this ?
Thank you !
Sebastian