I have some beans which contain methods which are annotated with both #RolesAllowed and #Transactional. I have one Spring config file which utilizes a BeanNameAutoProxyCreator for Security related beans and another Spring config file which utilizes a BeanNameAutoProxyCreator for Transactional related beans.
The problem is that some beans contain both security as well as transactional-related beans. So Spring creates proxies for one set of beans. It then tries to create proxies for the other set of beans. When it does, it tries to create proxies of the proxies and bombs out.
Has anyone tried to configure both security and transactionality in the same beans via Spring? What's the trick?
Thanks.
I've never tried it, buy I would look to use one BeanNameAutoProxyCreator that works on both annotation? This BeanNameAutoProxyCreator can create a proxy that delegates to the security and transactional proxies.
Related
Everyone says that it's because spring boot in that case will know that service exists. But if you call that service's method in another method that you try to run when the web app is running. Should it know the service exists without the annotation or not?
The main reason you put these stereotype annotations to classes is creating beans in application context and let the IOC container to provide the management and configuration of these beans depending on their stereotypes.
Spring is an IOC container responsible for instantiating, configuring and assembling these beans. And putting stereotype annotation is just a way to define bean.
You can define beans in various ways such as using #Bean annotation, stereotype annotations, XML definitions etc. and if you don't define your bean, the IOC container can not detect and instantiate the service.
We are externalizing configuration of our microservices (spring boot based) using spring cloud.
As per my understanding on Spring Cloud, to enable the beans loading refreshed/updated values from Config server we need to do 2 things in Spring Cloud Client:
add #RefreshScope on the beans reading values from property files
using #Value
add spring actuator to provide /refresh endpoint to
refresh the context.
Scenario:
We have 100s of classes reading values from property file using #Value.
I have to mark all these beans refresh enabled using #RefreshScope annotation.
How can I avoid putting #RefreshScope annotation on all these classes.
Is there any shortcut or spring cloud feature to get around this situation.
You may want to look into Spring Boot feature called #ConfigurationProperties. It is designed to better organize several external configuration options.
According this Github issue, it should work for spring-cloud without #RefreshScope usage.
EDIT (reaction on comment): Maybe you are missing point of #ConfigurationProperties. With this annotation, you wouldn't use it in other configuration classes. You would have dedicated class (or few classes) only for reading and providing properties. Other configuration classes would inject this configuration holder bean.
You could encapsulate your #Values into one (or several) ConfigurationService bean which is #RefreshScoped and autowire this service into your classes instead. That way you only have a small amount of request scoped beans and your services can stay singletons.
I have spring session scoped bean. That bean is using application scoped beans, and there's and error when the container tries to serialize the session.
I remember that problem from JSF applications, and in one project, we've solved that using serializable proxies. The injected bean was the proxy wrapper which fetched the singleton instance from the application context. Unfortunately, I can't remember how exactly it was called and was it a build-in solution or some external thing.
How to wrap spring beans in serializable proxies? Does Spring provide such functionality? AFAIR there was some special annotation to mark the references that should be wrapped in such proxy.
The feature is called scoped proxy and can be triggered via annotation to the service, that will be injected to session scoped beans:
#Scope(proxyMode = ScopedProxyMode.INTERFACES)
More details in question:
Spring session-scoped beans (controllers) and references to services, in terms of serialization
Is it possible to inject Spring beans into an RestEasy #Path class? I managed to do it with Jersey, with #InjectParam annotation, but for some other reasons, I need to switch to RestEasy, and I can't seem to find a way to do it (tried good ol' javax.inject.Inject, but nothing).
EDIT
This solution works:
http://www.mkyong.com/webservices/jax-rs/resteasy-spring-integration-example/
but it's not injection.. I'd still prefer something a little more elegant.
Simply annotate your RestEasy class with Spring's #Component and then inject your beans using Spring's #Autowired. Don't forget to include the annotation-config and component-scan elements in your spring configuration.
There is a working example that integrates RestEasy with Spring just try spring-resteasy.
You could use the #Configurable annotation to make a normal class (created by new) a spring Bean.
Then you can use the normal Spring annotation to inject everything in that class/instance like in a "normal" Spring Bean.
But that requires AspectJ!
#See Spring Reference Chapter 7.8.1 Using AspectJ to dependency inject domain objects with Spring
I totally agree with Peter's answer but there is another way to do it: you make all your exposition beans (RESTEasy or JAX-WS, which are not Spring components) extending the SpringBeanAutowiringSupport.
That way you can easily inject your Spring Services by #Autowired annotation in these classes.
I've noticed recently that spring can wire up my ejb's for me if I annotate the ejb with #Interceptors(SpringBeanAutowiringInterceptor.class). I've never actually done this so don't know the details.
I was wondering, is there a way to get this to work with other kinds of beans, for example, #WebService annotated ones as well.
At the moment in my web service classes (because the application server manages them) I have to load the dependencies from the BeanFactory and would thus prefer to have them autowired.
I know I could use the #Configurable annotation but am not particularly keen to have to specify and agent on the VM.
Is this possible?
Once again, spring has thought of this use case and catered for it!
The problem is that #WebService is not a spring annotation, it is a JAX-WS annotation and thus classes that are annotated with #WebService to be exposed as web services are not managed by spring, but their life cycle is managed by JAX-WS.
The way to handle this case is to have the JAX-WS managed bean extend org.springframework.web.context.support.SpringBeanAutowiringSupport - this will enable the #Autowire annotation, for example, to work in this bean. see here for more information
Yes, of course. There's #WebService, #Repository, #Controller, #Service, #Endpoint, and other annotations in Spring. Here's an example.