# Spring Boot basic common annotation explanation

Spring Boot basic common annotation explanation


@Autowired

  • Objects used for injecting dependencies can be used for setter methods, construction methods, and fields. Is package org.springframework.beans.factory.annotation;the comment inside. Assemble dependent objects according to type (ByType). The default is that the injected object must exist. If the object does not exist, an error will be reported, @Autowired(required=false)and the injected object can be used .

  • @QualifierUsed in conjunction with annotations. This means that the name of the currently injected bean is the @Qualifierlatter bean. Note that the @Qualifierlatter bean must exist during use, otherwise an error will be reported.

Example @Autowired injected object
@Component
public class AnnoationClazz {
    
    

    private UserService userService;

    @Autowired
    RedisProperties redis;

    @Autowired(required = true)
    public void setUserService(@Qualifier("userService") UserService userService) {
    
    
        this.userService = userService;
    }
}
Example @Autowried injecting static objects
@Component
public class AnnoationClazz {
    
    
    
    private static RedisProperties redisProperties;

    @Autowired(required = true)
    public void setRedisProperties(RedisProperties redisProperties) {
    
    
        AnnoationClazz.redisProperties=redisProperties;
    }
}

@Resource

  • The default injection method is ByName, which is the java.lang.annotation.*following class.
  • There are Name and Type attributes, Spring @Resourceparses the annotated name into the bean name, and Type is the bean type. If the name attribute is used, the automatic injection strategy of byName is used, and the automatic injection strategy of byType is used when the type attribute is used. If neither the name nor the type attribute is specified, then the byName automatic injection strategy will be used through the reflection mechanism
Instance @Resource injected object
@Resource
private UserService userervice;

@Resource
private void setUserService(UserService userService){
    
    
    this.userService=userService;
}

@RequestParam

  • The parameters received by @RequestParam come from requestHeader.
  • Map the parameters above the requested Url to the parameters in the method.
  • For example, the requested Url islocalhost:9999/request/test2?name=张三&age=1
Instance @RequestParam receives request parameters
@RequestMapping("/test2")
public String getMap(@RequestParam String name,int age) {
    
    
    System.out.println("方法请求到了!");
    return name+String.valueOf(age);
}

@RequestBody

  • @RequestBody receives parameters from requestBody and the request body.
  • Usually for processing the non- Content-Type: application/x-www-form-urlencodeddata encoding format, such as: application/json, application/xmland other types of data.
Instance receiving parameters
@RequestMapping("/test1")
public List<String> getList(@RequestBody List<String> list){
    
    
    System.out.println("方法请求到了!");
    return list;
}

AthPathVariable

  • Get the data in Url
  • Url such as:localhost:9999/request/test3/12/张三
Get the data in the url
@RequestMapping("/test3/{age}/{name}")
public String str(@PathVariable("age") int age,@PathVariable("name") String name) {
    
    
    return age+name;
}

@Component

  • Indicates that a class is a component class, and Spring needs to create a Bean for this class.
  • @Component (@Controller, @Service, @Respository) is automatically assembled into the Bean container through classpath scanning.

@Bean

  • Indicates that it is a method in a configuration class and returns an object to the Spring container. This object must be registered as a Bean in the Spring context.
  • The method marked Bean generally describes the logic of generating Bean objects
@Component
public class TestComponent {
    
    
	
	@Bean
	public User getUser() {
    
    
		User user =new User(1,"张三",new Date());
		return user;
	}
}

@PostConstruct

  • It is Java's own annotation.javax.annotation.PostConstruct
  • The order of the annotation in the entire Bean initialization is: Constructor->@Autowired->@PostConstruct
Instance initialization static object
@Component
public class UserUtils {
    
    
	
	private Logger logger=Logger.getLogger(UserUtils.class);
	
	private static User users;
	
	@Autowired
	User user;
		
	@PostConstruct
	public void init() {
    
    
		UserUtils.users=user;
	}
	
	public static Map<String,String> getUserMap(){
    
    
		Map<String,String> map=new HashMap<String, String>();
		map.put("age",String.valueOf(users.getAge()));
		map.put("name",users.getName());
		map.put("date", String.valueOf(users.getDate()));
		return map;		
	}
}

@Qualifier

  • When there are multiple beans of one type, you can use @Qualifier("name") to specify. Use with @Autowired annotation.

@PropertySource

  • Specify the address of the configuration file to be read, and use it with the @Value annotation.
@Component
@PropertySource(value = {
    
    "redis.properties"})
public class RedisProperties {
    
    
	
	@Value("${redis.url}")
	private String url;
	
	@Value("${redis.password}")
	private String password;
	
	@Value("${redis.username}")
	private String username;

}

Guess you like

Origin blog.csdn.net/qq_37248504/article/details/108859121