Java Spring autowire configuration error

I need to autowire a bean in Spring and I am having some difficulty.

I believe everything as described here was followed:
http://www.mkyong.com/spring/spring-auto-wiring-beans-with-autowired-annotation/
but I keep getting this error in the Java Concole:

SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@35de4376] to prepare test instance [com.Chelsea.web.controller.PatOrdersControllerTest@6dabbec4]
java.lang.IllegalStateException: Failed to load ApplicationContext
	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)

Open in new window


This is the Test class
//Test class
package com.Chelsea.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.ContextConfiguration;





import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import com.Chelsea.web.controller.helper.RandomString;
import com.chelsea.service.LookupService;


import static org.junit.Assert.*;
import static org.springframework.test.web.ModelAndViewAssert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
	    "classpath:spring-security.xml",
	    "classpath:applicationContext-mvc.xml",
	    "classpath:applicationContext-services.xml" })
public class TestControllerTest {

	@Autowired
    private LookupService lookupService;

	
	@Test
	public void testLookupService() throws Exception {
		logger.debug("Before : testLookupService");
		lookupService.getAllCompanies();
		logger.debug("After : testLookupService");
	}

}

Open in new window


In the xml file applicationContext-services.xml I have the bean configured:
	<bean id="lookupService" class="com.Chelsea.web.service.LookupService">
	</bean>

Open in new window


In addition the annotation-config is in the same file as well as the base package is configured.
 	<context:annotation-config />
	<context:component-scan base-package="com.Chelsea.service" />

Open in new window

onaled777Asked:
Who is Participating?
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Ok, so from what you've posted, I think the problem is here...


TestControllerTest.java (line 38, based on the above posted snippet)
import com.chelsea.service.LookupService;


... but ...


applicationContext-services.xml
<bean id="lookupService" class="com.Chelsea.web.service.LookupService">
</bean>



It appears that you have 2 different LookupService classes, you are asking Spring to create one version in the xml and then asking for a different version to be AutoWired and hence it can't find it and throws the error.
0
 
gurpsbassiCommented:
What is the folder structure of your project?
Is this a maven project where the spring xml config files reside in src/main/resources folder?
0
 
onaled777Author Commented:
Thank you!
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You're welcome!!
0
 
gurpsbassiCommented:
well spotted @mccarl !
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Thanks!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.