Spring Boot GemFire Pivotal Cloud Cache #EnableClusterAware and ClientCacheRegionFactory - spring-boot

Im trying to connect to Pivotal Cloud Cache server Regions.
I'm using #EnableClusterAware and trying to configure the client Regions via ClientRegionFactoryBean as below:
#Bean("clientRegion")
ClientRegionFactoryBean someClientRegion(GemFireCache gemfire) {
// ...
}
Either GemFireCache or ClientCache beans are not available with the annotation #EnableClusterAware. It's available only with #ClientCacheApplication.
Is there any annotations that needs to be used in conjunction with #EnableClusterAware so that GemFireCache gets injected?
Please help.

When Spring Boot for Apache Geode, or alternatively GemFire, (SBDG) is on the classpath of your Spring Boot application (see here), then SBDG auto-configuration will automatically create and configure a ClientCache instance for you (see here).
TIP: You can also review the Getting Stated Sample Guide and Source to see this behavior in action, for yourself. The Guide also talks about the use of the #EnableClusterAware annotation, here.
The #EnableClusterAware annotation is simply a development-time, SBDG annotation that enables you to switch between environments (e.g. local vs. managed, such as when pushing your Spring Boot application up to run in PCF, connected to PCC, and then testing locally in your IDE) without having to change any configuration (hence the goals).
The #EnableClusterAware annotation does not create any GemFire/Geode cache instances for you. Only SBDG's auto-configuration, or declaring an explicit SDG annotation (e.g. #ClientCacheApplication) will do that for you. Still, when using SBDG auto-configuration, you do not need an explicit SDG cache application annotation, like #ClientCacheApplication, since SBDG auto-configuration (again) creates and configures a ClientCache instance by default.
TIP: See the documentation on the #EnableClusterAware annotation for more details.
If ClientCache is not provided (auto-configured), then you are not:
Using SBDG, or rather do not have SBDG on the Spring Boot application classpath (see here)
Have not bootstrapped or configured and ran your Spring application with Spring Boot
Your Spring Boot application configuration is incorrect
You have explicitly disabled or overrode the SBDG auto-configuration
Etc.
One or more of these have to be true.

Related

What's the difference between Spring Boot's #Configuration and #AutoConfiguration?

And most important, what's the reason behind the "Auto" prefix? Classes annotated with #Configuration rather than #AutoConfiguration are less automatic or something?
#Configuration is a spring framework annotation and not strictly bound to spring-boot. It was introduced when spring started to allow programmatic creation of spring-beans as to move forward from xml definitions of beans.
#AutoConfiguration is a spring-boot specific annotation not commonly available in spring framework. The reason it exists, is for external providers that cooperate with spring-boot to be able to mark some classes in some libraries they provide with this annotation as to inform spring-boot that those classes could be parsed and make some initializations during start up of spring application automatically.
So if some regular programmer that develops some application happens to have kafka in dependencies then some beans will automatically be created and added in application context and will be ready for the programmer to use, although he has not defined any configuration for them. Spring-boot already knows this as the kafka provider has already informed by marking some class in the jar they provide with the annotation #AutoConfiguration.
For this reason #AutoConfiguration has some more powerful configurations available as before, after, beforeName, afterName as to allow the provider to specify when the configuration is applied during application startup if some order is necessary.
So this annotation is not to be used from some regular programmer that develops an application using spring-boot. It is for someone that develops a library that other users might use with spring-boot. One such example is kafka library.
For this to work in a spring-boot project #EnableAutoConfiguration is needed as well, to enable auto configuration.
From spring documentation
Spring Boot auto-configuration attempts to automatically configure
your Spring application based on the jar dependencies that you have
added. For example, if HSQLDB is on your classpath, and you have not
manually configured any database connection beans, then Spring Boot
auto-configures an in-memory database.
#Configuration instead is to be used from some regular programmer that develops an application using spring-boot or spring-framework as to inform the framework for which beans should be created and how.
#AutoConfiguration was introduced in 2.7 with the idea to mark all auto-configurations with its dedicated annotation and move away from spring.factories for auto-configuration imports in 3.0 as described in Github issue.
According to Spring documentation:
[#AutoConfiguration] indicates that a class provides configuration that can be
automatically applied by Spring Boot. Auto-configuration classes are
regular #Configuration with the exception that
Configuration#proxyBeanMethods() proxyBeanMethods is always false.
Usually, #AutoConfiguration classes automatically configure an application based on the dependencies that are present on the classpath. Those classes are generally marked as #ConditionalOnClass and #ConditionalOnMissingBean annotations that detect the presence or absence of specific classes.
Additionally, if a configuration needs to be applied in a specific order, you can use the before, beforeName, after, and afterName attributes on the #AutoConfiguration, unlike #Configuration which doesn't provide those attributes.

what does "spring-kafka without springboot" mean

I'm totally new to Kafka and terribly confused by this:
https://docs.spring.io/spring-kafka/reference/html/#with-java-configuration-no-spring-boot
I don't understand what that even means. What does "no spring boot mean" because that example sure as hell uses spring boot. I'm so confused....
EDIT
if I'm using SpringBoot and spring-kafka, should I have to manually create #Bean ConcurrentKafkaListenerContainerFactory as shown here. Most of the examples in the docs for setting up filtering / config / etc seem to use the "manual" configuration using #Bean. Is that "normal"? The docs are very confusing to me...especially this warning:
Spring for Apache Kafka is designed to be used in a Spring Application Context. For example, if you create the listener container yourself outside of a Spring context, not all functions will work unless you satisfy all of the …​Aware interfaces that the container implements.
It's referring to the autowired configuration, as compared to putting each property in the config via HashMap/Properties in-code.
Also, it does not use #SpringBootApplication or SpringApplication.run, it just calls a regular main method using a hard-coded Config class.
Spring boot contains the functionality of AutoConfiguration
What this means is that spring boot when discovers some specific jar dependencies it knows, in the project, it automatically configures them to work on a basic level. This does not exist in simple Spring project where even if you add the dependency you have to also provide the configuration as to how it should work in your application.
This is also happening here with dependencies of Kafka. Therefore the documentation explains what more you have to configure if you don't have spring-boot with auto-configuration to make kafka work in a spring project.
Another question asked in comment is what happens in case you want some complex custom configuration instead of the automatic configuration provided while you are in a spring-boot app.
As documented
Auto-configuration tries to be as intelligent as possible and will
back-away as you define more of your own configuration. You can always
manually exclude() any configuration that you never want to apply (use
excludeName() if you don't have access to them). You can also exclude
them via the spring.autoconfigure.exclude property.
So if you want to have some complex configuration which is not automatically provided by spring-boot through some other mechanism like a spring-boot specific application property, then you can make your own configuration with your custom bean and then either automatic configuration from spring-boot for that class will back of as spring does several intelligent checks during application context set up or you will have to exclude the class from auto configuration manually.
In that case you could probably take as an example reference of how to register manually your complex configurations in spring boot what is documented on how to be done in non spring boot app. doc

Alternate between Spring Boot cache and GemFire

I have a Spring Boot app with caching enabled using the #EnableCaching in the main class and #Cacheable for certain methods. This was earlier working well.
Now I have also configured GemFire cache to store other data.
The older code with the #Cacheable annotation is now trying to fetch this data in the GemFire cache. Is there a way to define when to use which cache?
I'm new to this and would appreciate any inputs on the best practice to be followed in such case and what can be done to overcome this.
Have a look at this: https://docs.spring.io/spring-boot-data-geode-build/current/reference/html5/#geode-caching-provider, and specifically this: https://docs.spring.io/spring-boot-data-geode-build/current/reference/html5/#geode-caching-provider-disable.
If you are not using Spring Boot for Apache Geode (SBDG; see project home as well as the docs) (which also applies to VMware Tanzu GemFire as well, given GemFire is based on OSS, Apache Geode), then you most definitely should consider it.
If you need to control which cache is used for which, e.g. service method, then you should read more about custom cache resolution as opposed to the default cache resolution strategy.
Last, SBDG honors Spring Boot's Caching configuration, after SBDG is essentially Spring Boot specifically designed and developed for Apache Geode (and GemFire).

Can SpringBoot be use for Backend application?

One of the Spring framework advantage is dependency injection. Many had used SpringBoot for providing REST Web Services.
Read up and notice there are Scheduler and CommandLineRunner for SpringBoot, could we using SpringBoot for backend type of application to replace the usual standalone java program while making use of SpringBoot advantage (Dependency Injection)
- Cron Job (Execute and stop running)
- Long Running Process
One of the main thing I am looking into is to use annotation such as Spring Configuration, Spring Data JPA and other technology in backend application.
Of course!
I used spring boot to back CLI projects, DB access projects and more.
Spring boot is very modular. It works by providing auto-configuration based on your maven/gradle imports. If you don't import starter-web/starter-jersey or any other starter that is for the web/rest api, the auto-configuration for this resources won't be triggered and you can basically enjoy all the power of spring boot to support your needs
Definitely,
Spring boot is not a separate framework.It reduces the configuration difficulties when you using spring framework. Spring boot provides a Rapid Application Development using without complex configuration including your dispatcher servlet, XML file for database connectivity and configuration files. You can use spring boot for back-end development. Simply says you can do everything what you does in spring MVC without any complex configuration. If you are using spring boot , You can configure your database details in application.properties file. I am adding one of two links for proper reading,
https://projects.spring.io/spring-boot/ ,
https://dzone.com/articles/why-springboot

How to use a Spring Boot enabled library inside a old-school spring server

tl/dr (How) Is it possible to use a jar library, that uses Spring Boot for configuration in a non Spring Boot (regular old-school Spring) server.
We have the problem, that we have a Spring server, that is from the pre-Boot times and we want to create a new library for it. We would like to use Spring Boot in the library (i.e. #EnableAutoConfiguration for configuration). To include the library we have put an spring.xml into the library that enables component-scan inside the library. The classes inside the library use #EnableAutoConfiguration and #EnableWebSecurity to enable configuration and security.
If we include now the library into our server and import the XML file from the library into the server's XML file, only parts of the configuration are working. The #RequestMappings in the library are working and the interfaces are available. However Spring security fails to register it's default filter chain, leading to ugly errors, where the regular Spring Boot config should already work with AnonymousAuthorizationFilter, etc.
We debugged, that the FilterRegistrationBean in spring security is never configured when running that way (and is, if we are running as a Spring Boot application).
So is there a common way how to deal with Spring Boot enabled libraries inside old-school Spring servers?
Is placing a single XML to enable component-scan in the library and importing this XML inside the main server's XML the correct way to include Spring Boot libraries (and how would be the best way, if the server would use Spring Boot itself)?
Does anyone know of the issue with a missing Spring Security filter chain?
PS: I know that we can add the required filters manually. However if we would do that, we would anyway get rid of Spring Boot completely in the library, so this question mainly aims for how to do it with Spring Boot. Of course if it is the wrong way to enable Spring Boot inside a library, please also mention that :-)

Resources