美文网首页
注解@积累

注解@积累

作者: 飞鹰雪玉 | 来源:发表于2022-03-18 09:40 被阅读0次

1 、@Valid ,@NotBlank用法:

@Valid 注解通常用于对象属性字段的规则检测,一般是在新增接口的时候会用到,检测前端传输的该实体对象字段是否满足既定的规则,比如:姓名不能为空,年龄不能超过100岁等等。
一般是在Bean 实体属性上增加@NotBlank等注解去实现规则
类似于下面:


image.png

然后在controller层去增加@Valid校验。


image.png
这样就可以实现规则校验,而不是写一大堆的if判断语句,类似于下面这样:
image.png

2 、@autowired

@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。在使用@Autowired之前,我们对一个bean配置起属性时,是这用用的

<property name="属性名" value=" 属性值"/>  

通过这种方式来,配置比较繁琐,而且代码比较多。在Spring 2.5 引入了 @Autowired 注释

下面用案例来具体说明

UserRepository.java

package com.proc.bean.repository;

public interface UserRepository {

    void save();
}

这里定义了一个UserRepository接口,其中定义了一个save方法

UserRepositoryImps.java

package com.proc.bean.repository;

import org.springframework.stereotype.Repository;

@Repository("userRepository")
public class UserRepositoryImps implements UserRepository{

    @Override
    public void save() {
        System.out.println("UserRepositoryImps save");
    }
}

定义一个UserRepository接口的实现类,并实现save方法,在这里指定了该bean在IoC中标识符名称为userRepository

UserService.java

package com.proc.bean.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.proc.bean.repository.UserRepository;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public void save(){
        userRepository.save();
    }
}

这里需要一个UserRepository类型的属性,通过@Autowired自动装配方式,从IoC容器中去查找到,并返回给该属性

applicationContext.xml配置

<context:component-scan base-package="com.proc.bean" />

测试代码:

  ApplicationContext ctx= new  ClassPathXmlApplicationContext("applicationContext.xml");
  UserService userService=(UserService) ctx.getBean("userService");
  userService.save();

输出结果:UserRepositoryImps save

那么使用@Autowired的原理是什么?

其实在启动spring IoC时,容器自动装载了一个AutowiredAnnotationBeanPostProcessor后置处理器,当容器扫描到@Autowied、@Resource(是CommonAnnotationBeanPostProcessor后置处理器处理的)或@Inject时,就会在IoC容器自动查找需要的bean,并装配给该对象的属性

 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  

** 注意事项:**

在使用@Autowired时,首先在容器中查询对应类型的bean

如果查询结果刚好为一个,就将该bean装配给@Autowired指定的数据

如果查询的结果不止一个,那么@Autowired会根据名称来查找。

如果查询的结果为空,那么会抛出异常。解决方法时,使用required=false

举例说明:

在上面例子中,我们在定一个类来实现UserRepository接口

package com.proc.bean.repository;

import org.springframework.stereotype.Repository;

@Repository
public class UserJdbcImps implements UserRepository {

    @Override
    public void save() {
        System.out.println("UserJdbcImps save");
    }
}

这时在启动容器后,在容器中有两个UserRepository类型的实例,一个名称为userRepository,另一个为userJdbcImps。在UserService中

@Autowired
private UserRepository userRepository;

输出结果:UserRepositoryImps save

这里由于查询到有两个该类型的实例,那么采用名称匹配方式,在容器中查找名称为userRepository的实例,并自动装配给该参数。

如果这里想要装载userJdbcImps的实例,除了将字段userRepository名称改成userJdbcImps外,可以提供了一个@Qualifier标记,来指定需要装配bean的名称,代码这样写

 @Autowired
 @Qualifier("userJdbcImps")
 private UserRepository userRepository;

输出结果:UserJdbcImps save

3 、@RequiredArgsConstructor和@AllArgsConstructor

lombok的这两个注解可以替代@ Autowired注解,完成注入。
@AllArgsConstructor使用时,默认将所有成员变量生成全参构造函数,所以我们在类中定义的成员变量就会没法完成注解,会报
Parameter x of constructor in xxxClass required a bean of type 'xxxxx' that could not be found.错误。
我在使用时是这样使用的:

@AllArgsConstructor
@RestController
@Slf4j
@RequestMapping("/index/tasks")
public class TaskController {

    /**
     * mongoDB接口
     */
    private final MongoTemplate mongoTemplate;
    /**
     * 统计服务
     */
    private final TaskService taskService;
    /**
     * 组织服务
     */
    private final OrganizationService organizationService;

    /**
     * 时间变量,控制定时任务传参使用
     */
    private Long curTime;

我声明的curTime只是成员变量,不需要注入,此时就会报错。
而采用@RequiredArgsConstructor则可以解决问题,我们可以将需要注入的变量加上final修饰符或者@NotNull注解来保证构造函数只对这些变量构造,不会将未加修饰符的成员变量放到构造函数里面。如:

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/index/tasks")
public class TaskController {

    /**
     * mongoDB接口
     */
    private final MongoTemplate mongoTemplate;
    /**
     * 统计服务
     */
    private final TaskService taskService;
    /**
     * 组织服务
     */
    private final OrganizationService organizationService;

    /**
     * 时间变量,控制定时任务传参使用
     */
    private Long curTime;

相关文章

  • 注解@积累

    1 、@Valid ,@NotBlank用法: @Valid 注解通常用于对象属性字段的规则检测,一般是在新增接口...

  • SpringBoot

    springBoot问题积累持续。。。 问题一: junit注解使用@Before不生效问题,初始化初始参数失败问...

  • 如何点评文章

    文章链接: 主要内容: 阅读感受: 文章亮点: 可提升点: 个人建议: 金句积累: 鼓励赠言: 相关注解: (1)...

  • 注解学习笔记

    什么是注解注解分类注解作用分类 元注解 Java内置注解 自定义注解自定义注解实现及使用编译时注解注解处理器注解处...

  • 注解与反射

    注解 声明一个注解类型 元注解 在定义注解时,注解类也能够使用其他的注解声明。对注解类型进行注解的注解类,我们称之...

  • 1.8 Java 注解annotation

    1.1 注解声明 Java注解Annotation,有声明注解和元注解 元注解:Java提供的元注解,所谓元注解就...

  • 注解的使用

    元注解 注解 注解本质就是接口: 元注解:修饰注解的注解 自定义注解 Text.java FruitName.ja...

  • 注解

    Java注解 注解 元注解 自定义注解 元注解:负责注解其他注解 共有4个标准的meta-annotation类型...

  • S10E15: 帐单废稿

    收集 S10E15 上半场积累的文字, 回顾注解为什么放弃: 写是写的最好回报 (...没说透) 随时想尝试写点儿...

  • Spring高级应用之组合注解和元注解

    1.核心概念: 元注解:可以注解在其他注解上的注解;被注解的注解成为组合注解; 2.组合注解的定义步骤 定义组合注...

网友评论

      本文标题:注解@积累

      本文链接:https://www.haomeiwen.com/subject/fzuxwktx.html