Spring Boot field watcher (observer) or something similar - spring

How to run a method in Spring Boot when the value of concrete database field changed or it reached a certain level (programmatically)? The user wants to choose which field the method will respond to. Is there any common solution or best practice in Spring Boot?

I think what you are looking is reload a bean when the properties which are loading from db is changed. you can use something similar here in this stackoverflow post with ApplicationContextAware in prototype scope.

Related

Null Pointer exception while initializing Object using "new" keyword in springboot based microservcies

What happens when I build an object using new keyword in service using springboot framework?
Does the Object created using "new" keyword is built out of the container?
No, the object created out of a "new" keyword doesn't come out of the container. There are two disadvantages to this approach. The first being that it renders the Spring framework a bit useless. Secondly, if there are #Autowired or Spring managed beans inside your "new" bean, they won't get injected. Once you do a "new", spring leaves all the subsequent hierarchical dependency injection to you.
in Spring, objects that are created using new keyword, created outside of the container. So you wouldn't get benefits like life cycle management, Security etc
You should always use Spring dependency injection to create Beans (using the #Autowired annotation is the recommended way to do this) otherwise you're losing the benefits of using the Spring framework. I would suggest reading this documentation on Beans and Dependency Injection if you haven't already

spring-boot-jpa Initial Entity Persistence

I'm trying to figure out how, in the lifecycle of a Spring Boot application, to conditionally persist a single entity when the application starts up. The normal method of loading initial data via 'import.sql' or similar methods is no good in this use case, because the insert is conditional. On start up, I'd like for my application to check and see if an admin account and the associated admin role exists. If neither exists, I'd like the application to create them. If they do exist, I'd like the application to do nothing and leave the existing entities alone. If I use the import.sql method, I'll either wind up with a duplicate key (username=admin) or I'll overwrite the existing admin account, which may have had it's password changed away from the default password by a previous installation of the application.
I've tried using ApplicationListeners for all the default Spring and Spring Boot events, and I get null pointer exceptions for every one of those listeners because the JPA repositories are not yet initialized. I thought that listeners invoked for the ContextRefreshedEvent were supposed to be called after all beans and such are initialized, but I guess I'm wrong about that. I've also tried to create a bean which has a #PostConstruct annotated method which does this entity creation, however I had the same result with the JPA CrudRepositories not being initialized.
So my base question is; "What is the preferred entry point for doing initialization work with JPA CrudRepositories during Spring Boot application start up?"
ContextRefreshedEvent would normally work (so you may have an initialization ordering problem with your listener). You can also use SmartInitializingSingleton or a SmartLifecycle with autostart=true. In a Spring Boot app you also have CommandLineRunner.

ClassBridge called by MassIndexer and access to spring service

I wonder whether it is possible to use any spring services from within hibernate search ClassBridge.
Abstract:
My entity does not have all information i'd like to put into indexed document. I can not get it by #IndexedEmbeded too. This data comes from external data source, and I have service to provide this. This is only needed when reindexing. When indexing single object save service provide this information in transient entity field.
For reindex I use MassIndexer.
My application is working in Spring MVC environment. I use annotation driven configuration. So to access my service I need only use #Autowired annotation and service is ready to be used.
Solution?
When using Hibernate Search life is not so easy (or I have no idea how it could be).
To get additional information I decided to use ClassBridge. I also implement simple MassIndexer procedure called from within my spring service.
In my ClassBridge spring does not autowire service. When I try do quick workaround and use static field in ClassBridge and pass service reference from MassIndexer caller the other problem occurred. Service reports exception "no session" (sessionFactory.getCurrentSession() throws exception)... I have no idea where to go further.
What is recommended way to access Spring service from within ClassBridge code?
How can I get active hibernate Session (or SessionFactory) from within ClassBridge?
Thanx for you time & hope your help.
The recommended way is through compile time weaving and #Configurable
A comment on this page (http://guylabs.ch/2014/02/22/autowiring-pring-beans-in-hibernate-jpa-entity-listeners/) provides a much simpler way of handling this that might be useful. You can have Spring autowire an object for you, more explicitly by calling:
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
Doc: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/context/support/SpringBeanAutowiringSupport.html

How to load cache the data present in a table during server start up

I am developing a java web application in which I want to cache all the data present in a table during server start up.
Also if there are any changes in DB values, I wish to refresh the cache (without restarting the server).
I am looking for some material in spring which may help me in achieving that. But I am not able to figure it out.
Please help how can I achieve the same. Also I would like to initialize some beans on server start up.
To start with read the following docs which will get you started.
Refer to Spring document http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/cache.html
Also check this simple tutorial http://viralpatel.net/blogs/cache-support-spring-3-1-m1/
Regarding your bean initialization you can use #PostConstruct annotation on a method of the bean class. Spring will call that method after the bean is constructed.
The application that your planning to build wouldnt be an easy one. In my experience, creating an application like that would require a knowledge of the following:
1. Spring
2. Ehcache
3. JMX
4. Servlet Listeners

Best practise - Struts2 / Spring Integrated application startup process - for adding default lookup values in application context

I am a Struts2 and Spring newbie and looking for some insight. When we load a web application we would typically want to cache some default look up data. e.g. if we wanted to store states or other data that does not change frequently and add it to the application context where we can access it across the application. What is the best way to realize this in a Struts2 application integrated with Spring? I read a bit about annotating with #PostConstruct which means I define my own class/method that would get a handle to the context by calling ServletActionContext.getServletContext() and then use setAttribute to add something. Is that a good way of going about things or is there a better option? Or would simply implementing a ServletContextListener be ideal?
Thanks for any input.
If you want to use the ServletContext, use Spring's ServletContextAware interface and then use an #PostConstruct or afterPropertiesSet method to add items to the servlet context.
This is simpler to use than the listener and integrates seamlessly with Spring, giving you access to properties files declared in Spring and any other beans.

Resources