English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SpringBoot 注解

Experience notes Online toolInformazioni supplementari. Le annotazioni di Spring Boot sono una forma di metadati che possono fornire informazioni sul programma. In altre parole, le annotazioni vengono utilizzate per fornire informazioni sul programma.

In questa sezione, discuteremo alcune informazioni importanti. Spring Boot annotations,Utilizzeremo questo tutorial in una parte successiva.

Core Spring框架注解

@Required: 它适用于 bean 设置方法。它指示必须在配置时使用必需的属性填充带注解的Bean,否则它将引发异常 BeanInitilizationException .

Esempio

public class Machine 
{
private Integer cost;
@Required
public void setCost(Integer cost) 
{
    this.cost = cost;
}
public Integer getCost() 
{
    return cost;
}   
}

@Autowired: : Spring通过提供@Autowired注解来提供基于注解的自动装配。它用于自动连接setter方法,实例变量和构造函数上的spring bean。当我们使用@Autowired批注时,spring容器通过匹配数据类型自动连接bean。

Esempio

@Component
public class Customer
{
    private Person person;
    @Autowired
    public Customer(Person person) 
    { 
        this.person=person;
    }
}

@Configuration: : 它是一个类级别的注解。带有@Configuration注解的类由Spring Containers用作bean定义的源。

Esempio

@Configuration
public class Vehicle
{
    @BeanVehicle engine()
    {
        return new Vehicle();
    }
}

@ComponentScan: : 当我们要扫描软件包中的bean时使用。它与注解@Configuration一起使用。我们还可以指定用于扫描Spring组件的基本软件包。

Esempio

@ComponentScan(basePackages = "com.w3codebox")
@Configuration
public class ScanComponent
{
// ...
}

@Bean: 是方法级的注解。它是XML标记的代替方法。它告诉产生由Spring Container管理的bean的方法。

Esempio

@Bean
public BeanExample beanExample() 
{
    return new BeanExample();
}

Spring Framework构造型注解

@Component: . It is a class-level annotation. It is used to mark Java classes as Beans. A class marked with @Component annotated Java class. The Spring framework picks it up and configures it in the application context as Spring Bean .

Esempio

@Component
public class Student
{
    ……
}

@Controller: @Controller is a class-level annotation. It is @Component specialization. It marks a class as a Web request handler. It is usually used to serve web pages. By default, it returns a string indicating the route to be redirected. It is usually used with @RequestMapping annotations are used together.

Esempio

@Controller
@RequestMapping("books")
public class BooksController 
{
    @RequestMapping(value = "/{name}", method = RequestMethod.GET)
    public Employee getBooksByName() 
    {
        return booksTemplate;
    }
}

@Service: is also used at the class level. It tells Spring that this class contains Business logic.

Esempio

package com.w3codebox;
@Service
public class TestService
{
    public void service1()
    {
        //business code
    }
}

@Repository: This is a class-level annotation. The repository is directly accessible to the database DAO (Data Access Object). This repository performs all operations related to the database.

package com.w3codebox;
@Repository 
public class TestRepository
{
    public void delete()
    {   
        //persistence code
    }
}

Spring Boot annotations

@EnableAutoConfiguration: : It automatically configures the beans present in the classpath and configures them as running methods. In the Spring Boot 1.2.0 release, the use of this annotation was reduced because developers provided an alternative to this annotation, namely @SpringBootApplication . @SpringBootApplication: : It is three annotations @EnableAutoConfiguration, @ComponentScan, @Configuration 的组合。

Spring MVC and REST annotations

@RequestMapping: per mapparerichiesta di rete. Ha molti elementi opzionali, come consumes, header, method, name, params, path, produces e value. Lo si utilizza insieme alla classe e al metodo.

Esempio

@Controller
public class BooksController 
{
    @RequestMapping("/computer-science/books")
    public String getAllBooks(Model model)
    {
        //application code
        return "bookList";
    }
}

@GetMapping: Esso HTTP GET Mappare la richiesta a un metodo di elaborazione specifico. Viene utilizzato per creareestrarreIl punto di terminazione del servizio web, invece di @RequestMapping(method = RequestMethod.GET) @PostMapping Esso HTTP POST Mappare la richiesta a un metodo di elaborazione specifico. Viene utilizzato per crearecreareIl punto di terminazione del servizio web, invece di @RequestMapping(method = RequestMethod.POST) @PutMapping: Esso HTTP PUT Mappare la richiesta a un metodo di elaborazione specifico. Viene utilizzato per crearecreareoaggiornareIl punto di terminazione del servizio web, invece di @RequestMapping(method = RequestMethod.PUT) @DeleteMapping: Esso HTTP DELETE Mappare la richiesta a un metodo di elaborazione specifico. Viene utilizzato per creareeliminareIl punto di terminazione del servizio web della risorsa. Utilizzalo al posto di @RequestMapping(method = RequestMethod.DELETE) @PatchMapping: Esso HTTP PATCH Mappare la richiesta a un metodo di elaborazione specifico. Utilizzalo al posto di @RequestMapping(method = RequestMethod.PATCH) @RequestBody: per binding l'oggetto della richiesta HTTP con l'oggetto nei parametri del metodo.binding. All'interno, utilizza HTTP MessageConverters Convertire il corpo della richiesta. Quando si utilizza l'annotazione @RequestBody per il parametro del metodo, il framework Spring binderà il corpo della richiesta HTTP al parametro. @ResponseBody: Il metodo binding il valore di ritorno al corpo della risposta. Informa il Framework di Spring Boot di serializzare l'oggetto di ritorno in formato JSON e XML. @PathVariable: 用于从URI中提取值。它最适合RESTful Web服务,其中URL包含路径变量。我们可以在一个方法中定义多个@PathVariable。 @RequestParam: 用于从URL提取查询参数。也称为查询参数。它最适合Web应用程序。如果URL中不存在查询参数,则可以指定默认值。 @RequestHeader: 用于获取有关HTTP请求标头的详细信息。我们将此注解用作方法参数。注解的可选元素是名称,必填,值,defaultValue。 对于标题中的每个细节,我们应指定单独的注解。我们可以在一种方法中多次使用它 @RestController: 可以将其视为 @Controller @ResponseBody 注解的组合。 @RestController注解本身使用@ResponseBody注解进行注解。无需使用@ResponseBody注解每个方法。 @RequestAttribute: 它将方法参数绑定到请求属性。它提供了从控制器方法方便地访问请求属性的方法。借助@RequestAttribute批注,我们可以访问服务器端填充的对象。

注意: 我们在RESTful Web服务教程中使用了以上所有示例和真实示例。