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();
    }

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值