Search in sources :

Example 21 with FactoryBean

use of org.springframework.beans.factory.FactoryBean in project cas by apereo.

the class CasCoreWebConfiguration method urlValidator.

@Bean
public FactoryBean<UrlValidator> urlValidator() {
    final HttpClientProperties httpClient = this.casProperties.getHttpClient();
    final boolean allowLocalLogoutUrls = httpClient.isAllowLocalLogoutUrls();
    final String authorityValidationRegEx = httpClient.getAuthorityValidationRegEx();
    final boolean authorityValidationRegExCaseSensitive = httpClient.isAuthorityValidationRegExCaseSensitive();
    return new SimpleUrlValidatorFactoryBean(allowLocalLogoutUrls, authorityValidationRegEx, authorityValidationRegExCaseSensitive);
}
Also used : SimpleUrlValidatorFactoryBean(org.apereo.cas.web.SimpleUrlValidatorFactoryBean) HttpClientProperties(org.apereo.cas.configuration.model.core.authentication.HttpClientProperties) PropertiesFactoryBean(org.springframework.beans.factory.config.PropertiesFactoryBean) FactoryBean(org.springframework.beans.factory.FactoryBean) SimpleUrlValidatorFactoryBean(org.apereo.cas.web.SimpleUrlValidatorFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 22 with FactoryBean

use of org.springframework.beans.factory.FactoryBean in project cas by apereo.

the class CasCoreHttpConfiguration method httpClient.

@ConditionalOnMissingBean(name = "httpClient")
@Bean
public FactoryBean<SimpleHttpClient> httpClient() {
    final SimpleHttpClientFactoryBean.DefaultHttpClient c = new SimpleHttpClientFactoryBean.DefaultHttpClient();
    final HttpClientProperties httpClient = casProperties.getHttpClient();
    c.setConnectionTimeout(Beans.newDuration(httpClient.getConnectionTimeout()).toMillis());
    c.setReadTimeout((int) Beans.newDuration(httpClient.getReadTimeout()).toMillis());
    return c;
}
Also used : SimpleHttpClientFactoryBean(org.apereo.cas.util.http.SimpleHttpClientFactoryBean) HttpClientProperties(org.apereo.cas.configuration.model.core.authentication.HttpClientProperties) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) FactoryBean(org.springframework.beans.factory.FactoryBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) SimpleHttpClientFactoryBean(org.apereo.cas.util.http.SimpleHttpClientFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 23 with FactoryBean

use of org.springframework.beans.factory.FactoryBean in project geronimo-xbean by apache.

the class NamedConstructorArgs method processParameters.

public void processParameters(BeanDefinitionHolder definitionHolder, MappingMetaData metadata) throws BeansException {
    // this only works if we have an abstsract bean definition
    if (!(definitionHolder.getBeanDefinition() instanceof AbstractBeanDefinition)) {
        return;
    }
    AbstractBeanDefinition beanDefinition = (AbstractBeanDefinition) definitionHolder.getBeanDefinition();
    ConstructorArgumentValues constructorArgumentValues = beanDefinition.getConstructorArgumentValues();
    // if this bean already has constructor arguments defined, don't mess with them
    if (constructorArgumentValues.getArgumentCount() > 0) {
        return;
    }
    // try to get a list of constructor arg names to use
    ConstructionInfo constructionInfo = selectConstructionMethod(beanDefinition, metadata);
    if (constructionInfo == null) {
        return;
    }
    // remove each named property and add an indexed constructor arg
    MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
    String[] parameterNames = constructionInfo.parameterNames;
    Class[] parameterTypes = constructionInfo.parameterTypes;
    for (int i = 0; i < parameterNames.length; i++) {
        String parameterName = parameterNames[i];
        Class parameterType = parameterTypes[i];
        PropertyValue propertyValue = propertyValues.getPropertyValue(parameterName);
        if (propertyValue != null) {
            propertyValues.removePropertyValue(parameterName);
            constructorArgumentValues.addIndexedArgumentValue(i, propertyValue.getValue(), parameterType.getName());
        } else {
            Object defaultValue = defaultValues.get(new PropertyKey(parameterName, parameterType));
            if (defaultValue == null) {
                defaultValue = DEFAULT_VALUE.get(parameterType);
            }
            if (defaultValue instanceof FactoryBean) {
                try {
                    defaultValue = ((FactoryBean) defaultValue).getObject();
                } catch (Exception e) {
                    throw new FatalBeanException("Unable to get object value from bean factory", e);
                }
            }
            constructorArgumentValues.addIndexedArgumentValue(i, defaultValue, parameterType.getName());
        }
    }
// todo set any usable default values on the bean definition
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) PropertyValue(org.springframework.beans.PropertyValue) FatalBeanException(org.springframework.beans.FatalBeanException) BeansException(org.springframework.beans.BeansException) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) FatalBeanException(org.springframework.beans.FatalBeanException) FactoryBean(org.springframework.beans.factory.FactoryBean)

Example 24 with FactoryBean

use of org.springframework.beans.factory.FactoryBean in project cxf by apache.

the class AbstractFactoryBeanDefinitionParser method doParse.

@Override
protected void doParse(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
    Class<?> factoryClass = getFactoryClass();
    BeanDefinitionBuilder factoryBean = bean;
    if (!FactoryBean.class.isAssignableFrom(factoryClass)) {
        factoryBean = BeanDefinitionBuilder.rootBeanDefinition(getFactoryClass());
    }
    NamedNodeMap atts = element.getAttributes();
    boolean createdFromAPI = false;
    boolean setBus = false;
    for (int i = 0; i < atts.getLength(); i++) {
        Attr node = (Attr) atts.item(i);
        String val = node.getValue();
        String pre = node.getPrefix();
        String name = node.getLocalName();
        if ("createdFromAPI".equals(name)) {
            factoryBean.setAbstract(true);
            bean.setAbstract(true);
            createdFromAPI = true;
        } else if ("abstract".equals(name)) {
            factoryBean.setAbstract(true);
            bean.setAbstract(true);
        } else if ("depends-on".equals(name)) {
            factoryBean.addDependsOn(val);
            bean.addDependsOn(val);
        } else if (!"id".equals(name) && !"name".equals(name) && isAttribute(pre, name)) {
            if ("bus".equals(name)) {
                setBus = true;
                if (!val.startsWith("#")) {
                    // bus attributes always need to be a reference
                    val = "#" + val;
                }
            }
            mapAttribute(factoryBean, element, name, val);
        }
    }
    if (!setBus) {
        addBusWiringAttribute(factoryBean, BusWiringType.PROPERTY);
    }
    Node node = element.getFirstChild();
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            String name = node.getLocalName();
            mapElement(ctx, factoryBean, (Element) node, name);
        }
        node = node.getNextSibling();
    }
    String id = getIdOrName(element);
    BeanDefinition container = ctx.getContainingBeanDefinition();
    boolean noFactory = false;
    if (StringUtils.isEmpty(id)) {
        if (container == null) {
            id = BeanDefinitionReaderUtils.generateBeanName(bean.getBeanDefinition(), ctx.getRegistry(), false);
        } else {
            id = BeanDefinitionReaderUtils.generateBeanName(bean.getBeanDefinition(), ctx.getRegistry(), true);
            noFactory = true;
        // inner bean, no need for the factory to be public at all
        }
    }
    if (createdFromAPI) {
        id = id + getSuffix();
    }
    if (FactoryBean.class.isAssignableFrom(getFactoryClass())) {
        if (!noFactory) {
            AbstractBeanDefinition def = factoryBean.getRawBeanDefinition().cloneBeanDefinition();
            def.setBeanClass(getRawFactoryClass());
            def.setAbstract(factoriesAreAbstract);
            def.setLazyInit(true);
            ctx.getRegistry().registerBeanDefinition(id + getFactoryIdSuffix(), def);
        }
        bean.getBeanDefinition().setAttribute("id", id);
    } else {
        String factoryId = id + getFactoryIdSuffix();
        ctx.getRegistry().registerBeanDefinition(factoryId, factoryBean.getBeanDefinition());
        bean.getRawBeanDefinition().setAttribute("id", id);
        bean.getRawBeanDefinition().setFactoryBeanName(factoryId);
        bean.getRawBeanDefinition().setFactoryMethodName("create");
    }
    if (getDestroyMethod() != null) {
        bean.setDestroyMethodName(getDestroyMethod());
    }
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) NamedNodeMap(org.w3c.dom.NamedNodeMap) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) Node(org.w3c.dom.Node) FactoryBean(org.springframework.beans.factory.FactoryBean) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) Attr(org.w3c.dom.Attr)

Example 25 with FactoryBean

use of org.springframework.beans.factory.FactoryBean in project tutorials by eugenp.

the class JmsClient method invoker.

@Bean
FactoryBean invoker(ConnectionFactory factory, Queue queue) {
    JmsInvokerProxyFactoryBean factoryBean = new JmsInvokerProxyFactoryBean();
    factoryBean.setConnectionFactory(factory);
    factoryBean.setServiceInterface(CabBookingService.class);
    factoryBean.setQueue(queue);
    return factoryBean;
}
Also used : JmsInvokerProxyFactoryBean(org.springframework.jms.remoting.JmsInvokerProxyFactoryBean) FactoryBean(org.springframework.beans.factory.FactoryBean) Bean(org.springframework.context.annotation.Bean) JmsInvokerProxyFactoryBean(org.springframework.jms.remoting.JmsInvokerProxyFactoryBean)

Aggregations

FactoryBean (org.springframework.beans.factory.FactoryBean)27 Test (org.junit.jupiter.api.Test)10 SmartFactoryBean (org.springframework.beans.factory.SmartFactoryBean)7 TestBean (org.springframework.beans.testfixture.beans.TestBean)6 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)5 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)5 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)5 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)5 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)4 BeanFactory (org.springframework.beans.factory.BeanFactory)4 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)4 Bean (org.springframework.context.annotation.Bean)4 BeansException (org.springframework.beans.BeansException)3 GroovyObject (groovy.lang.GroovyObject)2 PrivilegedAction (java.security.PrivilegedAction)2 ArrayList (java.util.ArrayList)2 HttpClientProperties (org.apereo.cas.configuration.model.core.authentication.HttpClientProperties)2 Test (org.junit.Test)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 SmartInitializingSingleton (org.springframework.beans.factory.SmartInitializingSingleton)2