Example usage for org.springframework.context.annotation AnnotationConfigApplicationContext scan

List of usage examples for org.springframework.context.annotation AnnotationConfigApplicationContext scan

Introduction

In this page you can find the example usage for org.springframework.context.annotation AnnotationConfigApplicationContext scan.

Prototype

public void scan(String... basePackages) 

Source Link

Document

Perform a scan within the specified base packages.

Usage

From source file:de.thischwa.pmcms.conf.BasicConfigurator.java

private void init() {
    if (System.getProperty("data.dir") == null)
        throw new IllegalArgumentException("No data directory set!");
    dataDir = new File(System.getProperty("data.dir"));
    if (!dataDir.exists())
        throw new IllegalArgumentException(
                String.format("Data directory not found: %s", dataDir.getAbsolutePath()));

    // load and merge the properties 
    loadProperties();/*from   w w  w  . ja  va 2s .  c  om*/

    // build special props
    String baseUrl = String.format("http://%s:%s/", props.get("pmcms.jetty.host"),
            props.get("pmcms.jetty.port"));
    props.setProperty("baseurl", baseUrl);
    props.setProperty("data.dir", dataDir.getAbsolutePath());
    System.setProperty("content.types.user.table",
            new File(Constants.APPLICATION_DIR, "lib/content-types.properties").getAbsolutePath());

    // init log4j
    LogManager.resetConfiguration();
    PropertyConfigurator.configure(PropertiesTool.getProperties(props, "log4j"));
    Logger logger = Logger.getLogger(BasicConfigurator.class);
    logger.info("*** log4j initialized!");

    // init the spring framework
    try {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.scan("de.thischwa.pmcms");
        PropertySourcesPlaceholderConfigurer config = new PropertySourcesPlaceholderConfigurer();
        config.setProperties(props);
        config.postProcessBeanFactory(ctx.getDefaultListableBeanFactory());
        ctx.refresh();
        context = ctx;
        logger.info("*** Spring initialized!");
        PropertiesManager pm = context.getBean(PropertiesManager.class);
        pm.setProperties(props);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    logger.info("*** Basic configuration successful done.");
}

From source file:org.twinkql.template.AbstractTwinkqlTemplateFactory.java

/**
 * Gets the twinkql template.//www  .  j ava  2 s . c om
 *
 * @return the twinkql template
 * @throws Exception the exception
 */
public TwinkqlTemplate getTwinkqlTemplate() {

    DefaultListableBeanFactory parentBeanFactory = new DefaultListableBeanFactory();

    parentBeanFactory.registerSingleton("twinkqlContext", this.getTwinkqlContext());

    GenericApplicationContext parentContext = new GenericApplicationContext(parentBeanFactory);

    parentContext.refresh();

    AnnotationConfigApplicationContext annotationConfigApplicationContext = this
            .decorateContext(new AnnotationConfigApplicationContext());
    annotationConfigApplicationContext.setParent(parentContext);
    annotationConfigApplicationContext.scan(PACKAGE_SCAN);
    annotationConfigApplicationContext.refresh();

    TwinkqlTemplate template = annotationConfigApplicationContext.getBean(TwinkqlTemplate.class);

    return template;
}

From source file:de.rkl.tools.tzconv.TimezoneConverter.java

private ApplicationContext initializeSpringContext() {
    final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.scan(TimezoneConverter.class.getPackage().getName());
    context.refresh();/*from ww  w  .  j  a  v  a 2  s.c o  m*/
    return context;
}

From source file:st.malike.service.storm.LNSBolt.java

@Override
public void prepare(Map stormConf, TopologyContext context) {
    try {/* ww w.  j  a v  a2s .co m*/
        this.config = stormConf;
        // reinitialize Spring IoC-- a hack
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.getEnvironment().getPropertySources()
                .addFirst(new ResourcePropertySource("classpath:application.properties"));
        ctx.scan("st.malike");
        ctx.refresh();
        demographicSummaryService = ctx.getBean(DemographicSummaryService.class);
        demographicService = ctx.getBean(DemographicService.class);
        summaryElasticSearchService = ctx.getBean(SummaryElasticSearchService.class);
        liveNotificationService = ctx.getBean(LiveNotificationService.class);
        summaryCalculatorService = ctx.getBean(SummaryCalculatorService.class);
    } catch (IOException ex) {
        Logger.getLogger(LNSBolt.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:pl.bristleback.server.bristle.conf.runner.ServerInstanceResolver.java

public BristlebackServerInstance resolverServerInstance() {
    InitialConfiguration initialConfiguration = initialConfigurationResolver.resolveConfiguration();
    startLogger(initialConfiguration);//from  w w  w  .j  av a  2s  . c  o  m

    AnnotationConfigApplicationContext frameworkContext = new AnnotationConfigApplicationContext();
    BristleSpringIntegration springIntegration = new BristleSpringIntegration(actualApplicationContext,
            frameworkContext);
    BristlebackBeanFactoryPostProcessor bristlebackPostProcessor = new BristlebackBeanFactoryPostProcessor(
            initialConfiguration, springIntegration);
    frameworkContext.addBeanFactoryPostProcessor(bristlebackPostProcessor);
    frameworkContext.register(SpringConfigurationResolver.class);
    frameworkContext.scan(InitialConfiguration.SYSTEM_BASE_PACKAGES);
    frameworkContext.refresh();

    BristlebackConfig configuration = frameworkContext.getBean("bristlebackConfigurationFinal",
            BristlebackConfig.class);
    return new BristlebackServerInstance(configuration);
}

From source file:com.visural.domo.spring.TransactionImplTest.java

private AnnotationConfigApplicationContext springBootstrap(ConnectionSource source)
        throws BeansException, IllegalStateException {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(AnnotationAwareAspectJAutoProxyCreator.class);
    CustomScopeConfigurer conf = new CustomScopeConfigurer();
    Map<String, Object> scopes = new HashMap<String, Object>();
    TransactionScope scope = new TransactionScope();
    scopes.put(TransactionScope.Name, scope);
    conf.setScopes(scopes);/*from w ww  .  java 2 s . c  om*/
    context.getBeanFactory().registerSingleton("transactionScope", scope);
    context.addBeanFactoryPostProcessor(conf);
    context.scan("com.visural");
    context.refresh();
    context.getBean(TransactionConfig.class).getConnectionProvider().registerDefaultConnectionSource(source);
    return context;
}

From source file:org.apache.camel.spring.javaconfig.test.JavaConfigContextLoader.java

/**
 * Loads a new {@link ApplicationContext context} based on the supplied {@code locations},
 * configures the context, and finally returns the context in fully <em>refreshed</em> state.
 * <p/>/* ww w.j a va2 s . co  m*/
 *
 * Configuration locations are either fully-qualified class names or base package names. These
 * locations will be given to a {@link JavaConfigApplicationContext} for configuration via the
 * {@link JavaConfigApplicationContext#addConfigClass(Class)} and
 * {@link JavaConfigApplicationContext#addBasePackage(String)} methods.
 *
 * @param locations the locations to use to load the application context
 * @return a new application context
 * @throws IllegalArgumentException if any of <var>locations</var> are not valid fully-qualified
 * Class or Package names
 */
public ApplicationContext loadContext(String... locations) {
    if (logger.isDebugEnabled()) {
        logger.debug("Creating a JavaConfigApplicationContext for " + Arrays.asList(locations));
    }

    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

    ArrayList<Class<?>> configClasses = new ArrayList<Class<?>>();
    ArrayList<String> basePackages = new ArrayList<String>();
    for (String location : locations) {
        // if the location refers to a class, use it. Otherwise assume it's a base package name
        try {
            final Class<?> aClass = this.getClass().getClassLoader().loadClass(location);
            configClasses.add(aClass);
        } catch (ClassNotFoundException e) {
            if (Package.getPackage(location) == null) {
                throw new IllegalArgumentException(
                        String.format("A non-existent class or package name was specified: [%s]", location));
            }
            basePackages.add(location);
        }
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Setting config classes to " + configClasses);
        logger.debug("Setting base packages to " + basePackages);
    }

    for (Class<?> configClass : configClasses) {
        context.register(configClass);
    }

    for (String basePackage : basePackages) {
        context.scan(basePackage);
    }

    context.refresh();

    // Have to create a child context that implements BeanDefinitionRegistry
    // to pass to registerAnnotationConfigProcessors, since
    // JavaConfigApplicationContext does not
    final GenericApplicationContext gac = new GenericApplicationContext(context);
    AnnotationConfigUtils.registerAnnotationConfigProcessors(gac);
    // copy BeanPostProcessors to the child context
    for (String bppName : context.getBeanFactory().getBeanNamesForType(BeanPostProcessor.class)) {
        gac.registerBeanDefinition(bppName, context.getBeanFactory().getBeanDefinition(bppName));
    }
    gac.refresh();
    gac.registerShutdownHook();

    return gac;
}

From source file:org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer.java

/**
 * Configures classpath component scanning using the specified base packages on the specified
 * AnnotationConfigApplicationContext.//from  w  w  w  . ja v a  2 s .c om
 *
 * @param applicationContext the AnnotationConfigApplicationContext to setup with classpath component scanning
 * using the specified base packages.
 * @param basePackages an array of Strings indicating the base packages to use in the classpath component scan.
 * @return the given AnnotationConfigApplicationContext.
 * @see org.springframework.context.annotation.AnnotationConfigApplicationContext#scan(String...)
 */
AnnotationConfigApplicationContext scanBasePackages(AnnotationConfigApplicationContext applicationContext,
        String[] basePackages) {
    if (!ObjectUtils.isEmpty(basePackages)) {
        applicationContext.scan(basePackages);
    }

    return applicationContext;
}