onaled777
asked on
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:
This is the Test class
In the xml file applicationContext-service s.xml I have the bean configured:
In addition the annotation-config is in the same file as well as the base package is configured.
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)
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");
}
}
In the xml file applicationContext-service
<bean id="lookupService" class="com.Chelsea.web.service.LookupService">
</bean>
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" />
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you!
You're welcome!!
well spotted @mccarl !
Thanks!!
Is this a maven project where the spring xml config files reside in src/main/resources folder?