首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义注释不适用于setter

自定义注释不适用于setter
EN

Stack Overflow用户
提问于 2020-03-25 08:35:29
回答 1查看 882关注 0票数 0

我需要将模型类字段设置为加密。我采用的方法是创建一个自定义注释。无论什么地方,我都会有注释,它说我会加密它的价值。

目标是创建一个字段/方法注释。如果它是一个方法注释,那么我将注释Setter。

我已经编写了代码,但它不起作用。帮帮忙吧。

我想加密工资的Pojo类。

代码语言:javascript
复制
package com.example.springaop.model;

import com.example.springaop.customannotation.CustomAnnotation;
import com.example.springaop.customannotation.Encryptvalue;

public class Employee {

    private String empId;
    private String name;
    private int salary;

    public String getName() {
        return name;
    }

    @CustomAnnotation
    public void setName(String name) {
        this.name = name;
    }

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

    public int getSalary() {
        return salary;
    }
    @Encryptvalue
    public void setSalary(int salary) {
        this.salary = salary;
    }


}

自定义注释

代码语言:javascript
复制
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Encryptvalue {

}

类EmployeeController

代码语言:javascript
复制
package com.example.springaop.controller;

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

import com.example.springaop.customannotation.LogExecutionTime;
import com.example.springaop.model.Employee;
import com.example.springaop.service.EmployeeService;


@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/add/employee", method = RequestMethod.GET)
    @LogExecutionTime
    public Employee addEmployee(@RequestParam("name") String name, @RequestParam("empId") String empId) throws InterruptedException {
        return employeeService.createEmployee(name, empId,1000);
    }

    @RequestMapping(value = "/remove/employee", method = RequestMethod.GET)
    public String removeEmployee( @RequestParam("empId") String empId) {

        employeeService.deleteEmployee(empId);

        return "Employee removed";
    }

}

类EmployeeService

代码语言:javascript
复制
package com.example.springaop.service;


import org.springframework.stereotype.Service;

import com.example.springaop.customannotation.CustomAnnotation;
import com.example.springaop.model.Employee;


@Service
public class EmployeeService {

    @CustomAnnotation
    public Employee createEmployee(String name, String empId, int salary) {
        Employee emp = new Employee();
        emp.setName(name);
        emp.setEmpId(empId);
        emp.setSalary(salary);
        return emp;
    }

    public void deleteEmployee(String empId) {

    }
}

类EmployeeServiceAspect

代码语言:javascript
复制
package com.example.springaop.aspect;

import org.aspectj.lang.JoinPoint;
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.springframework.stereotype.Component;

@Aspect
@Component
public class EmployeeServiceAspect {

    @Before(value = "execution(* com.example.springaop.service.EmployeeService.*(..)) and args(name,empId)")
    public void beforeAdvice(JoinPoint joinPoint, String name, String empId) {
        System.out.println("Before method:" + joinPoint.getSignature());

        System.out.println("Creating Employee with name - " + name + " and id - " + empId);
    }

    @After(value = "execution(* com.example.springaop.service.EmployeeService.*(..)) and args(name,empId)")
    public void afterAdvice(JoinPoint joinPoint, String name, String empId) {
        System.out.println("After method:" + joinPoint.getSignature());

        System.out.println("Successfully created Employee with name - " + name + " and id - " + empId);
    }

    @Around("@annotation(com.example.springaop.customannotation.LogExecutionTime)")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        long executionTime = System.currentTimeMillis() - start;
        System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
        return proceed;
    }


    @Around("@annotation(com.example.springaop.customannotation.CustomAnnotation)")
    public Object customAnnotation(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = joinPoint.proceed();
        System.out.println(joinPoint.getSignature() + "############## Executed customAnnotation #################");
        return proceed;
    }

    @Around("@annotation(com.example.springaop.customannotation.Encryptvalue)")
    public Object Encryptvalue(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = joinPoint.proceed();
        System.out.println(joinPoint.getSignature() + "############## Executed Encryptvalue Annotation #################");
        return proceed;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-25 12:43:42

为什么这个方面不能工作?

  1. Employee实例不是Spring。春天AOP只能给豆子提建议。请阅读参考文献
  2. @Before@After点剪切表达式拦截方法调用是不正确的。
  3. Encryptvalue被定义为@Target(ElementType.FIELD),示例将用法显示为@Target(ElementType.METHOD)。理想情况下,代码不应该编译。

如果可以将Employee类变成Spring (在本例中是一个作用域bean ),则基于注释的@Around建议将按预期工作。类似地,按照下面的方式修改Pointcut表达式应该会拦截EmployeeService.createEmployee()方法调用。若要通知EmployeeService的所有方法调用,请删除切入点表达式的and args(name,empId)部分。

例子:

代码语言:javascript
复制
@After(value = "execution(* com.example.springaop.service.EmployeeService.*(..)) and args(name,empId,salary)")
public void afterAdvice(JoinPoint joinPoint, String name, String empId,int salary) {..}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60845264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档