Override #Component from dependency - spring-boot

Let's say I have a Base project that I'll use as dependency in other projects. In this Base project I have a component that I want to make customizable. To simplify let's call it Tools. The Base project is making use of Tools in different classes.
Is it possible to override the methods from Tools component in a new project and make Base use the overrided methods?
Sorry if this is a basic question but I didn't find anything about this.

It's been some time, but i would like to share a really simple way to do it. I could not believe how simple it is.
Just extend the component you want to override, and add #Primary in it.
#Component
public class Parent {
#Primary
#Component
public class Child extends Parent {
Now, your app will use Child instead of Parent where Parent was #Autowired

If you scanned the original package for original component bean, stop to scan the package and extend it yourself and scan the package you newly implemented. It's should work. Otherwords, you can define a BeanPostProcessor and replace it to yours. Both should work fine.

Related

Is it possible to override abstract beans in Spring?

I'm trying to override this bean which is provided by standard Hybris (OOTB) framework. I would love to override it so it uses my own custom class. Is this possible?
You can create new extended abstract class but it is not used by existing classes. You can use customize functionality to overwrite class and/or spring config. Bu you must careful during patching or updating system.
How to override hybris platform files other than customize
Do you want to inject a custom implementation for one of the properties mapped to the abstract bean definition or do you want the abstract bean to use a custom class that you provide?
The latter just doesnt work, because you would also break all configurations of implementing beans.
You should seek for all beans that use this abstract bean as a parent and think, which of those you want to change and change that. Most likely, those beans will have an alias defined that you can use to change it. :-)
Best Bechte

How to add rest controller to spring boot library, and use it in another application?

I am working on a library xyz.jar that needs to add a UI page with mappings like this one:
#RestController
public class LibCtrl {
#EventListener(ApplicationReadyEvent.class)
#RequestMapping("/updateDomainList")
String updateDomainList() {
return "we can call a controller from another jar like this";
}
}
This then needs to be called in my main springboot application, myMainApplication.war, so when I call
http://localhost/myMainApplication/updateDomainList
I should see
we can call controller from another jar like this
on the browser.
How can achieve this? #Component also did not work for me. Once it begins to work, would #Autowired to JdbcTemplate also work?
It was a simple fix. #ComponentScan allows for multiple packages to be scanned. This made is possible for me to add my Library Packages to be managed by Spring. Just add the following to your application class.
#ComponentScan({"my.mainapplication.package","my.library.package"})

spring data - get beans in a custom default repository implementation

Im trying to upgrade my project from spring-boot-1.4.3.RELEASE to spring-boot-1.5.9.RELEASE.
In the 1.4.3.RELEASE the way I have used my custom implementation of repositories is as follow:
Made an interface MyCustomRepositroy that extends JpaRepository
Had a class MyCustomRepositoryImpl that implements MyCustomRepositroy and SimpleJpaRepository. In that class I have changed the behavior of
the of save, find and delete methods, because I needed a
ceratin behavior for entitys of certain type (lets say that I need a
custom save for all entitys that implements Special interface)
I made a MyCustomJpaRepositoryFactoryBean that extends JpaRepositoryFactoryBean. In that factory, I've overridden createRepositoryFactory and gave it my MyRepositoryFactory implementation.
In MyRepositoryFactory implementation I've overridden the getTargetRepository, and getRepositoryBaseClass.In these methods, I check if the entity is of type Special, and if so, I return MyCustomRepositoryImpl, otherwise I return SimpleJpaRepository.
Also, I can get the beanFactory in my MyCustomRepositoryImpl class because I call my own constructor of MyCustomRepositoryImpl that has also a beanFactory parameter via getTargetRepositoryViaReflection.
Now, with the new version (that uses spring-data-commons-1.13.9.RELEASE), I cannot override the Factory class, and hence can't decide for each entitiy which implementation to give, and have no way to get the beanFactory.
Is there any way I can get what I want?
Sorry for the mess, but I cannot post my code here.
P.S - my project is a spring based library, so I can't do anything to the entitys because my clients declare them, all I know that some entitys implement the Special interface and some don't
I am trying to figure out what happened in 1.5.9.RELEASE, but in the meantime, just as a fyi - 1.5.6.RELEASE works ok.
I am having the same issue when trying to update from 1.5.6.RELEASE to 1.5.9.RELEASE

where to place componentscan

I have spring mvc application with complete annotations. Where is the best place to add #componentScan? Let me know any of these recommended
class that extends AbstractAnnotationConfigDispatcherServletInitializer?
class that extend WebMvcConfigurationSupport
class that extend WebSecurityConfigurerAdapter
I placed in 2 without security and workign fine. When I added security, I got problem with security config not able to find userdetails service. Then I moved to 3.
I found other issues with security and put code to just reuturn null instead of
securityconfig object from getRootConfigClasses(). Then I got issues of controllers not found. I am able to fix it to put componenentscan in 2.
I just want to know any links and how it works. Is it ok to put #componentscan in all of these 3? Appreciate your help.
It depends on your project's package tree and what you want to scan. If you want to scan all annoted classes with such annotations like : #Configuration, #Component, #Repository, etc... put #ComponentScan at the top of your package tree.
You can also use the basePackages attribute to specify where to start the scanning.
Say you have an application packages organized like this :
com.app.config,com.app.config.web, com.app.services, com.app.web.controllers
If you want to scan all annoted classes, put the class annoted with #ComponentScan in com.app package.
If you want to scan only controller, add #ComponentScan(basePackages="com.app.web.controllers")
It's up to you to decide.

How to use Rest service class as a library class

I have a Spring app and I created a Rest service with a couple of GET methods.
Now that web service needs to be moved to a library module to be used by other modules. How can I do that? Where to place it in the project I want to use it? Extend it in the project? I don't have anything to add to it or override (it is a kind of utility web service). I just need its name as a component. Would I just have some child class that is just extending it with empty body and give it a component name? Is that ok?
import xxx.yyy.lib.UtilityWebService;
#Component("utilityWS")
public class MyWebService extends UtilityWebService{
}
What would be the best practice for this situation?

Resources