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;
网友评论