Example usage for org.springframework.context ApplicationContext getBeansWithAnnotation

List of usage examples for org.springframework.context ApplicationContext getBeansWithAnnotation

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext getBeansWithAnnotation.

Prototype

Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;

Source Link

Document

Find all beans which are annotated with the supplied Annotation type, returning a Map of bean names with corresponding bean instances.

Usage

From source file:net.firejack.platform.core.validation.constraint.RuleMapper.java

protected void onContextRefreshed(ContextRefreshedEvent event) {
    ApplicationContext context = event.getApplicationContext();
    Map<String, Object> beans = context.getBeansWithAnnotation(RuleSource.class);

    for (Object bean : beans.values()) {
        Class<?> clazz = bean.getClass();
        RuleSource ruleSource = clazz.getAnnotation(RuleSource.class);
        if (ruleSource != null) {
            if (StringUtils.isNotBlank(ruleSource.value())) {
                CONSTRAINT_LOCATIONS.put(ruleSource.value(), clazz);
            }/*from www  .  j a v  a 2 s  .co  m*/
        }
    }
}

From source file:org.alfresco.rest.framework.core.ApiBootstrap.java

@Override
protected void onBootstrap(ApplicationEvent event) {
    logger.info("Bootstapping the API");
    ContextRefreshedEvent refreshEvent = (ContextRefreshedEvent) event;
    ApplicationContext ac = refreshEvent.getApplicationContext();
    Map<String, Object> entityResourceBeans = ac.getBeansWithAnnotation(EntityResource.class);
    Map<String, Object> relationResourceBeans = ac.getBeansWithAnnotation(RelationshipResource.class);
    apiDictionary.setDictionary(/*from   ww  w.  java 2s. co m*/
            ResourceDictionaryBuilder.build(entityResourceBeans.values(), relationResourceBeans.values()));
}

From source file:org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint.java

private Map<String, Object> getConfigurationPropertiesBeans(ApplicationContext context,
        ConfigurationBeanFactoryMetadata beanFactoryMetadata) {
    Map<String, Object> beans = new HashMap<>();
    beans.putAll(context.getBeansWithAnnotation(ConfigurationProperties.class));
    if (beanFactoryMetadata != null) {
        beans.putAll(beanFactoryMetadata.getBeansWithFactoryAnnotation(ConfigurationProperties.class));
    }//from  ww  w. j  a va2  s . c  o m
    return beans;
}

From source file:org.springframework.boot.actuate.endpoint.ConfigurationPropertiesReportEndpoint.java

private Map<String, Object> getConfigurationPropertiesBeans(ApplicationContext context,
        ConfigurationBeanFactoryMetaData beanFactoryMetaData) {
    Map<String, Object> beans = new HashMap<String, Object>();
    beans.putAll(context.getBeansWithAnnotation(ConfigurationProperties.class));
    if (beanFactoryMetaData != null) {
        beans.putAll(beanFactoryMetaData.getBeansWithFactoryAnnotation(ConfigurationProperties.class));
    }//from www . j a  v a  2s.  c om
    return beans;
}

From source file:org.springframework.web.socket.server.endpoint.EndpointExporter.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) {

    this.applicationContext = applicationContext;

    this.serverContainer = getServerContainer();

    Map<String, Object> beans = applicationContext.getBeansWithAnnotation(ServerEndpoint.class);
    for (String beanName : beans.keySet()) {
        Class<?> beanType = applicationContext.getType(beanName);
        if (logger.isInfoEnabled()) {
            logger.info(//from   ww  w .  j a v  a  2  s . c  om
                    "Detected @ServerEndpoint bean '" + beanName + "', registering it as an endpoint by type");
        }
        this.annotatedEndpointBeanTypes.add(beanType);
    }
}

From source file:org.springframework.web.socket.server.standard.ServerEndpointExporter.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
    this.applicationContext = applicationContext;
    this.serverContainer = getServerContainer();
    Map<String, Object> beans = applicationContext.getBeansWithAnnotation(ServerEndpoint.class);
    for (String beanName : beans.keySet()) {
        Class<?> beanType = applicationContext.getType(beanName);
        if (logger.isInfoEnabled()) {
            logger.info(//from   www  .j a  v a 2s  .  c  om
                    "Detected @ServerEndpoint bean '" + beanName + "', registering it as an endpoint by type");
        }
        this.annotatedEndpointBeanTypes.add(beanType);
    }
}

From source file:utilities.PopulateDatabase.java

public static void main(String[] args) throws Throwable {
    ApplicationContext applicationContext;
    EntityManagerFactory entityManagerFactory;
    EntityManager entityManager;/*from  www.  ja va 2  s.c om*/
    EntityTransaction entityTransaction;

    applicationContext = new ClassPathXmlApplicationContext("classpath:PopulateDatabase.xml");

    entityManagerFactory = Persistence.createEntityManagerFactory(PersistenceUnit);
    entityManager = entityManagerFactory.createEntityManager();
    entityTransaction = entityManager.getTransaction();

    initialise(entityManagerFactory, entityManager);

    entityTransaction.begin();
    try {
        for (Entry<String, Object> entry : applicationContext.getBeansWithAnnotation(Entity.class).entrySet()) {
            String beanName;
            DomainEntity entity;

            beanName = entry.getKey();
            entity = (DomainEntity) entry.getValue();
            entityManager.persist(entity);
            System.out.println(String.format("Persisting (%s, %s, %d)", beanName, entity.getClass().getName(),
                    entity.getId()));
        }
        entityTransaction.commit();
    } catch (Exception oops) {
        oops.printStackTrace();
        entityTransaction.rollback();
    } finally {
        if (entityManager.isOpen())
            entityManager.close();
        if (entityManagerFactory.isOpen())
            entityManagerFactory.close();
    }
}