I wrote a sample application which I am trying to autowire my CustomerService bean into my Application.class to do a System.out.println()
But it threw an exception saying root of factory hierarchy which I dont understand why I cant do so, except to retrieve the bean using the commented codes?
My error
Aug 03, 2014 9:52:42 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5f3d285f: startup date [Sun Aug 03 21:52:42 SGT 2014]; root of context hierarchy Aug 03, 2014 9:52:42 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [applicationContext.xml] Aug 03, 2014 9:52:43 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@25bcb56b: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,customerRepository,customerService,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy we are using constructor injection Exception in thread "main" java.lang.NullPointerException at Application.print(Application.java:20) at Application.main(Application.java:30) This is my code.
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.pluralsight.service.CustomerService; public class Application { @Autowired private CustomerService service; public Application(CustomerService service) { this.service = service; } public Application() { } public void print() { System.out.println(service.findAll().get(0).getFirstName()); } public static void main(String[] args) { //ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //CustomerService service = context.getBean("customerService", CustomerService.class); //System.out.println(service.findAll().get(0).getFirstName()); ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Application application = new Application(); application.print(); } } This is my application context
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.pluralsight"></context:component-scan> </beans> My CustomerService class
package com.pluralsight.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.pluralsight.model.Customer; import com.pluralsight.repository.CustomerRepository; @Service("customerService") public class CustomerServiceImpl implements CustomerService { //@Autowired private CustomerRepository customerRepository; @Autowired public CustomerServiceImpl(CustomerRepository customerRepository) { System.out.println("we are using constructor injection"); this.customerRepository = customerRepository; } //@Autowired public void setCustomerRepository(CustomerRepository customerRepository) { System.out.println("we are using setter injection"); this.customerRepository = customerRepository; } /* (non-Javadoc) * @see com.pluralsight.service.CustomerService#findAll() */ public List<Customer> findAll() { return customerRepository.findAll(); } }