Can I extend an @Component and create another @Component class and make only one available at a time?

jishamenon :

I have a library jar which I want to provide to a number of applications. The behavior I want is to create a common spring component class in the library. If in the applications, the same component is not extended then use the common component; if it's extended in the app, then use the extended component (child class). Is this possible? - Create CommonComponent only if a child for that class doesn't exist.

I am using Java 1.8, Springboot2.0

Created class in library:

@Component
public class CommonComponent{}

In one of the child apps using the library, I added a child component:

@Component
public class ChildComponent extends CommonComponent{}

I expected one component ChildComponent created; but in the above scenario 2 components - CommonComponent and ChildComponent are created.

Todd :

One way you could do this is to take advantage of the @ConditionalOnMissingBean annotation that Spring Boot has. When combined with a bean definition in a @Configuration class, we can tell Spring to only define our bean if it doesn't already have one.

This is untested:

@Configuration
public class CustomComponentConfiguration {

    @ConditionalOnMissingBean(CustomComponent.class)
    @Bean
    public CustomComponent customComponent() {
        return new CustomComponent();
    }
}

In this example, when our @Configuration runs, Spring determines if there is any other bean that is a CustomComponent. If not, it executes the customComponent() method and defines whatever bean you return. So if somebody else defines a ChildComponent, this method will not get called.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How I can one component to another component in react

Can I create a constructor for a @Component class in Spring

How can I make a value modified in a child component available to the parent?

How can I make a list defined in one method available in another method when both methods are in the same class?

How to create and extend a abstract class component in VueJS?

How i can create Middleware on October CMS only on Component Class or Plugin?

How do I make an Angular directive usable only by one component?

How can i make only one choice available from 3?

How can I extend a "connected" component?

How can I extend a built in component in Angular?

How can i make a method available only from within the class

How can I make a button link to another component?

How can I pass props from one component to another in typescript?

Can I use the component of One Application into another Application?

How can I pass an *ngIf value from one component to another?

How can I emit from component and listen from another one?

How can I call a function from one component to another? React

how can i share function of one sibling to another sibling component

How can I use Redux to only update one instance of a component?

Can i link a css to only one react component?

Can I create a component as an attribute?

How can I replace one loaded component with another component in cakephp 3.x?

How can I pass value from one component from another component in Angular8 (Independent components)

How can I refresh a list in one component after form submit in another component that shares same parent?

Angular2 How can i Use One Component function In Another Component

How can i pass data of clicked row from one component to another component without passing to the url

How can I update a hook from one component and have it update another component as a result?

How can I re-render one component from another component in React?

NSDate with date component from one and time component from another NSDate