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

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?

Related

At what time spring create object of a class when it is Autowired..?

We autowire class in spring framework so that we can call the methods of autowired class to performe some action. My question is when spring creates object of that class internally.
The Spring Context is initialized directly when you start your application by the Spring Framework.
THen maybe you can refine your question if you want to ask something specific about ordering or availability of your beans.

In spring why the interface is injected?

In spring why the interface is injected rather than the class implementing the interface?
In fact even if you use interface a class is injected. Spring looks for a bean - class which implements the interface.
In some cases like e.g. Spring Data dynamic proxy is created for the interfaces which in fact a class to listen interface's methods calls and do some logic on the calls.
If you set a breakpoint and check what is really injected you will find a class (either your class or some Proxy object)

How to hide spring data repository functions in service class?

I am using spring data JPA repository, my requirement is when i call repository class methods in service class it should show only custom methods like addUser(X,Y) instead of save().
Few things i understand, implementation of spring repository is provided by spring framework at runtime, So we cannot provide out own implementation. (This will overhead).
All methods in JPARepository is public only, so its obivious when we implement this interface all methods will be visible through out.
I am thinking of using DAO and Repository both at same time. DAO will provide custom function signature and repository will implement DAO interface.
Any Hack ?
If you don't want the methods from JpaRepository or CrudRepository, don't extend those but just Repository instead. It is perfectly fine to have a repository interface like
MyVeryLimitedRepository extends Repository<User, Long> {
User findByName(String name);
}
Of course methods like addUser(X,Y) will need a custom implementation.
You can very well use DAO pattern in this case .
By implementing DAO Pattern in Service Class
You create a wrapper between Service and Repository.
You can custom code your DAO layer to only expose custom methods to service layer

HystrixCommand only works with Spring Service or Component?

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.

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.

Resources