Search in sources :

Example 41 with Primary

use of org.springframework.context.annotation.Primary in project kylo by Teradata.

the class LivyWranglerConfig method sparkConf.

/**
 * Creates the Spark configuration.
 *
 * @return the Spark configuration
 */
@Bean
@Primary
public SparkConf sparkConf(final Environment env) /*, @Qualifier("sparkShellPort") final int serverPort*/
{
    final SparkConf conf = new SparkConf().setAppName("SparkShellServer");
    // .set("spark.ui.port", Integer.toString(serverPort + 1));
    final Iterable<Map.Entry<String, Object>> properties = FluentIterable.from(Collections.singleton(env)).filter(AbstractEnvironment.class).transformAndConcat(new Function<AbstractEnvironment, Iterable<?>>() {

        @Nullable
        @Override
        public Iterable<?> apply(@Nullable final AbstractEnvironment input) {
            return (input != null) ? input.getPropertySources() : null;
        }
    }).filter(ResourcePropertySource.class).transform(new Function<ResourcePropertySource, Map<String, Object>>() {

        @Nullable
        @Override
        public Map<String, Object> apply(@Nullable final ResourcePropertySource input) {
            return (input != null) ? input.getSource() : null;
        }
    }).transformAndConcat(new Function<Map<String, Object>, Iterable<Map.Entry<String, Object>>>() {

        @Nullable
        @Override
        public Iterable<Map.Entry<String, Object>> apply(@Nullable final Map<String, Object> input) {
            return (input != null) ? input.entrySet() : null;
        }
    }).filter(new Predicate<Map.Entry<String, Object>>() {

        @Override
        public boolean apply(@Nullable final Map.Entry<String, Object> input) {
            return (input != null && input.getKey().startsWith("spark."));
        }
    });
    for (final Map.Entry<String, Object> entry : properties) {
        conf.set(entry.getKey(), entry.getValue().toString());
    }
    return conf;
}
Also used : FluentIterable(com.google.common.collect.FluentIterable) AbstractEnvironment(org.springframework.core.env.AbstractEnvironment) Function(com.google.common.base.Function) ResourcePropertySource(org.springframework.core.io.support.ResourcePropertySource) SparkConf(org.apache.spark.SparkConf) Map(java.util.Map) Nullable(javax.annotation.Nullable) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Example 42 with Primary

use of org.springframework.context.annotation.Primary in project leopard by tanhaichao.

the class LeopardPropertyPlaceholderConfigurer method getSingleBean.

public static <T> T getSingleBean(BeanFactory beanFactory, Class<T> requiredType) throws BeansException {
    DefaultListableBeanFactory factory = (DefaultListableBeanFactory) beanFactory;
    Map<String, T> matchingBeans = factory.getBeansOfType(requiredType);
    if (matchingBeans.isEmpty()) {
        throw new NoSuchBeanDefinitionException(requiredType);
    }
    if (matchingBeans.size() == 1) {
        return matchingBeans.entrySet().iterator().next().getValue();
    }
    for (Entry<String, T> entry : matchingBeans.entrySet()) {
        T bean = entry.getValue();
        // TODO 还没有支持Bean有AOP
        Primary primary = bean.getClass().getDeclaredAnnotation(Primary.class);
        if (primary != null) {
            return bean;
        }
    }
    throw new NoUniqueBeanDefinitionException(requiredType, matchingBeans.keySet());
}
Also used : Primary(org.springframework.context.annotation.Primary) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) NoUniqueBeanDefinitionException(org.springframework.beans.factory.NoUniqueBeanDefinitionException)

Example 43 with Primary

use of org.springframework.context.annotation.Primary in project leopard by tanhaichao.

the class LeopardBeanFactoryAware method getSingleBean.

public static <T> T getSingleBean(BeanFactory beanFactory, Class<T> requiredType) throws BeansException {
    DefaultListableBeanFactory factory = (DefaultListableBeanFactory) beanFactory;
    Map<String, T> matchingBeans = factory.getBeansOfType(requiredType);
    if (matchingBeans.isEmpty()) {
        throw new NoSuchBeanDefinitionException(requiredType);
    }
    if (matchingBeans.size() == 1) {
        return matchingBeans.entrySet().iterator().next().getValue();
    }
    for (Entry<String, T> entry : matchingBeans.entrySet()) {
        T bean = entry.getValue();
        // TODO 还没有支持Bean有AOP
        Primary primary = bean.getClass().getDeclaredAnnotation(Primary.class);
        if (primary != null) {
            return bean;
        }
    }
    throw new NoUniqueBeanDefinitionException(requiredType, matchingBeans.keySet());
}
Also used : Primary(org.springframework.context.annotation.Primary) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) NoUniqueBeanDefinitionException(org.springframework.beans.factory.NoUniqueBeanDefinitionException)

Example 44 with Primary

use of org.springframework.context.annotation.Primary in project commons-dao by reportportal.

the class DatabaseConfiguration method transactionManager.

@Bean
@Primary
public PlatformTransactionManager transactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory());
    return transactionManager;
}
Also used : JpaTransactionManager(org.springframework.orm.jpa.JpaTransactionManager) Primary(org.springframework.context.annotation.Primary) JpaRepositoryFactoryBean(org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Example 45 with Primary

use of org.springframework.context.annotation.Primary in project spring-boot by spring-projects.

the class JmxAutoConfiguration method mbeanExporter.

@Bean
@Primary
@ConditionalOnMissingBean(value = MBeanExporter.class, search = SearchStrategy.CURRENT)
public AnnotationMBeanExporter mbeanExporter(ObjectNamingStrategy namingStrategy) {
    AnnotationMBeanExporter exporter = new AnnotationMBeanExporter();
    exporter.setRegistrationPolicy(RegistrationPolicy.FAIL_ON_EXISTING);
    exporter.setNamingStrategy(namingStrategy);
    String server = this.propertyResolver.getProperty("server", "mbeanServer");
    if (StringUtils.hasLength(server)) {
        exporter.setServer(this.beanFactory.getBean(server, MBeanServer.class));
    }
    return exporter;
}
Also used : AnnotationMBeanExporter(org.springframework.jmx.export.annotation.AnnotationMBeanExporter) MBeanServer(javax.management.MBeanServer) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Primary(org.springframework.context.annotation.Primary) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) MBeanServerFactoryBean(org.springframework.jmx.support.MBeanServerFactoryBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

Primary (org.springframework.context.annotation.Primary)87 Bean (org.springframework.context.annotation.Bean)84 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)14 SqlSessionFactoryBean (org.mybatis.spring.SqlSessionFactoryBean)13 SQLException (java.sql.SQLException)10 LocalContainerEntityManagerFactoryBean (org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)10 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 JpaTransactionManager (org.springframework.orm.jpa.JpaTransactionManager)7 ServletRegistrationBean (org.springframework.boot.web.servlet.ServletRegistrationBean)6 DefaultTokenServices (org.springframework.security.oauth2.provider.token.DefaultTokenServices)6 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)4 ConfigurationProperties (org.springframework.boot.context.properties.ConfigurationProperties)4 JodaModule (com.fasterxml.jackson.datatype.joda.JodaModule)3 LocalDateSerializer (com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer)3 LocalDateTimeSerializer (com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer)3 LocalTimeSerializer (com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer)3 XmAuthenticationContext (com.icthh.xm.commons.security.XmAuthenticationContext)3 XmAuthenticationContextHolder (com.icthh.xm.commons.security.XmAuthenticationContextHolder)3 FilterRegistrationBean (org.springframework.boot.web.servlet.FilterRegistrationBean)3