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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
onaled777Author Commented:
Thank you!
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
You're welcome!!
0
gurpsbassiCommented:
well spotted @mccarl !
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Thanks!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.