当前位置: 首页 > article >正文

spring框架注解

3.Spring有哪些常用注解呢?
Spring常用注解

Web:

@Controller:组合注解(组合了@Component注解),应用在MVC层(控制层)。

@RestController:该注解为一个组合注解,相当于@Controller和@ResponseBody的组合,注解在类上,意味着,该Controller的所有方法都默认加上了@ResponseBody。

@RequestMapping:用于映射Web请求,包括访问路径和参数。如果是Restful风格接口,还可以根据请求类型使用不同的注解:

@GetMapping

@PostMapping

@PutMapping

@DeleteMapping

@ResponseBody:支持将返回值放在response内,而不是一个页面,通常用户返回json数据。

@RequestBody:允许request的参数在request体中,而不是在直接连接在地址后面。

@PathVariable:用于接收路径参数,比如@RequestMapping(“/hello/{name}”)申明的路径,将注解放在参数中前,即可获取该值,通常作为Restful的接口实现方法。

@RestController:该注解为一个组合注解,相当于@Controller和@ResponseBody的组合,注解在类上,意味着,该Controller的所有方法都默认加上了@ResponseBody。

容器:

@Component:表示一个带注释的类是一个“组件”,成为Spring管理的Bean。当使用基于注解的配置和类路径扫描时,这些类被视为自动检测的候选对象。同时@Component还是一个元注解。

@Service:组合注解(组合了@Component注解),应用在service层(业务逻辑层)。

@Repository:组合注解(组合了@Component注解),应用在dao层(数据访问层)。

@Autowired:Spring提供的工具(由Spring的依赖注入工具(BeanPostProcessor、BeanFactoryPostProcessor)自动注入)。

@Qualifier:该注解通常跟 @Autowired 一起使用,当想对注入的过程做更多的控制,@Qualifier 可帮助配置,比如两个以上相同类型的 Bean 时 Spring 无法抉择,用到此注解

@Configuration:声明当前类是一个配置类(相当于一个Spring配置的xml文件)

@Value:可用在字段,构造器参数跟方法参数,指定一个默认值,支持 #{} 跟 ${} 两个方式。一般将 SpringbBoot 中的 application.properties 配置的属性值赋值给变量。

@Bean:注解在方法上,声明当前方法的返回值为一个Bean。返回的Bean对应的类中可以定义init()方法和destroy()方法,然后在@Bean(initMethod=”init”,destroyMethod=”destroy”)定义,在构造之后执行init,在销毁之前执行destroy。

@Scope:定义我们采用什么模式去创建Bean(方法上,得有@Bean) 其设置类型包括:Singleton 、Prototype、Request 、 Session、GlobalSession。

实体类和接口

@Component
@Aspect
public class Loger {
   @Before("execution(* *..BookServiceimpl.*(..))")
    public void check(){ System.out.println("前置通知/增强:执行系统的权限验证"); }
    @AfterReturning("execution(* *..BookServiceimpl.*(..))")
    public void logPrint(){ System.out.println("后置通知/增强:执行日志的打印"); }
    @AfterThrowing("execution(* *..BookServiceimpl.*(..))")
    public void exeption(){ System.out.println("异常通知/增强:做出异常的通知"); }
    @After("execution(* *..BookServiceimpl.*(..))")
    public void distory(){ System.out.println("最终通知/增强:资源的释放"); }

public interface BookService {
    void add();
    void del();
    void update();
    void find();
@Component
public class BookServiceimpl implements BookService {
    @Override
    public void add() {
        System.out.println("添加");
    }

    @Override
    public void del() {
        System.out.println("删除");
    }

    @Override
    public void update() {
        System.out.println("修改");
    }

    @Override
    public void find() {
        System.out.println("查询");
    }
}

spring。xml代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:component-scan base-package="com.lei"/>
    <aop:aspectj-autoproxy/>

</beans>

测试类

   @Test
    public void test01(){
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        BookService bookService=context.getBean(BookService.class);
        bookService.add();
        bookService.del();
        bookService.find();
        bookService.update();
    }


http://www.kler.cn/news/10000.html

相关文章:

  • 半个月狂飙1000亿,ChatGPT概念股凭什么?
  • 7.Easyexcel的使用
  • Mac下Python3安装及基于Idea开发
  • 如何面对人生困境至暗时刻
  • 2023年数据挖掘与知识发现国际会议(DMKD 2023) | IOP JPCS独立出版
  • Python 小型项目大全 21~25
  • 一篇搞定Lambda和Stream流
  • 最新CAMx-Python融合技术应用与大气污染来源解析方法应用
  • MongoDB 介绍和基本操作
  • 【设计模式】常用的几种设计模式——单例模式
  • RuoYi-Vue Pro框架学习启动教程
  • 【数据结构】排序习题
  • Vue学习-Vue入门
  • 【MySQL学习】认识MySQL数据库
  • ObjectBox一种基于中心点的无锚点目标检测方法
  • 「UG/NX」Block UI 快速定位ORentXpress
  • 4.10日报
  • 10个镜像网站工具箱供你使用,不注册ChatGPT也能免费使用ChatGPT
  • 2023美团春招4.8 后端真题和解析 第三题:水果打包
  • Servlet入门讲解
  • Python 小型项目大全 26~30
  • 一位27岁软件测试员,测试在职近5年,月薪不到2W,担心被应届生取代
  • keep-alive 和 router-view 的使用方法(Vue3)
  • 怎样才能进有数据、有技术的公司?
  • 3.1 微分中值定理
  • leetcode 139.单词拆分
  • 无重复全排列 [2*+]
  • tpm2-tools源码分析之tpm2_load.c(6)
  • 【二分汇总】
  • 全国青少年信息素养大赛2023年python·选做题模拟二卷