HystrixCommand only works with Spring Service or Component? - spring

Does Spring Hystrix only work with #Service and #Component?
I had a class that was defined as a #RestController and my HystrixCommand would not fire, the method would execute but not behave as a HystrixCommand. When I made a #Service class and put the HystrixCommand method and fallback into it the HystrixCommand would work properly.
What are the appropriate Spring annotations that can be used with #EnableHystrix?

For now, you described the appropriate places. We have an open issue that mentions support for controllers.

Related

How #Service class is exposing and intercept methods on top of Repository

My question is in Spring Boot. I have created domain class, CRUD repository class and #Service wired service class. I have written some methods in #Service class like save entity based on some conditions and code is working fine.
My question is, how spring boot invokes particular method when I call REST POST method?

Including Spring #Retryable without main or service class

I have an api that does not have main method in it, It is just set of classes for calling database procedures. None of the classes has Service annotation in them.
I am including this api in another spring boot application.
When I annotate any method as #Retryable in external api and call from Spring boot application it does not provide retry.
Can you please help on this?
Do you have #Configuration annotated class in your external api?
Can you check , whether you enabled retry using #EnableRetry?
This should be declared on any #Configuration class in your external api.
org.springframework.retry.annotation
Annotation Type EnableRetry
Global enabler for #Retryable annotations in Spring beans. If this is
declared on any #Configuration in the context then beans that have
retryable methods will be proxied and the retry handled according to
the metadata in the annotations.

Analog of spring #Controller annotation in Java config

In Spring DI I love to use #Bean with factory method in #Configuration class instead of using #Component annotation explicitly. Is there a way to do the same with #Controller annotation for Spring Web? In another words, could I declare controller via factory method in some #Configuration class instead of explicit annotation?
Thats not possible since #Controller can only be placed on types. But more important are the methods. I assume you have multiple methods with #RequestMethod annotations. You can place as much methods as you like in this controller with differnet paths. Which should end up in around the same thing as you want?

How does SimpleCORSFilter work?

How does SimpleCORSFilter work in this example?
Enabling Cross Origin Requests for a RESTful Web Service.
I only see a declaration of SimpleCORSFilter class but no instance. I tried ctrl+f to search the example page but can't find anywhere this class be instantiated.
How does it work?
I am new to Spring and Java.
So more detail more helpful. Thx.
A main point of Spring is a mechanism called dependency injection. Spring allows you to mark your classes, instance variables and so on with special annotations. Spring will look for those annotations and configure your application according to them.
In your example you annotate your filter with #Component:
#Component
public class SimpleCORSFilter implements Filter
And you annotate your Application class with #SpringBootApplication:
#SpringBootApplication
public class Application
The second annotation (#SpringBootApplication) tells Spring to search through your project for #Component annotations. As you annotated your filter with this, Spring will find your filter and instantiate it automatically. That's how your filter will be created and put to the right place.

Invoking custom annotation using Spring AOP

I searched the web for a clear example on how to invoke my custom method annotation using Spring AOP but couldn't find a clear example.
I am building a framework to inject a user profile in the context when certain methods on any POJO is called.
The framework API should be invoked via a custom method annotation, say #RunAsAdmin. I can build the annotation part and parser, my question is what is the best way to invoke my parser when the annotated method is called.
We are using Spring 3.0 and would like to know what is the best practice to configure Spring framework to understand those methods annotated with specific annotation and invoke my annotation handler (parser).
Any example or guidance will be highly appreciated.
Intercepting a call on any annotated Spring bean method is straightforward, see the example below.
Intercepting a call on a POJO is not possible by definition - you have to return a proxy instead.
That is, you should implement a factory for these POJOs that will return proxies instead of real POJOs. You might use ProxyFactoryBean http://docs.spring.io/autorepo/docs/spring-framework/3.2.0.BUILD-SNAPSHOT/api/org/springframework/aop/framework/ProxyFactoryBean.html for this.
//intercepting a call to any method annotated with #com.yours.RunAsAdmin of any Spring-managed bean
#Component #Aspect class RunAsAdminAspect {
#Autowired
private YourRunAsAdminHandler handler;
#Before("#annotation(com.yours.RunAsAdmin)")
public void doRunAsAdmin() {
handler.grantAdminRightsToCurrentUser(); //your real stuff
}
}

Resources