How to use Rest service class as a library class - spring

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?

Related

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

Override #Component from dependency

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.

Spring in a JavaFX application - how to property handle controller as dependency?

I have a JavaFX application that uses spring boot, exactly as described in this blog post:
http://www.greggbolinger.com/let-spring-be-your-javafx-controller-factory/
I am using the FXML loader overriding the controller factory to use spring.
The problem is that Spring loads the controller class marked as #Component on application start or later if marked with #Lazy, but keeps the bean in memory.
If I open a Stage, modify the data, close the stage and open it again, the data is still there (because the controller was kept by spring). It also gets in the way if I open two of the same Stage (window). It shares the same controller, so if I modify one, the other modifies too, and this is not the desired behavior.
How to I properly handle JavaFX controllers with spring?
Thanks!
Mark the controller as having prototype scope, so that a new instance is created on each request:
#Component
#Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class Controller {
// ...
}

How to read property from OSGI configuration in AEM 6.1 in model class which extends WCMUse class

I am extending my model class to WCMUse class to write my business logic. I have osgi configuration which has couple of properties. I want to read one of the property in my model class. I am not sure how to get the handle of osgi configuration in WCMUse class. Any pointers will be highly appreciated.
Adding the answer for future references.
You can use the #getSlingScriptHelper() method of the WCMUse class to get an handle of the SlingScriptHelper. This is the same sling object that is available through inclusion of global.jsp in traditional JSP scripts.
You can then call #getService() method of the SlingScriptHelper to look up the desired OSGi Service.
The following code snippet can be used in the model to get the service configuration
getSlingScriptHelper().getService(<<Configuration Service>>.class)

Resources