Example usage for org.springframework.context.expression BeanFactoryResolver BeanFactoryResolver

List of usage examples for org.springframework.context.expression BeanFactoryResolver BeanFactoryResolver

Introduction

In this page you can find the example usage for org.springframework.context.expression BeanFactoryResolver BeanFactoryResolver.

Prototype

public BeanFactoryResolver(BeanFactory beanFactory) 

Source Link

Document

Create a new BeanFactoryResolver for the given factory.

Usage

From source file:de.hybris.platform.addonsupport.config.SpelBeanFactory.java

@Override
public void onApplicationEvent(final ContextRefreshedEvent event) {
    final ExpressionParser parser = new SpelExpressionParser();
    final StandardEvaluationContext context = new StandardEvaluationContext();
    context.setBeanResolver(new BeanFactoryResolver(applicationContext));

    final String[] beanNames = applicationContext.getBeanNamesForType(AbstractConverter.class);
    for (final String beanName : beanNames) {
        final BeanDefinition def = beanFactory.getBeanDefinition(beanName);
        final PropertyValue pv = def.getPropertyValues().getPropertyValue("targetClass");
        if (pv != null) {

            if (pv.getValue() instanceof TypedStringValue) {

                String expr = StringUtils.strip(((TypedStringValue) pv.getValue()).getValue());
                if (expr.startsWith("#{")) {
                    expr = StringUtils.replaceOnce(expr, "#{", "@");
                    expr = StringUtils.stripEnd(expr, "}");
                    final Object result = parser.parseExpression(expr).getValue(context);
                    if (result != null) {

                        try {
                            applicationContext.getBean(beanName, AbstractConverter.class)
                                    .setTargetClass(ClassUtils.getClass(result.toString()));
                        } catch (final ClassNotFoundException e) {
                            LOG.error(beanName + " target class instantiation failed", e);
                        }// www . ja  v  a2 s. com
                    }
                }
            }
        }
    }
}

From source file:com.couchbase.spring.core.mapping.BasicCouchbasePersistentEntity.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    context.addPropertyAccessor(new BeanFactoryAccessor());
    context.setBeanResolver(new BeanFactoryResolver(applicationContext));
    context.setRootObject(applicationContext);
}

From source file:org.gerzog.jstataggr.expressions.spel.SpelExpressionHandler.java

@PostConstruct
public void initialize() {
    final StandardEvaluationContext context = new StandardEvaluationContext();
    context.setBeanResolver(new BeanFactoryResolver(beanFactory));

    this.context = context;
    this.expressionParser = new SpelExpressionParser();
}

From source file:org.jasig.portlet.widget.service.SpringELProcessor.java

/**
 * @{inheritDoc}// w  ww.  j a va2 s.  c o  m
 */
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    this.beanResolver = new BeanFactoryResolver(beanFactory);
}

From source file:io.dyn.core.handler.AnnotationHandlerMethodResolver.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    if (null != applicationContext) {
        this.applicationContext = applicationContext;
        evalCtx.setBeanResolver(new BeanFactoryResolver(this.applicationContext));
    }//ww  w. j  a  v a 2s.c  o m
}

From source file:org.jasig.portlet.spring.SpringELProcessor.java

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    this.beanResolver = new BeanFactoryResolver(beanFactory);
}

From source file:io.dyn.core.handler.GuardedHandlerMethodInvoker.java

@Override
public Object invoke(HandlerMethod method, Object handler, EvaluationContext evalCtx, Object... args)
        throws Exception {
    if (null == evalCtx) {
        StandardEvaluationContext ec = new StandardEvaluationContext();
        if (null != applicationContext) {
            ec.setBeanResolver(new BeanFactoryResolver(applicationContext));
        }/* w w  w.  j  a va2 s .c om*/
        evalCtx = ec;
    }

    HandlerMethodArgument[] handlerMethodArguments = method.arguments();
    int argc = handlerMethodArguments.length;
    Object[] argv = new Object[argc];
    for (int i = 0; i < argc; i++) {
        HandlerMethodArgument handlerArg = handlerMethodArguments[i];
        if (ClassUtils.isAssignable(EvaluationContext.class, handlerArg.targetType())) {
            argv[i] = evalCtx;
        } else if (null != handlerArg.valueExpression()) {
            try {
                argv[i] = handlerArg.valueExpression().getValue(evalCtx, handlerArg.targetType());
            } catch (SpelEvaluationException e) {
                argv[i] = null;
            }
        } else {
            try {
                Object o = args[handlerArg.index()];
                if (ClassUtils.isAssignable(handlerArg.targetType(), o.getClass())) {
                    argv[i] = o;
                } else if (null != customConversionService
                        && customConversionService.canConvert(o.getClass(), handlerArg.targetType())) {
                    argv[i] = customConversionService.convert(o, handlerArg.targetType());
                } else {
                    argv[i] = Sys.DEFAULT_CONVERSION_SERVICE.convert(o, handlerArg.targetType());
                }
            } catch (IndexOutOfBoundsException e) {
            }
        }
        evalCtx.setVariable(handlerArg.name(), argv[i]);
    }

    InvocationHandler invoker = method.invocationHandler();
    Method m = method.methodToInvoke();
    if (null != method.guard()) {
        if (method.guard().checkGuard(null, evalCtx)) {
            if (null != invoker) {
                try {
                    invoker.invoke(handler, m, argv);
                } catch (Throwable throwable) {
                    throw new IllegalStateException(throwable);
                }
            } else {
                return m.invoke(handler, argv);
            }
        } else {
            //LOG.debug("Guard expression %s failed", method.guard().expression().getExpressionString());
        }
    } else {
        if (null != invoker) {
            try {
                return invoker.invoke(handler, m, argv);
            } catch (Throwable throwable) {
                throw new IllegalStateException(throwable);
            }
        } else {
            return m.invoke(handler, argv);
        }
    }

    return null;
}

From source file:com.frank.search.solr.core.mapping.SimpleSolrPersistentEntity.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

    context.addPropertyAccessor(new BeanFactoryAccessor());
    context.setBeanResolver(new BeanFactoryResolver(applicationContext));
    context.setRootObject(applicationContext);
}

From source file:app.web.InterpreterController.java

@RequestMapping("/interpreter/run")
public ModelAndView run(@RequestParam("script") String script) {
    Transaction transaction = Db.getSession().beginTransaction();
    // Groovy Shell
    //        Binding binding = new Binding();
    //        binding.setVariable("userService", userService); // FIXME: go through ApplicationContext or try to use SpEl instead?
    //        Object result = new GroovyShell(binding).parse(script).run();
    // Groovy Interpreter
    //        Object result = GroovyInterpreter.run(script);
    // SpEl/*from ww w .j av  a 2s  .  c o m*/
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.setBeanResolver(new BeanFactoryResolver(beanFactory));
    Object result = parser.parseExpression(script).getValue(ctx);
    //        Db.flush(); // can not flush here, we get org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    transaction.commit();

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("script", script);
    map.put("result", result != null ? result.toString().replaceAll("\n", "<br/>").replaceAll(" ", "&nbsp;")
            : Void.class.getSimpleName());
    return new ModelAndView("/interpreter", map);
}

From source file:reactor.spring.context.ConsumerBeanPostProcessor.java

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    beanResolver = new BeanFactoryResolver(beanFactory);
}