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

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);



    }
}

 

你可能感兴趣的:(spring)