Results 1 to 6 of 6

Thread: Inject spring bean from another bean in jar file

  1. #1
    Join Date
    Sep 2012
    Posts
    4

    Angry Inject spring bean from another bean in jar file

    Can we generate classes with Spring Bean annotation as jar file?
    How to inject another Spring Bean from the class of that jar?
    Can you provide the possible way? When I get as below it does not work?

    In Jar File

    package org.java.support;

    @Service("CommonService")
    public class CommonService {
    }

    In project

    package com.java.test.app;

    @Service(value = "OtherService")
    public class OtherService {
    @Resource(name = "CommonService")
    private CommonService service;
    }

    In configuraiton

    <context:component-scan base-package="com.java.test.app, org.java.support">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Reposit ory"/>
    </context:component-scan>

    I get the following error;

    13:32:12,331 DEBUG [org.springframework.context.annotation.AnnotationC onfigApplicationContext] Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [or
    g.springframework.context.support.DefaultLifecycle Processor@32dcb03b]
    13:32:12,331 DEBUG [org.springframework.beans.factory.support.DefaultL istableBeanFactory] Returning cached instance of singleton bean 'lifecycleProcessor'
    Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'CommonService' is defined
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanDefinition(DefaultListab leBeanFactory.java:529)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getMergedLocalBeanDefinition(AbstractB eanFactory.java:1095)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:277 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.context.support.AbstractApplic ationContext.getBean(AbstractApplicationContext.ja va:1093)
    at org.ace.java.support.delete.Test.main(Test.java:18 )
    Last edited by javamutu; Sep 26th, 2012 at 02:24 AM.

  2. #2

    Default

    The "name" attribute is intended to be used as the JNDI name of the resource.
    Quote Originally Posted by javamutu View Post
    @Resource(name = "CommonService")
    private CommonService service;
    }
    What you need is to use the @Qualifier annotation to indicate the bean name you want to be autowired.

  3. #3
    Join Date
    Sep 2012
    Posts
    4

    Default

    Although I used @Resource annotation, I get the same problem.

  4. #4

    Default

    Quote Originally Posted by javamutu View Post
    Although I used @Resource annotation, I get the same problem.
    I'm not sure you're following me. What I say is to rewrite your autowiring stuff to the next:
    Code:
    @Qualifier("whatever-your-bean-name-is")
    @Resource
    private ...

  5. #5
    Join Date
    Sep 2012
    Posts
    4

    Default

    Here my error is.

    Code:
    @Service(value = "OtherService")
    public class OtherService {
        @Qualifier("CommonService")
        @Resource    
        private CommonService commonService;
    }

    Code:
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'OtherService': Injection of resource dependencies failed; nested exc
    eption is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.java.support.CommonService] found for dependency: expected at 
    least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=CommonService), @javax
    .annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}
    	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    	at org.ace.java.support.delete.Test.main(Test.java:12)
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.java.support.CommonService] found for dependency: expected at
     least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=CommonService), @java
    x.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
    	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:438)
    	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
    	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:549)
    	at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
    	... 13 more
    Last edited by javamutu; Sep 26th, 2012 at 11:41 AM.

  6. #6

    Default

    I'm quite sure the problem is related to how you're creating the JAR file.
    I guess you're doing it with Eclipse (export...), right?
    If so, then make sure you select the option "Add directory entries" option when exporting the JAR file.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •