Spring常用注解

控制层参数相关注解
Spring常用注解

    //相当于@PostMapping(“/requestParamTest”)
    @RequestMapping(value="/requestParamTest", method = RequestMethod.POST)
    public String requestParamTest(@RequestParam(value="username") String userName, @RequestParam(value="usernick") String userNick){
    //若前台入参与此处接收参数完全一致,可不用@RequestParam
        System.out.println("requestParam Test");
        System.out.println("username: " + userName);
        System.out.println("usernick: " + userNick);
        return "hello";
    }
@GetMapping(value = {"/list/{deviceTypeId}","/list"})
public BaseBody getAll(@PathVariable(value = "deviceTypeId",required = false) Long deviceTypeId) throws Exception {

}

@Autowired默认按类型匹配的方式,在容器查找匹配的Bean,当有且仅有一个匹配的Bean时,Spring将其注入@Autowired标注的变量中。
@Qualifier注解限定Bean的名称
@Resource,默认通过name属性去匹配bean,找不到再按type去匹配

@Controller用于标注控制层组件
@Service对应的是业务层Bean
@Component是所有受Spring 管理组件的通用形式,@Component注解可以放在类的头上,@Component不推荐使用
@Repository对应数据访问层Bean

@Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean
@Scope注解 作用域
@Lazy(true) 表示延迟初始化

@PostConstruct用于指定初始化方法(用在方法上)
@PreDestory用于指定销毁方法(用在方法上)

原文地址:https://www.cnblogs.com/xiaobingzi/p/10751402.html