Spring boot 自定义注解

本文介绍如何在SpringBoot项目中创建并使用自定义注解。通过定义注解、配置切面及应用到业务逻辑中,展示了自定义注解在AOP编程中的具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      相信很多学习Spring框架初学者都会碰到各种注解比如@Controller , @Response,@Compent等各类注解,如何在Spring boot中增加自己的注解,是一个比较基础的话题,也是进入AOP编程大门必经之路。

首先定义一个@Annotation的注解接口如下:

package com.AnnotationTest;


import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;   


@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SelfAnnotation {
 String desc() default "Hi Call";
}

第二部定义切面

package com.AnnotationTest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class Logging {
@Pointcut("@annotation(com.AnnotationTest.SelfAnnotation)")
private void cut(){}

@Before("cut()") 
public void BeforeCall()
{
 System.out.println("事前通知");
}



@Around("cut()")  
public void AroundCall(ProceedingJoinPoint joinPoint)
{
 System.out.println("环绕通知之开始");
  try {
        joinPoint.proceed();
  } catch (Throwable e) {
        e.printStackTrace();
  }
  System.out.println("环绕通知之结束");
}

@After("cut()")
public void AfterCall()
{
 System.out.println("事后通知");
}

}

第三部定义一个控制器,

package com.AnnotationTest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

//@Scheduled(fixedRate=5000)
void TimeCall()

{

callMyRoute();

}

@RequestMapping("/index")
public String index() {

runner.Run();
return "this is my index";
}
/*
@SelfAnnotation
public void callMyRoute()
{
System.out.println("MyController is callMyRoute");
}*/


@Autowired
private TestRunner runner;

}

大家注意到红色部分字体,代码,这部分如果使用,使用自定义注解,这个切面是否能生效。

第四部 我们再定义一个注解使用类


package com.AnnotationTest;


import org.springframework.stereotype.Service;


@Service
public class TestRunner {
@SelfAnnotation
public void Run()
{
System.out.println("MyController is Run");
}

}

第五步定义主函数使用Spring boot 启动程序

package com.AnnotationTest;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;


@SpringBootApplication
@EnableScheduling
public class SelfAnnotationTestApplication {


public static void main(String[] args) {
SpringApplication.run(SelfAnnotationTestApplication.class, args);
}
}

运行结果可以


环绕通知之开始
事前通知
MyController is Run
环绕通知之结束
事后通知

但是,其实控制器中定时任务中注解并没有生效。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值