Difference between #Configuration #Bean and #RestController annotations - java

I'm new to Java and the Spring framework. I would like to understand the difference between
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
and
import org.springframework.web.bind.annotation.RestController;
I see people are using the above annotations to define the controllers in the application. I would like to understand what is the difference between them and when to use what.
In my case, I'm using the spring webflux framework so which is the most suitable annotation for defining the routers?
Any help is appreciatable :)

#Bean:
In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.
--> Classes that have logic in them. They do some business logic.
https://www.baeldung.com/spring-bean
#Configuration:
Spring #Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has #Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application
https://www.journaldev.com/21033/spring-configuration-annotation
--> How you tell Spring that you have beans that need to setup.
#RestController:
The #RestController annotation was introduced in Spring 4.0 to simplify the creation of RESTful web services. It's a convenience annotation that combines #Controller and #ResponseBody – which eliminates the need to annotate every request handling method of the controller class with the #ResponseBody annotation.
https://www.baeldung.com/spring-controller-vs-restcontroller
--> If you have endpoints? You are definining an API, GET/POST/DELETE api interface for your business. In those #Controller, you call your #Autowired beans (Services/Components) to do the business logic.
If there is another Application calling yours through the REST API? Then they call your #Controller Endpoints, (That is their main entry point to your system)

Related

How the default run() function in springboot maven project works

I am new to the Springboot framework, and I have some questions about the defualt run() function in the application.java file of the springboot maven project.
I'm trying to build a REST api with spring maven project. In specific, if I have a #Restcontroller class, when and how does the default run() function call an instantiated object of it? And how are the annotated classes like #Service, #RestController and #SpringBootApplication linked together when running the application? (like, what is the process of execution of all these components at runtime?) I know the controller class is used to host APIs, but what should I put in the #Service classes?
Thanks!
#SpringBootApplication annotation in the main application class combines the #EnableAutoConfiguration, #Configuration and the #ComponentScan annotations.
When run, the static run() method starts the Spring Boot application:
#ComponentScan scans the main class package and subpackages for different #Component classes such as controllers, services and repos and registers them as beans in the ApplicationContext.
#EnableAutoConfiguration auto configures your application based on the included jars in classpath.
As far as components go:
#RestController as you said exposes the application's endpoints using #RequestMapping method annotations.
#Service holds the business logic and calls repository methods.
#Repository is data access object that connects to the databases.

In Spring, is there a way to inject a class that does not have a #Component or only uses javax annotations?

I have a library that can use javax.inject annotations but must not be coupled to Spring, as it's used by both a Spring application and an application using Guice.
Other that creating an #Bean 'provider' in the #Configuration, is there a way to get Spring to inject the class into #Autowired/#Inject fields?

Purpose of #Component annotation on spring-boot-starter-jersey resources in Spring boot

#Path("test")
public TestResource {
#GET
public Response testGet() {
return Response.ok().build();
}
}
From the spring boot documentation, the section on JAX-RS and Jersey, "All the registered endpoints should be #Components with HTTP resource annotations (#GET etc.), e.g.". The above resource still works without the #Component annotation. What would I be breaking by leaving out the #Component annotation?
"To enable JAX-RS resources to work Spring functionality that requires proxying, such as Spring transaction management (with #Transactional), Spring Security and aspect oriented programming (such as #Aspect), the resources must themselves be managed by Spring, by annotating with #Component, #Service, #Controller or #Repository:"

Spring MVC #Controller and profiles

I have a spring mvc controller and I want to enable it only in some profiles (for example development and test).
I know I can use the profile attribute of the beans element in xml configuration to limit the scope of my beans, but I'm using convenient annotations for the controllers now.
Can I bind the annotated controller to given profile somehow?
Or do I have to use the "old way" (of implementing and declaring controller) without annotations and use the beans element in xml configuration?
Will the annotated controllers mix well with the "old ones"?
EDIT: another way, which comes to my mind, is to check the profile in runtime from autowired Environment instance, but this denies the inversion of control
Is this what you mean ?
#Controller
#Profile("test")
public class CacheController {
}
javadoc

What is #Service in Spring MVC

If I use #Service on a service class, do I need to make a service class bean in my servlet xml file or do I have to do both?
You don't have to declare a bean in your context file if you:
1) Annotate the class with:
#Component, #Service, #Controller or #Repository
2) Include the context:component-scan element in your context file like this:
<context:component-scan base-package="your.package" />
Hope that helps.
Last time I looked (Spring 2.5) #Service was a marker annotation subclassed from #Component, but with no additional behaviour. Which means that beans tagged with #Service become candidates for auto detection if you are using annotation-based configuration via classpath scanning.
As per the docs, the intention is that this annotation might include service layer specific functionality in future Spring releases. It can also act as an AOP point cut for all of your service layer components.

Categories

Resources