AOP Method
AOP Method
allows us to extend the logic directly on the Class Method through decorators
Create AOP Method
For example, we create a AOP Method log
in the module demo-student to output the execution time of method
1. Cli command
$ vona :create:bean aopMethod log --module=demo-student
2. Menu command
TIP
Context Menu - [Module Path]: Vona Aspect/AOP Method
AOP Method Definition
export interface IAopMethodOptionsLog extends IDecoratorAopMethodOptions {}
@AopMethod<IAopMethodOptionsLog>()
class AopMethodLog {
async execute(_options: IAopMethodOptionsLog, _args: [], next: Next | NextSync, _receiver: any, _prop: string): Promise<any> {
const timeBegin = Date.now();
const res = await next();
const timeEnd = Date.now();
console.log('time:', timeEnd - timeBegin);
return res;
}
}
IAopMethodOptionsLog
: Defines aop method parametersexecute
: Outputs execution time
Using AOP Method
1. Annotating controller actions
import { Aspect } from 'vona-module-a-aspect';
@Controller()
class ControllerStudent {
@Web.get()
+ @Aspect.aopMethod('demo-student:log')
async findMany() {}
}
@Aspect.aopMethod
: This decorator is used to use AOP Method. Simply pass the AOP Method name- The log AOP Method belongs to the
demo-student
module, so the full name isdemo-student:log
- The log AOP Method belongs to the
2. Annotating service methods
import { Aspect } from 'vona-module-a-aspect';
@Service()
class ServiceStudent {
+ @Aspect.aopMethod('demo-student:log')
async findMany(){}
AOP Method Parameters
You can define parameters for AOP Method, allowing for more flexible configuration of AOP Method logic
For example, define the prefix
parameter for the log AOP Method to control the output format
1. Defining parameter types
export interface IAopMethodOptionsLog extends IDecoratorAopMethodOptions {
+ prefix: string;
}
2. Providing default values for parameters
@AopMethod<IAopMethodOptionsLog>({
+ prefix: 'time',
})
3. Using Parameters
export interface IAopMethodOptionsLog extends IDecoratorAopMethodOptions {
prefix: string;
}
@AopMethod<IAopMethodOptionsLog>({
prefix: 'time',
})
class AopMethodLog {
async execute(options: IAopMethodOptionsLog, _args: [], next: Next | NextSync, _receiver: any, _prop: string): Promise<any> {
const timeBegin = Date.now();
const res = await next();
const timeEnd = Date.now();
- console.log('time:', timeEnd - timeBegin);
+ console.log(`${options.prefix}:`, timeEnd - timeBegin);
return res;
}
}
4. Specify parameters when using
You can specify AOP Method parameters for any specific Class Method
class ControllerStudent {
@Web.get()
+ @Aspect.aopMethod('demo-student:log', { prefix: 'elapsed' })
async findMany() {}
}
- When using AOP Method, just provide the parameter value directly
5. App config
AOP Method parameters can be configured in App config
src/backend/config/config/config.ts
// onions
config.onions = {
aopMethod: {
'demo-student:log': {
prefix: 'elapsed',
},
},
};
6. Parameter precedence
Specify parameters when using
> App config
> Default values
AOP Method enable/disable
You can control enable/disable
of AOP Method
1. Enable
- Disable for an Class Method
class ControllerStudent {
@Web.get()
+ @Aspect.aopMethod('demo-student:log', { enable: false })
async findMany() {}
}
- Disable for Class Methods
src/backend/config/config/config.ts
// onions
config.onions = {
aopMethod: {
'demo-student:log': {
+ enable: false,
},
},
};
2. Meta
Allows AOP Method to take effect in a specified operating environment
Name | Type | Description |
---|---|---|
flavor | string|string[] | See: Runtime Environments and Flavors |
mode | string|string[] | See: Runtime Environments and Flavors |
- Example
@AopMethod({
+ meta: {
+ flavor: 'normal',
+ mode: 'dev',
+ },
})
class AopMethodLog {}