Spring 配置AOP切面注解报错,求教大虾

Spring 配置AOP切面注解报错,求教大虾,第1张

有配置的,代码示例如下:

这是业务测试类:

package aopannotationservice;

import orgspringframeworkcontextannotationScope;

import orgspringframeworkstereotypeService;

@Service("deptSerivceImpl")

@Scope("prototype")

public class DeptSerivceImpl implements DeptService {

public DeptSerivceImpl(){}

public void delete() {

try {

Threadsleep(200);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

Systemoutprintln("删除部门");

}

public void save() {

try {

Threadsleep(500);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

Systemoutprintln("保存部门");

}

}

这个是切面测试类:

package aopannotationaspect;

import orgaspectjlangProceedingJoinPoint;

import orgaspectjlangannotationAround;

import orgaspectjlangannotationAspect;

import orgaspectjlangannotationBefore;

import orgaspectjlangannotationPointcut;

import orgspringframeworkstereotypeComponent;

import orgspringframeworkutilStopWatch;

@Aspect

@Component("timeHander")

public class TimeHander {

@Pointcut("bean (Service)")

public void myPointCut(){};

@Before("maPointCut()")

public void myBefore(){

Systemoutprintln("-----执行前置处理-------");

}

@Around("myPointCut()")

public Object handerTime(ProceedingJoinPoint pjp){

try {

// 开始计时

StopWatch watch=new StopWatch(pjpgetTarget()getClass()getName());

watchstart(pjpgetSignature()getName());

Object obj=pjpproceed();

// 停止计时

watchstop();

Systemoutprintln(watchprettyPrint());

return obj;

} catch (Throwable e) {

// TODO Auto-generated catch block

eprintStackTrace();

return null;

}//执行目标

}

}

@PathVariable,@RequestParam,@RequestBody

1针对一些非必填的参数,可以使用required关键字来标识,同时必须设置默认值defaultValue,如getOrder方法中对price参数的获取:

@RequestParam(value = "price",required = false,defaultValue = "0") Integer price

2参数可以不配注解直接与Entity类绑定,但不支持json格式,只支持form-data和x- >

《spring常用的注解》百度网盘资源下载 ida4   

注解本身没有功能的,就和 xml 一样。注解和 xml 都是一种元数据,元数据即解释数据的数据,这就是所谓配置。本文主要罗列 Spring|Spring MVC相关注解的简介。

写在前面:项目所有的>

private void beforeTransationHandle(JoinPoint point) throws Exception{

//拦截的实体类

Object target = pointgetTarget();

//拦截的方法名称

String methodName = pointgetSignature()getName();

//拦截的方法参数

Object[] args = pointgetArgs();

//拦截的放参数类型

Class[] parameterTypes = ((MethodSignature)pointgetSignature())getMethod()getParameterTypes();

Method m = null;

try {

//通过反射获得拦截的method

m = targetgetClass()getMethod(methodName, parameterTypes);

//如果是桥则要获得实际拦截的method

if(misBridge()){

for(int i = 0; i < argslength; i++){

//获得泛型类型

Class genClazz = GenericsUtilsgetSuperClassGenricType(targetgetClass());

//根据实际参数类型替换parameterType中的类型

if(args[i]getClass()isAssignableFrom(genClazz)){

parameterTypes[i] = genClazz;

}

}

//获得parameterType参数类型的方法

m = targetgetClass()getMethod(methodName, parameterTypes);

}

} catch (SecurityException e) {

eprintStackTrace();

} catch (NoSuchMethodException e) {

eprintStackTrace();

}

}

spring中给引用类型赋值的注解

1、@Autowired: spring框架提供的注解,实现引用类型的赋值。

2、@Resource: 来自jdk中的注解,spring框架提供了对这个注解的功能支持,可以使用它给引用类型赋值;

注意:高于JDK18版本的如果想使用@Resource注解需要添加一个依赖

1定义自定义枚举@EnumValidator

2定义实现枚举值校验处理类EnumValidatorClass

1定义要校验的枚举值

2在相应要检验的字段加上@EnumValidator注解

2controller层正常使用、无需添加任何东西

1入参不再枚举值中

2入参在枚举值中

gitee源码:

>

不用配置xml,直接java代码实现,参考代码如下:public class GetApplicationContext {private static class ApplicationContextHolder {// 单例变量private static ApplicationContext AC = new FileSystemXmlApplicationContext("classpath:applicationContextxml");}// 私有化的构造方法,保证外部的类不能通过构造器来实例化。private GetApplicationContext() {}// 获取单例对象实例public static ApplicationContext getInstance() {if (ApplicationContextHolderAC == null) {ApplicationContextHolderAC = new FileSystemXmlApplicationContext("classpath:applicationContextxml");}return ApplicationContextHolderAC;}}获取所有spring自动装配的bean://获取spring装配的bean个数GetApplicationContextgetInstance()getBeanDefinitionNames()length;//逐个打印出spring自动装配的bean。根据我的测试,类名第一个字母小写即bean的名字for(int i=0;i<33;i++){Systemoutprintln( GetApplicationContextgetInstance()getBeanDefinitionNames()[i]);}然后通过下面的代码获取到spring注解装配的bean供自己使用:StorageReturnService ossService = (StorageReturnService) GetApplicationContextgetInstance()getBean("storageReturnServiceImpl");怎样获取 spring 已经注解的bean

以上就是关于Spring 配置AOP切面注解报错,求教大虾全部的内容,包括:Spring 配置AOP切面注解报错,求教大虾、springboot controller获取请求参数的注解、spring常用的注解有哪些等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10155423.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存