Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

How to import beans annotated with @Bean from other project

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a situation where I need to import beans annotated with @Bean from one project to another project. Is there any way to achieve this.

For example I have project A and Project B, I want to use a bean of project A in project B so that I don't want to write the same code again. All these DAO beans are annotated with @Bean
 
author & internet detective
Posts: 40521
825
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your XML, you can specify that you want that component scanned by specifying the package name:

 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Raaja, you will have to provide more details. Are you using Spring Javabased Configuration?

If one project has a configuration like this


And you want to include this bean in your own project, you can do this




AConfig will have to be on classpath of project B.
 
Raaja Gotluru
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Jeanne and Javesh.

Project A is having java configuration and project B is xml configured. As suggested by Jeanne I used <context:component-scan base-package="com.coderanch.myPackage" /> and imported project A's spring-context xml. But I dont have any idea what is happening when I run the test it is throwing following error as it is not able to find the bean.



project A config file



And individually project A is running

Test class

 
Raaja Gotluru
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am able to access the service from project A in project B after adding the following line



along with



ref: http://stackoverflow.com/questions/8391944/getting-spring-error-bean-named-x-must-be-of-type-y-but-was-actually-of-ty

Thanks for the help Jeanne and Javesh.
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic