简体   繁体   中英

Forcing external java (jar project) library beans to be managed by Spring fabric

I have two project the first is a spring boot app and the second one ie java static library project that is not dependent on anything else except java. In the past those two projects were one project however i separated them since they represent two different logical components, and that java library is used in other sub projects as well. Now since the statical library is simply a jar i can not instantiate classes of that jar based on the interface name provided such as we do it in spring for example if my interface was located in path:

edu.university.ServiceLayer.StudentInterface

i would easily do:

Student object = (Student) applicationContext.getBean("StudentInterface");

and that gives me the student object

Now i would like to do the same with the external java library. Since i have never done this, my question is what would be the best way to do it if keeping in mind that i would like to keep that library not dependent on anything else except java.

In my spring boot project in order to do that i needed simply need to annotate the selected bean with the correct artifcat ie @Component, @Repository @Service etc. and those beans are then automatically managed by the spring fabric. Ie I can then seen them by printing the BeanDefinitionNames() of the applicationContext ie

String[] beanNames = applicationContext.getBeanDefinitionNames();

But that trick does not work with external jar. Now what would be the best compromise for this constellation, ie shell i really add spring dependency to my jar java library or is there any magical way i can do it without adding those dependency to my library. Again my target is to allow spring to manage selected beans of the external library ie i would like them to appear under:

applicationContext.getBeanDefinitionNames()

is there any pattern-like way that is used to accomplish this?

many thanks for the ideas.

This doesn't relate to Spring Integration at all.

Plus you have to read more documentations .

Any class available in the CLASSPATH can be instantiated as a bean in the Spring Container, eg

@Bean
public Foo foo() {
    return new Foo();
}

When that Foo is in your jar. No need to modify them for those stereotype annotations.

BTW, to be more clear you even can create beans for Java native classes:

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public Date now() {
    return new Date();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM