Home  >  Article  >  Java  >  A brief analysis of spring and springboot annotations

A brief analysis of spring and springboot annotations

不言
不言forward
2018-10-15 16:03:292254browse

The content of this article is about the annotations of spring and springboot. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

@ConfigurationProperties

It is very convenient to bind the content in the resource file to the object

@Value( "${app.name}")

Inject simple values

@Import

Add the instance to springIOC through import Add the @Import annotation to the

/**
 * 定义一个圆形
 *
 * @author zhangqh
 * @date 2018年5月1日
 */
public class Circular {
}
/**
 * 定义一个正方形
 *
 * @author zhangqh
 * @date 2018年5月1日
 */
public class Square {
}

MainConfig annotation configuration in the container as follows:

@Import({Square.class,Circular.class})
@Configuration
public class MainConfig
AnnotationConfigApplicationContext applicationContext2 = new AnnotationConfigApplicationContext(MainConfig.class);
String[] beanNames = applicationContext2.getBeanDefinitionNames();
for(int i=0;i<beanNames.length;i++){
    System.out.println("bean名称为==="+beanNames[i]);
}

Running results:

bean名称为===mainConfig
bean名称为===com.zhang.bean.Square
bean名称为===com.zhang.bean.Circular

@RequestMapping

This annotation can be used on classes and methods. It is used on classes to indicate the parent path. For example, if the class is demo and the method is /demo1, then the access path is demo/demo1

This annotation has six attributes. :
params: The specified request must contain certain parameter values ​​before it can be processed by this method.
headers: The specified request must contain certain specified header values ​​in order for this method to process the request.
value: Specify the actual address of the request, the specified address can be URI Template mode
method: Specify the method type of the request, GET, POST, PUT, DELETE, etc.
consumes: Specify the submission content type for processing the request (Content-Type), such as application/json, text/html;
produces: Specifies the content type to be returned. Only when the (Accept) type in the request header contains the specified type will it return

@PathVariable

The variable corresponding to the path, used before the parameter, the variable name on the path must be consistent with the parameter name

RequestMapping("demo/demo1/{name}")public String say(@PathVariable String name){
}

@RequestParam

has the same effect as the following code

String name =request.getParameter("name ").

@RequestBody

means that the method parameters are bound to the HTTP request body, and the front end cannot submit it in a form and needs to submit it in json.

@RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body,@RequestBody User user){
   //可以绑定自定义的对象类型
}

@ResponseBody

means that the output is data in json format.

@ModelAttribute

1. When applied to parameters, the parameters passed by the client will be injected into the specified object by name, and the object will be automatically added. ModelMap, easy to use in the View layer;

2. When applied to methods, it will be executed before each method marked with @RequestMapping. If there is a return value, the return value will be automatically added to the ModelMap;

@Bean

is equivalent to 60e23eb984d18edbb092da6b8f295aba4bb0e59fd50cbfe6f6ce2215b9d94243 in XML. It is placed above the method instead of the class, which means to generate a bean. And handed over to spring management.

@Qualifier

When there are multiple beans of the same type, you can use @Qualifier("name") to specify. Used in conjunction with @Autowired

@Autowired

Belongs to Spring's org.springframework.beans.factory.annotation package and can be used to provide class attributes, constructors, Method for value injection

@Resource

does not belong to spring annotations, but comes from JSR-250 located under the java.annotation package. Use this annotation as the target bean. Specify the collaborator bean.

@Resource is equivalent to @Autowired, and can be annotated on the setter method of a field or attribute.

Note:

@Autowired annotation is assembled by type by default. If the container contains multiple beans of the same type, an exception that the specified type of bean cannot be found will be reported when starting the container. Solution It is qualified in combination with the @Qualified annotation and specifies the injected bean name.

@Resource If the name attribute is not specified and the dependent object cannot be found according to the default name, the @Resource annotation will fall back to assembly by type. But once the name attribute is specified, it can only be assembled by name.

@Autowired annotation can easily throw exceptions when assembly, especially when there are multiple bean types being assembled. The solution is to add @Qualified to limit it.

Component class annotations

@Component: Standard an ordinary spring Bean class.
@Repository: Mark a DAO component class.
@Service: Mark a business logic component class.
@Controller: Mark a controller component class.

@Component can replace @Repository, @Service, and @Controller, because these three annotations are annotated by @Component. The annotated Java class is treated as a Bean instance. The name of the Bean instance defaults to the first letter of the Bean class in lowercase, and the other parts remain unchanged. @Service can also customize the Bean name, but it must be unique!

@SpringBootApplication

Contains @Configuration, @EnableAutoConfiguration, @ComponentScan
usually used on startup classes.

@ComponentScan

Component scan. If classes with @Component @Controller @Service and other annotations are scanned, these classes will be registered as beans.

@Configuration

Indicates that this class is the information source of Bean configuration, which is equivalent to 3c9fdce8078791299ca92e41bddc2b8fa6748f3712e7c0e1f60ffca24b3db7b0 in XML, and is generally added to the main class superior.

@EnableAutoConfiguration

Let Spring Boot automatically configure the Spring framework based on the dependencies declared by the application, usually added to the main class.

@Profiles
Spring Profiles提供了一种隔离应用程序配置的方式,并让这些配置只能在特定的环境下生效。
任何@Component或@Configuration都能被@Profile标记,从而限制加载它的时机。

@Configuration
@Profile("prod")public class ProductionConfiguration {    // ...}

The above is the detailed content of A brief analysis of spring and springboot annotations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete