@Autowired objects getting a null value

Trying to set up a project, but crashing with Autowiring objects via Spring.

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; 

Doing this gives me a NullPointerException when calling cd.addContact. 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); } 

My servlet file:

  <?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> 

My hibernate.cfg.xml file:

 <?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> 

My web.xml file:

 <?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> 

I assume the error is that SessionFactory is not getting initialization via @Autowired correctly, but why is this? Maybe this is a simple problem with a directory / file variable or something more complicated?

Thanks in advance.

UPDATE: ContactDAOImpl class:

 @Repository public class ContactDAOImpl extends HibernateDaoSupport implements ContactDAO{ @Autowired @Qualifier("sessionFactory") private SessionFactory sessionFactory; public void addContact(Contact contact) { sessionFactory.getCurrentSession().save(contact); } 
+8
java spring java-ee autowired
source share
6 answers

To use Spring functions (auto-messaging, calling methods or aspects of post-construction), you must let Spring initialize instances instead of using new .

For example:

 public static void main(String[] args) { ApplicationContext context = AnnotationConfigApplicationContext("se.hsr.web") ContactDAO cd = (ContactDAO)context.getBean("contactDAOImpl"); Contact contact = new Contact(); contact.setFirstname("Zorro"); cd.addContact(contact); } 

AnnotationConfigApplicationContext scans classes in classes in the package se.hsr.web for classes with Spring annotations. Spring 3.0 is required to work. Before that, you should add the following line to your applicationContext.xml file:

 <context:component-scan base-package="se.hsr.web" /> 
+14
source share

This is needed at the top of the test class:

 @RunWith(SpringJUnit4ClassRunner.class) // ApplicationContext will be loaded from "/applicationContext.xml" and "/applicationContext-test.xml" // in the root of the classpath @ContextConfiguration(locations={"/applicationContext.xml", "/applicationContext-test.xml"}) public class MyTest { 

I suggested JUnit4; my supervision.

You need a context configuration tag in the application context somewhere, but I don't see the place in your code where you actually open the application context file and create the ApplicationContext. This is usually done in the setup method for your test. You are lucky if you create an ApplicationContext somewhere. Try reading the XML from your CLASSPATH in the setup method and see if that helps.

+2
source share

You need this in Spring configuration to work in autowiring mode

 xmlns:context="http://www.springframework.org/schema/context" .... <context:annotation-config/> 
+1
source share

Add @Component/@Repository to DAO / DAOImpl.

+1
source share

you create a POJO out of context of spring.

if you really want to be able to run manually, you can fix this by adding <context:spring-configured /> to your configuration and then annotating ContactDAOImpl with @Configurable

+1
source share

You need to get a ContactDAO instance from the Spring context. You start with the keyword new .

See the link below;

@ User annotation fails to insert bean in JUnit class

or if not unit test

 ClassPathResource resource = new ClassPathResource("beans.xml"); BeanFactory factory = new XmlBeanFactory(resource); beanFactory.getBean("nameOfYourBean"); 

http://static.springsource.org/spring/docs/2.0.x/reference/beans.html

0
source share

All Articles