Announcement Announcement Module
Collapse
No announcement yet.
Multiple components/application contexts Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple components/application contexts

    I am trying to set up a Spring integration test where I reference a @Service bean defined in a different jar, and I am getting the following error:

    Code:
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.company.service.userid.IService com.company.service.userid.ExampleConfigurationTests.loggingServicesExampleService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.company.service.user.IService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=loggingServicesExampleService)}
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:507)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)
    	... 30 more
    Here's a snippet of code from my integration test:

    Code:
    @ContextConfiguration({"classpath*:/userServices-appContext.xml"})
    @RunWith(SpringJUnit4ClassRunner.class)
    public class ExampleConfigurationTests {
    	
    	@Autowired
    	@Qualifier("loggingServicesExampleService")
    	private IService loggingServicesExampleService;
    Here's the UserServices application context:

    Code:
    <?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:aop="http://www.springframework.org/schema/aop"
    	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/aop 
    		http://www.springframework.org/schema/aop/spring-aop.xsd
    				http://www.springframework.org/schema/context
    		http://www.springframework.org/schema/context/spring-context.xsd">
    
    	<!-- Import the Logging Services component. -->
    	<import resource="classpath*:/META-INF/spring/loggingServices-appContext.xml" />
    
    	<!-- Configure Spring to use scan all classes within the base package and 
    		automatically discover Spring beans and declare them. -->
    	<context:component-scan base-package="com.company" />
    
    	<!-- The following <aop:aspectj-autoproxy /> element creates an AnnotationAwareAspectJAutoProxyCreator 
    		in the Spring context that automatically proxies beans whose methods match 
    		the pointcuts defined with @Pointcut annotations in @Aspect-annotated beans. -->
    	<aop:aspectj-autoproxy />
    
    </beans>
    And here's my LoggingServices application context:

    Code:
    <?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:aop="http://www.springframework.org/schema/aop"
    	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/aop 
    		http://www.springframework.org/schema/aop/spring-aop.xsd
    				http://www.springframework.org/schema/context
    		http://www.springframework.org/schema/context/spring-context.xsd">
    
    	<!-- Configure Spring to use scan all classes within the base package and 
    		automatically discover Spring beans and declare them. -->
    	<context:component-scan base-package="com.company" />
    
    	<!-- The following <aop:aspectj-autoproxy /> element creates an AnnotationAwareAspectJAutoProxyCreator 
    		in the Spring context that automatically proxies beans whose methods match 
    		the pointcuts defined with @Pointcut annotations in @Aspect-annotated beans. -->
    	<!-- <context:spring-configured/>-->	
    	<aop:aspectj-autoproxy />
    
    
    </beans>
    In general, I want to be able to set up multiple projects in Eclipse, each corresponding to a component (e.g., jar, war) that I can manage independently yet include as a dependency for other components. For example, I have a LoggingServices project where I'm setting up a simple AOP exception logger, and I want to be able to have this logger applied as an aspect across multiple other components. Ideally, I want to have LoggingServices available as a jar that has the code and application context configuration bundled so that all I have to do from another component is import the LoggingServices application context to get its functionality.

    I am wanting to set this up using pure annotations so that I don't have to have heavyweight XML configuration files. Can anyone help me who has set up multiple components and application contexts in a similar fashion? In general, how can aspects and other services defined in one jar be referenced from another?

    One other odd thing I noticed: If I misspell the name of the logging services application context (the one I'm trying to import), I don't get any errors at all saying Spring can't find it. So, how do I get Spring to "see" the services and aspects defined in another jar?
Working...
X