एक प्रोजेक्ट सेट अप करने का प्रयास कर रहे हैं लेकिन स्प्रिंग के माध्यम से ऑटोवॉयरिंग ऑब्जेक्ट्स में विफल हो जाते हैं।@Autowired ऑब्जेक्ट्स शून्य मान प्राप्त कर रहे हैं
package se.hsr.web;
public class TestRunner {
public static void main(String[] args) {
ContactDAO cd = new ContactDAOImpl();
Contact contact = new Contact();
contact.setFirstname("Zorro");
cd.addContact(contact);
}
}
package se.hsr.web;
इसे चलाने से मुझे cd.addContact लागू होने पर एक NullPointerException देता है। ContactDaoImpl:
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class ContactDAOImpl implements ContactDAO {
@Autowired
private SessionFactory sessionFactory;
public void addContact(Contact contact) {
sessionFactory.getCurrentSession().save(contact);
}
मेरे सर्वलेट फ़ाइल:
<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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:component-scan
base-package="se.hsr.web"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</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>classpath: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>
<bean id="contactDAOImpl"
class="se.hsr.web.ContactDAOImpl"/>
<context:annotation-config/>
</beans>
मेरे hibernate.cfg.xml फ़ाइल:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="se.hsr.web.Contact" />
</session-factory>
</hibernate-configuration>
मेरे 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">
<display-name>HSRMVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>HSR</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HSR</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
मुझे लगता है कि त्रुटि वें है सत्र फ़ैक्टरी पर @Autowired के माध्यम से शुरू नहीं किया जा रहा है, लेकिन वह क्यों है? क्या यह एक साधारण निर्देशिका संरचना/फ़ाइलपैथ समस्या हो सकती है या यह कुछ और जटिल है?
अग्रिम धन्यवाद।
अद्यतन: ContactDAOImpl वर्ग:
@Repository
public class ContactDAOImpl extends HibernateDaoSupport implements ContactDAO{
@Autowired
@Qualifier("sessionFactory")
private SessionFactory sessionFactory;
public void addContact(Contact contact) {
sessionFactory.getCurrentSession().save(contact);
}
के संभावित डुप्लिकेट के साथ
ContactDAOImpl
व्याख्या द्वारा "मैन्युअल" instanciate के लिए, आप इसे ठीक कर सकते, सक्षम होना चाहते हैं [क्यों मेरी स्प्रिंग @Autowired फ़ील्ड रिक्त है ?] (http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – chrylis