0
点赞
收藏
分享

微信扫一扫

直接使用spel表达式操作AnnotationConfigApplicationContext里的对象

雅典娜的棒槌 2022-08-18 阅读 40


package com.alibaba.aop;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan("com")
@EnableAspectJAutoProxy
public class AopConfig {

}


package com.alibaba.aop;


import org.springframework.stereotype.Service;

@Service
public class PersonService {


public String hello(){
return "adf";
}
}




package com.alibaba.aop;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class App {
public static void main( String[] args ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);

ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext mycontext = new StandardEvaluationContext();
mycontext.setBeanResolver(new BeanFactoryResolver(context));


Object str = parser.parseExpression("@personService.hello()").getValue(mycontext);
System.out.println(str);



}
}

 

举报

相关推荐

0 条评论