How does spring boot find the path url very fast - java

I just started learning spring boot and learned about rest services.
I have a doubt that how spring is so fast to find that exact path('') class file in multiple jars.
Like I am sure it does not traverse the whole codebase. So does it maintain an index file with every URL and class file name?
Example: Below is given index function which is present in TestController.java file which should be called when /index is called. But if I have multiple jars in my project how the Spring knows that a getMapping of Greetings is defined in that class.
package com.example.springboot;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HelloController {
#GetMapping("/Greetings")
public String index() {
return "Greetings from Spring Boot!";
}
}

when you start spring boot application it scans for the classes which are annotated with stereotype annotation like #Controller, #Restcontroller,#Bean,#service.
when a class is annotated with #Controller it means it handles web requests. After the component scan it creates a bean for that class and stores it in spring Ioc container.
when a web request is made to the server it looks for the requested url is presented in that bean and serves the request.
A Bean is a simple object of that class.

Related

How does Spring auto convert objects to json for #RestController

I'm looking at code in which I'm assuming spring decides to use Jackson behind the scenes to auto convert an object to json for a #RestController
#RestController
#RequestMapping("/api")
public class ApiController {
private RoomServices roomServices;
#Autowired
public ApiController(RoomServices roomServices) {
this.roomServices = roomServices;
}
#GetMapping("/rooms")
public List<Room> getAllRooms() {
return this.roomServices.getAllRooms();
}
}
The Room class is just a plain java class with some fields, getters/setters. There is no Jackson or any other explicit serialization going on in the code. Although this does return json when checking the url. I tried looking through the spring documentation but I'm not quite sure what I'm looking for. What is the name for this process in spring / how does it work? I tried with just #Controller and it broke. Is this functionality coming from #RestController?
If you are using Spring Boot Starter Web, you can see that it's using Spring Boot Starter JSON through the compile dependencies, and Jackson is the dependency of the Start JSON library. So, you're assumption is right (Spring is using Jackson for Json convertion by default)
Spring use it's AOP mechanism to intercept the mapping methods in #Controller (you can see that #RestController is actually a #Controller with #ResponseBody), spring create a proxy object (using JDK proxy or through cglib) for the class that annotated with #Controller.
When the request flow is processing, the program who really call the mapping method will be lead to the proxy first, the proxy will invoke the real #Controller object's method and convert it's returning value to Json String using Jackson Library (if the method is annotated with #ResponseBody) and then return the Json String back to the calling program.

How to #Autowire services in SpringBoot

Good day, guys. I have a question about autowiring services into my classes when using Springboot. All of the examples I have seen on the Internet as well as in the Springboot specification do something of the like (taking an excerpt from the Springboot version 1.5.7 specification):
package com.example.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
#Service
public class DatabaseAccountService implements AccountService {
private final RiskAssessor riskAssessor;
#Autowired
public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
}
// ...
}
This is a class that injects a property through its constructor, by means of #Autowiring the constructor. Another form is to #Autowire the property like this:
#Autowired
private final RiskAssessor riskAssessor
But, where I work, for these two methods to work, I have been told that I need to use this method:
applicationContext.getAutowireCapableBeanFactory().autowireBean(Object.class)
They have told me that I need this in order for the #Autowired annotation to work.
Now my question to you is: why is there no simple annotation that allows the #Autowire to function correctly? (Something like #AutowiredClass). The above method is too verbose and hard to remember, so surely there must be a better way to make #Autowired work on classes in order to inject services, just like we do in Grails where we just say def someService and it is automatically injected.
If you want properly use #Autowired in your spring-boot application, you must do next steps:
Add #SpringBootApplicationto your main class
Add #Service or #Component annotation to class you want inject
Use one of two ways that you describe in question, to autowire
If you don't have any wiered package structure and the main class package includes all other classes you want spring to instantiate (directly or in the subpackages) a simple annotation #ComponentScan on your main class will help you save all those boiler plate code. Then spring will do the magic, it will go and scan the package(and subpackages) and look for classes annotated with #Service, #Component etc and instantiate it.
Even better, use #SpringBootApplication in your main class, this will cover #Configuration as well. If it is a green field project , I would encourage to start from start.spring.io - a template generation/scaffolding tool for spring
Now my question to you is: why is there no simple annotation that allows the #Autowire to function correctly?
There is: #SpringBootApplication
If you put this at the root of your application (file that contains the main class) and as long as your services are at the same package or a sub-package, Spring will auto-discover, instantiate, and inject the proper classes.
There's an example in this walk-through: REST Service with Spring Boot
As described in that page:
#SpringBootApplication is a convenience annotation that adds all of the following:
#Configuration tags the class as a source of bean definitions for the application context.
#EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
#ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.
You need to annotate the implementation of RestService as a #Service or #Component so Spring would pick it up.
#Service
public class MyRiskAssessorImpl implements RiskAssessor {
///
}
#Autowired almost works out of the box. Just do your component scanning of the class you want to autowire and you are done. Just make sure your main class (or main configuration class) uses #ComponentScan("{com.example.app}") or #SpringBootApplication (main class). The docs explain this stuff pretty good

<context:componentscan> how does it work?

Can "context: componentscan" scan custom annotations? If so, where does it store the scanned beans in application context after scanning? How can I access the results?
We register beans or components in XML configuration file. So Spring can detect those beans, components. Spring also support to auto scan, detect and instantiate beans from pre-defined project package via the annotation. So we don't have to declare in the configuration anymore. For example:
<context:component-scan base-package="abc.controller, abc.service" />
in your Controller or Service, you just have to add annotation like:
#Controller
public class SampleController
#Service
public class SampleService
Spring will know that your SampleController and SamplerService and you can use it as you want.
Some detail here: http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch06s02.html

Failing to #AutoWire a member in a #WebServlet

I can't seem to get my servlet's fields to #AutoWire; they end up null. I have a pure annotation-configured webapp (no XML files). My servlet looks like this:
#WebServlet("/service")
public
class
SatDBHessianServlet
extends HttpServlet
{
#Autowired protected NewsItemDAO mNewsItemDAO;
}
Other #AutoWired things seem to work fine, both #Service objects and #Repository objects. But not this one, and I can't figure out why. I even tried adding its package to the ComponentScan(basePackages) list for my other classes.
Additional Info:
I added the following to my servlet’s init() method, and everything seemed to wire up properly, but I'm confused as to why Spring can't wire it up without that.
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, inConfig.getServletContext());
Servlet are web components that are not being created by Spring container, based on that lifecycle is not managed by the container and a lot of stuff that spring provides such as autowired or aspect can not run from them.
You need to indicate to the spring container that a component was created outside of IoC container and need to be part of it.
And as the API said SpringBeanAutowiringSupport is for:
Convenient base class for self-autowiring classes that gets
constructed within a Spring-based web application
This generic servlet base class has no dependency on the Spring ApplicationContext concept.
There is another way to indicate servlets being created by spring container using an interface
Spring MVC uses the DispatcherServlet for handling all requests in a Servlet environment.
The DispatcherServlet accordingly forwards the requests to the appropriate #Controller class (if any) based on the #RequestMapping on that class and/or it's methods.
What is happening in your Servlet is that it is not managed by Spring (but by the Servlet container) and there for no injection of dependencies is occurring.

Accessing Service Layer from custom Controller in Spring MVC

I am new to Spring and ROO and this Annotation/Aspect hell.
I have an Spring MVC Project created with Spring ROO.
I use mongo-db as my persistance layer.
I have an entity Report with the domain object, the service, the repository and the controller.
I added a custom controller wich workes so far.
I want to just access my stored reports with the ReportService.findAllReports(), but I'm not sure how to get access to this service.
Here is a link to my roo generated site http://sauberseite.cloudfoundry.com/
The main objective is to report adresses and then display all adresses in a google map, for which I have my custom controller and where I need to access the service layer
You can directly #Autowired it as follows.
#Controller
public class CustomController {
#Autowired
ReportService reportService; //this inject's your bean here.
List<Report> getReports() {
return reportService.findAllReports();
}
}
If you don't use annotation #Controller and defined your bean in xml, then you can inject ReportService as a property (just remove #Autowired annotation) and write a setter for it.

Categories