Get a reference of current proxy in Spring boot - java

Anyone know how to get a reference of the current proxy in a class with #Service annotation in Spring boot?
AopContext.currentProxy() doesn't work, it says that need to set exposeProxy to true, but I've no idea how to do it with Spring boot.

There was a bug related but it was resolved on Spring 4.3.1.
Now you can set #EnableAspectJAutoProxy(exposeProxy=true)

Related

Is it possible to achieve session scope behavior in Spring WebFlux?

Currently experimenting with reactive programming using Spring WebFlux.
I need to reset some values(create new instance of one class) every session.
It is not possible to inject a #RequestScope or #SessionScope bean as it will end up with IllegalStateExcepion error.
Is it possible to achieve session scope behavior in Spring Webflux? Any tips what can I use to achieve that?
Spring Session
In order to use sessions in Spring Webflux, you can use Spring Session, more specifically you can use the integration with Spring Webflux's WebSession
Some tutorial can be found here: https://www.baeldung.com/spring-session-reactive
I have resolved this by using hazelcast session replication.

Can you use scoped proxies with Spring Boot 2 #ConfigurationProperties?

Using Spring Boot 1.5.12, we create scoped proxies for #ConfigurationProperties beans. We do this so that we can effectively have property values that are scoped to the user/session/etc. We use a BeanFactoryPostProcessor to register a scoped proxy created by ScopedProxyUtils.createScopedProxy and change the scope of the target bean definition to our custom scope.
This worked great in Spring Boot 1.5.12. However, in Spring Boot 2, the introduction of the Binder API has made this stop working. This is because when Spring Boot binds #ConfigurationProperties in its ConfigurationPropertiesBindingPostProcessor, it uses Bindable.of(type).withExistingValue(bean) passing in a type (e.g. org.example.Foo) and the bean which is an instance of ScopedProxyFactoryBean. Bindable checks that bean is an instance of type which it's not and things blow up.
Does anyone know of a general way to accomplish this? Specifically, to replace the #ConfigurationProperties bean definition with a proxy while still allowing the properties to bind to the instance? I've considered delaying the creation of the proxy until after the target has already been bound by ConfigurationPropertiesBindingPostProcessor (i.e. in my own BeanPostProcessor). However, at this point the bean is instantiated and my proxy can only replace the bean. It can't really leave the original bean in the context as a target. So I'm struggling with how to do this using a BeanPostProcessor.
EDIT: I've created a simple example project that demonstrates the issue (with the ability to check out code that works on Spring Boot 1 and fails on Spring Boot 2).

Disable Redis AutoConfig in spring boot when testing

I am trying to disable Redis when I am testing with spring boot. I have disabled my configuration but the auto config created a default connection and fails because it can't connect to a non-existent service. For testing I am content to just use a basic in-memory cache or a no-op cache. That doesn't work either. Here is what I have tried:
per this issue I added said configuration to my test app properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
But. That gets me a bit further. But ultimately I get a NoSuchBeanDefinitionException redisTemplate - this is because redisReferenceResolver is trying to look that up.
Looking at my debugger right now, the bean it's trying to hydrate is:
org.springframework.data.redis.core.convert.ReferenceResolverImpl which is coming from spring-data-redis:1.8.0.RELEASE which is coming from this dependency: compile('org.springframework.boot:spring-boot-starter-data-redis') . I admit, the bean name is a bit misleading. The type it actually resolves to is not
The only other reference to redis is in our hibernate support.
Can someone explain how to turn this off for testing?
Try excluding this two auto-configuration classes in your test properties file:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration
or
exclude
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
and set: spring.data.redis.repositories.enabled=false
With YAML syntax (& Spring Boot):
spring.autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
- org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration
If you have SystemEnvironmentPropertySource in you app context you can use environment variable SPRING_AUTOCONFIGURE_EXCLUDE separating items with comma:
SPRING_AUTOCONFIGURE_EXCLUDE=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration
Also try #EnableAutoConfiguration(exclude = {...}) on a #TestConfiguration annotated class.
If you dont want to change any files/code, you can also do this with an environment variable:
SPRING_AUTOCONFIGURE_EXCLUDE=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration

How to set responseSkew property value in WebSSOProfileConsumerImpl in Spring boot

I am trying to integrate SAML OKTA in a spring boot application. I need to use the the following bean setup in spring boot:
Can any one please help me setting the responseSkew property in webSSOprofileConsumer bean in Spring boot.I just need an equivalent spring injection technique of the xml configuration that I mentioned above in annotation based spring boot injection.
I have already gone through the link :
http://docs.spring.io/spring-security-saml/docs/current/reference/html/configuration-advanced.html
There it is mentioned about setting responseSkew but it is not mentioned how to do it in java based annotated configuration in a spring boot application.
Thanks in advance.
If you're using a child class of AbstractProfileBase (e.g. WebSSOProfileConsumerImpl) you simply can use its setter:
setResponseSkew(int responseSkew)
For xml configuration of spring boot I dunno how to achieve this because I never got used to it.

How to automatically test that Ehcache is used in Spring Boot project

I started using Ehcache in my Spring Boot project. How can I prove that the Ehcache is being used instead of the default ConcurrentHashMap, which is provided by Spring Boot by default? How can I prove it automatically in the integration tests?
If I understand the question correctly, you are trying to validate your spring configuration.
A one time thing regarding Ehcache is to check logs - you should see the information about it being started and configured.
For automated testing, the easiest way is going to be to have the test configured to be injected with the cacheManager bean and then making sure it is of the right type.
This should give you the confidence that your setup is correct.

Categories

Resources