宿迁手机网站建设公司,镇江微信推广平台,广州百度seo,app应用分发平台开发注解的优势#xff1a; 采用纯 java 代码#xff0c;不在需要配置繁杂的 xml 文件在配置中也可享受面向对象带来的好处类型安全对重构可以提供良好的支持减少复杂配置文件的同时亦能享受到 springIoC 容器提供的功能 1. 常用的Spring Boot注释及其用途和示例
1#xff09;S… 注解的优势 采用纯 java 代码不在需要配置繁杂的 xml 文件在配置中也可享受面向对象带来的好处类型安全对重构可以提供良好的支持减少复杂配置文件的同时亦能享受到 springIoC 容器提供的功能 1. 常用的Spring Boot注释及其用途和示例
1SpringBootApplication
这是一个组合注解它包含了 ConfigurationEnableAutoConfiguration 和 ComponentScan 三个注解。它的作用是标记 Spring Boot 应用的主类让 Spring Boot 自动进行必要的配置扫描并加载符合条件的组件或 bean 定义启动内嵌的 web 服务器等。一般来说我们只需要在主类上添加这个注解就可以快速启动一个 Spring Boot 应用。
SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}2RestController
这是一个组合注解它包含了 Controller 和 ResponseBody 两个注解。它的作用是标记一个控制器类表示该类的所有方法的返回值都直接写入 HTTP 响应体中而不是解析为跳转路径。这个注解通常用于构建 RESTful 的 API返回 JSON 或 XML 格式的数据。
RestController
RequestMapping(/users)
public class UserController {Autowiredprivate UserService userService;GetMapping(/{id})public User getUser(PathVariable(id) Long id) {return userService.getUserById(id);}
}3RequestMapping
这是一个用于映射 HTTP 请求的注解它可以用在类或方法上。它的作用是指定一个或多个请求路径以及对应的请求方法请求参数请求头等条件将其绑定到一个控制器方法上。它还可以指定一个返回值的媒体类型以及一个视图名称等属性。
RequestMapping(value /hello, method RequestMethod.GET)
public String hello() {return Hello, Spring Boot!;
}4GetMapping、PostMapping、 PutMapping、 DeleteMapping、PatchMapping
这些注解都是 RequestMapping 的缩写它们分别对应了 GET, POST, PUT, DELETE, PATCH 这五种 HTTP 请求方法。它们的作用是简化 RequestMapping 的写法只需要指定一个请求路径即可其他属性都有默认值。它们的用法和 RequestMapping 类似只是更加简洁。
GetMapping(/hello)
public String hello() {return Hello, Spring Boot!;
}PostMapping(/users)
public User createUser(RequestBody User user) {return userService.saveUser(user);
}5PathVariable、RequestParam、RequestBody、 RequestHeader、 CookieValue
这些注解都是用于获取 HTTP 请求中的数据的注解它们可以用在控制器方法的参数上。它们的作用是分别从请求路径请求参数请求体请求头或 Cookie 中获取数据并将其绑定到方法参数上。它们都可以指定一个参数名以及一个是否必须的属性。
GetMapping(/users/{id})
public User getUser(PathVariable(id) Long id) {return userService.getUserById(id);
}GetMapping(/users)
public ListUser getUsers(RequestParam(value name, required false) String name) {return userService.getUsersByName(name);
}PostMapping(/users)
public User createUser(RequestBody User user) {return userService.saveUser(user);
}GetMapping(/hello)
public String hello(RequestHeader(User-Agent) String userAgent) {return Hello, your user agent is: userAgent;
}GetMapping(/cookie)
public String cookie(CookieValue(JSESSIONID) String sessionId) {return Your session id is: sessionId;
}6Autowired、Resource、Inject
这些注解都是用于实现依赖注入的注解它们可以用在字段构造器或方法上。它们的作用是从 Spring 容器中获取一个 bean 实例并将其赋值给被注解的元素。它们的区别是
Autowired 是 Spring 提供的注解它默认按照类型匹配 bean也可以通过 Qualifier 指定按照名称匹配。Resource 是 Java 标准提供的注解它默认按照名称匹配 bean也可以通过 type 属性指定按照类型匹配。Inject 是 Java 标准提供的注解它和 Autowired 类似都是按照类型匹配 bean但是它需要依赖 JSR-330 的实现如 Google Guice。
Service
public class UserService {Autowiredprivate UserDao userDao;Resource(name userCache)private Cache userCache;Injectprivate PasswordEncoder passwordEncoder;// ...
}7Bean、Component、 Service、Repository、 Controller
这些注解都是用于声明一个 bean 的注解它们可以用在类上。它们的作用是让 Spring 容器扫描并识别这些类将其实例化并加入到容器中以便其他组件可以使用。它们的区别是
Bean 是用在方法上的它表示该方法返回一个 bean 实例方法所在的类必须被 Configuration 注解标记。Component 是一个通用的注解它表示该类是一个组件没有特殊的功能。Service 是一个业务逻辑层的注解它表示该类是一个服务类提供一些业务功能。Repository 是一个数据访问层的注解它表示该类是一个数据访问对象提供一些数据操作功能。Controller 是一个控制器层的注解它表示该类是一个控制器类处理 HTTP 请求。
Configuration
public class AppConfig {Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}
}Component
public class HelloComponent {public void sayHello() {System.out.println(Hello, Spring Boot!);}
}Service
public class UserService {// ...
}Repository
public class UserDao {// ...
}Controller
public class UserController {// ...
}8Value、 ConfigurationProperties、 PropertySource
这些注解都是用于配置属性的注解它们可以用在类或字段上。它们的作用是从配置文件或环境变量中获取一些属性值并将其赋值给被注解的元素。它们的区别是
Value 是用于获取单个属性值的它可以使用 ${…} 占位符或 SpEL 表达式来获取属性值也可以指定一个默认值。ConfigurationProperties 是用于获取一组属性值的它可以使用 prefix 属性来指定一个属性前缀然后将其下的所有属性值绑定到一个 bean 的字段上支持松散绑定和类型转换也支持 JSR-303 的数据校验。PropertySource 是用于指定一个额外的属性源的它可以使用 value 属性来指定一个或多个属性文件的路径然后将其加载到 Spring 环境中以便其他注解可以使用。
Component
PropertySource(classpath:app.properties)
public class AppConfig {Value(${app.name})private String appName;Value(${app.version:1.0})private String appVersion;Value(#{systemProperties[os.name]})private String osName;// ...
}Component
ConfigurationProperties(prefix user)
Validated
public class UserConfig {NotBlankprivate String name;Min(18)private int age;private ListString hobbies;// getters and setters
}9EnableAutoConfiguration
此注解用于启用 Spring Boot 的自动配置机制。它根据类路径依赖项和属性自动配置应用程序。他可以简化配置过程从而实现快速开发。
例如
SpringBootApplication
EnableAutoConfiguration
public class MyApplication {// ...
}有EnableAutoConfiguration的情况下
Spring Boot将会根据项目的依赖和配置自动配置应用程序的各个组件例如数据源、JPA、Web等。MyService类会被自动扫描并纳入Spring容器管理。
没有EnableAutoConfiguration的情况下
我们需要手动配置应用程序的各个组件例如配置数据源、JPA、Web等这会增加开发工作量。MyService类不会被自动扫描需要显式配置才能被Spring容器管理
如果发现正在应用不需要的特定自动配置类则可以使用 EnableAutoConfiguration 的 exclude 属性 来禁用它们
例如
EnableAutoConfiguration(excludeName {org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration})10ConditionalOnProperty
此注解用于根据属性的值有条件地启用或禁用 bean 或配置。
例如
Configuration
ConditionalOnProperty(name my.feature.enabled, havingValue true)
public class MyConfiguration {// 当启用时的功能配置
}11Scheduled
此注解用于以固定的时间间隔调度方法的执行。
例如
Component
public class MyScheduler {Scheduled(fixedDelay 5000)public void doSomething() {// 定期执行任务}
}12Cacheable、CachePut、CacheEvict
这些注解用于缓存方法结果。它们允许您分别缓存方法的返回值、更新缓存或去除缓存。
Service
public class MyService {Cacheable(users)public User getUserById(Long id) {// 从数据库检索用户}CachePut(users)public User updateUser(User user) {// 更新数据库和缓存中的用户}CacheEvict(users)public void deleteUser(Long id) {// 从数据库删除用户并从缓存中移除}
}13PostConstruct
PostConstruct注解用于在依赖注入完成后且在类的初始化之前执行标注的方法。这提供了一个非常方便的生命周期钩子允许开发者在对象创建和依赖注入完成后执行初始化代码。
Component
public class MyBean {PostConstructpublic void init() {// 执行初始化操作如加载配置文件检查资源等System.out.println(MyBean is initialized);}
}在上述代码中init方法会在MyBean对象创建并注入所有必要的依赖之后被自动调用。
14ExceptionHandler、ControllerAdvice
ExceptionHandler注解用于处理Controller层抛出的异常。通过将此注解应用于方法上可以捕获特定类型的异常并对其进行自定义处理。
ControllerAdvice最常见的用途之一是全局异常处理。通过在类上使用ControllerAdvice注解然后在该类中使用ExceptionHandler注解标注的方法可以捕获指定类型的异常并进行处理。
ControllerAdvice
public class GlobalExceptionHandler {ExceptionHandler(value Exception.class)public ResponseEntityObject handleException(Exception e) {// 构建自定义的响应体如错误信息和错误码return new ResponseEntity(An error occurred, HttpStatus.INTERNAL_SERVER_ERROR);}
}2. 面向切面的编程 AOP 注解
这些注解通常用于Spring框架中的面向切面编程AOPAspect-Oriented Programming于实现面向切面编程将横切关注点从核心业务逻辑中分离出来以提高代码的可维护性和可重用性。
1Aspect
这个注解用于声明一个类是一个切面类定义切面类的时候需要添加这个注解。切面类是用来封装切面逻辑的类通常包含一个或多个通知方法以及一个或多个切点表达式。切面类需要被 Spring 容器管理因此通常也需要添加 Component 注解。例如
Aspect
Component
public class LogAspect {// 通知方法和切点表达式
}2Pointcut
这个注解用于定义一个切点表达式指定哪些连接点即方法执行的点需要被拦截。切点表达式可以使用 AspectJ 的语法也可以使用自定义的注解。切点表达式通常定义在一个空的方法上作为一个标识符供其他注解引用。例如
Pointcut(execution(* com.example.service.*.*(..)))
public void servicePointcut() {// 空方法仅用于标识切点
}这个注解表示所有 com.example.service 包下的类的所有方法都是切点需要被拦截。
3Before, After, AfterReturning, AfterThrowing, Around
这些注解都是用于定义通知方法的指定在切点执行的不同阶段执行不同的逻辑。通知方法是用来实现切面功能的方法通常需要引用一个切点表达式或者一个切点标识符。这些注解的含义和用法如下
Before表示在切点之前执行可以用来做一些前置处理如参数校验日志打印等。After表示在切点之后执行无论切点是否正常返回或抛出异常都会执行可以用来做一些清理工作如释放资源还原状态等。AfterReturning表示在切点正常返回之后执行可以用来做一些后置处理如返回结果处理日志记录等。AfterThrowing表示在切点抛出异常之后执行可以用来做一些异常处理如异常日志记录事务回滚等。Around表示环绕切点执行可以在切点前后执行自定义的逻辑也可以控制切点是否执行以及修改切点的参数和返回值是最强大也最复杂的通知类型。
例如
Before(servicePointcut())
public void beforeAdvice(JoinPoint joinPoint) {// 获取方法签名和参数MethodSignature signature (MethodSignature) joinPoint.getSignature();Method method signature.getMethod();Object[] args joinPoint.getArgs();// 打印方法信息和参数System.out.println(Before advice: method , args: Arrays.toString(args));
}AfterReturning(value servicePointcut(), returning result)
public void afterReturningAdvice(JoinPoint joinPoint, Object result) {// 获取方法签名和返回值MethodSignature signature (MethodSignature) joinPoint.getSignature();Method method signature.getMethod();// 打印方法信息和返回值System.out.println(After returning advice: method , result: result);
}AfterThrowing(value servicePointcut(), throwing ex)
public void afterThrowingAdvice(JoinPoint joinPoint, Exception ex) {// 获取方法签名和异常MethodSignature signature (MethodSignature) joinPoint.getSignature();Method method signature.getMethod();// 打印方法信息和异常System.out.println(After throwing advice: method , exception: ex.getMessage());
}Around(servicePointcut())
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {// 获取方法签名MethodSignature signature (MethodSignature) joinPoint.getSignature();Method method signature.getMethod();// 记录方法开始时间long startTime System.currentTimeMillis();// 执行切点方法Object result joinPoint.proceed();// 记录方法结束时间long endTime System.currentTimeMillis();// 计算方法耗时long duration endTime - startTime;// 打印方法信息和耗时System.out.println(Around advice: method , duration: duration ms);// 返回结果return result;
}4Order
这个注解用于指定切面类的优先级当有多个切面类作用于同一个切点时可以用这个注解来控制执行顺序。注解的值越小优先级越高。例如
Aspect
Component
Order(1)
public class LogAspect {// ...
}Aspect
Component
Order(2)
public class SecurityAspect {// ...
}这个注解表示LogAspect 的优先级高于 SecurityAspect因此 LogAspect 的通知方法会先于 SecurityAspect 的通知方法执行。
3. 验证注释
这些注解通常用于Java中的Bean ValidationJSR-380规范中用于对JavaBean属性进行验证
Valid、NotNull、Size、Min、Max、Email、Pattern
Valid用于指示在验证嵌套对象时应该递归执行验证。通常与复杂对象的属性一起使用以确保嵌套对象的所有属性都被验证。NotNull用于验证属性值不能为null。Size用于验证属性值的长度是否在指定范围内。Min用于验证属性值是否大于等于指定的最小值。Max用于验证属性值是否小于等于指定的最大值。Email用于验证属性值是否符合Email地址的格式。Pattern用于验证属性值是否匹配指定的正则表达式。
例如
public class Address {NotNullprivate String street;// 其他属性和方法
}public class User {Validprivate Address address;NotNullprivate String username;Size(min 2, max 50)private String username;Min(18)private int age;Max(100)private int age;Emailprivate String email;Pattern(regexp ^[A-Za-z0-9]$)private String username;// 其他属性和方法
}