コード例 #1
0
 @Override
 protected ApplicationContext initApplicationContext() {
   AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
   wac.register(WebConfig.class);
   wac.refresh();
   return wac;
 }
コード例 #2
0
  public static AnnotationConfigApplicationContext inititializeApplicationContext() {
    final AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(PersistenceJPAConfig.class);
    ctx.refresh();

    return ctx;
  }
コード例 #3
0
ファイル: Main.java プロジェクト: ggoskag/containers
  public static void main(String[] args) {

    try {

      LOGGER.info("Starting application");

      String resourceName = "spring.profiles.active";
      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      Properties props = new Properties();
      try (InputStream resourceStream = loader.getResourceAsStream(resourceName)) {
        props.load(resourceStream);
      }
      System.setProperty("spring.profiles.active", props.getProperty("spring.profiles.active"));

      context = new AnnotationConfigApplicationContext();
      context.register(ApplicationConfiguration.class);
      context.refresh();

      CategoryDAO dao = context.getBean(CategoryDAO.class);
      dao.getAllCategories();
      ProducerDAO prdao = context.getBean(ProducerDAO.class);
      prdao.getAllProducers();
      ProductDAO pdao = context.getBean(ProductDAO.class);
      pdao.getAllProducts();

      MainView view = context.getBean(MainView.class);
      view.show();

    } catch (Exception e) {
      LOGGER.error(" ERROR " + e.getMessage());
    }
  }
  @Test
  public void testEndpointMBeanExporterInParentChild()
      throws IntrospectionException, InstanceNotFoundException, MalformedObjectNameException,
          ReflectionException {
    this.context = new AnnotationConfigApplicationContext();
    this.context.register(
        JmxAutoConfiguration.class,
        EndpointAutoConfiguration.class,
        EndpointMBeanExportAutoConfiguration.class);

    AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
    parent.register(
        JmxAutoConfiguration.class,
        EndpointAutoConfiguration.class,
        EndpointMBeanExportAutoConfiguration.class);
    this.context.setParent(parent);

    parent.refresh();
    this.context.refresh();

    parent.close();

    System.out.println("parent " + ObjectUtils.getIdentityHexString(parent));
    System.out.println("child " + ObjectUtils.getIdentityHexString(this.context));
  }
コード例 #5
0
  @Override
  public void configure(AtmosphereConfig config) {
    try {

      String s = config.getInitParameter(ATMOSPHERE_SPRING_EXCLUDE_CLASSES);
      if (s != null) {
        String[] list = s.split(",");
        for (String clazz : list) {
          excludedFromInjection.add(IOUtils.loadClass(getClass(), clazz));
        }

        if (list.length > 0) {
          preventSpringInjection = true;
        }
      }

      context = new AnnotationConfigApplicationContext();
      context.setParent(
          WebApplicationContextUtils.getWebApplicationContext(
              config.framework().getServletContext()));

      context.refresh();

      // Hack to make it injectable
      context.register(AtmosphereConfig.class);
      ((AtmosphereConfig)
              context.getBean(AtmosphereConfig.class.getCanonicalName(), config.framework()))
          .populate(config);
    } catch (Exception ex) {
      logger.warn("Unable to configure injection", ex);
    }
  }
コード例 #6
0
 private AnnotationConfigApplicationContext load(Class<?> config, String... env) {
   AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
   EnvironmentTestUtils.addEnvironment(context, env);
   context.register(config);
   context.refresh();
   return context;
 }
 @Test
 public void basicBinding() {
   EnvironmentTestUtils.addEnvironment(context, "eureka.instance.appGroupName=mygroup");
   context.register(PropertyPlaceholderAutoConfiguration.class, TestConfiguration.class);
   context.refresh();
   assertEquals("mygroup", context.getBean(EurekaInstanceConfigBean.class).getAppGroupName());
 }
コード例 #8
0
  public void performAction() {
    /* Choose appropriate Spring profile */
    final String profileName = action.getSpringProfileName();

    /* Let action do any pre-setup work */
    action.beforeApplicationContextInit();

    /* Initialise ApplicationConetext */
    logger.debug("Setting up Spring ApplicationContext using profile '{}'", profileName);
    final AnnotationConfigApplicationContext applicationContext =
        new AnnotationConfigApplicationContext();
    applicationContext.getEnvironment().setActiveProfiles(profileName);
    QtiWorksApplicationContextHelper.registerConfigPropertySources(
        applicationContext, deploymentPropertiesResource);
    applicationContext.register(
        PropertiesConfiguration.class,
        JpaProductionConfiguration.class,
        JpaBootstrapConfiguration.class,
        ServicesConfiguration.class,
        ManagerConfiguration.class);
    applicationContext.refresh();

    /* Now let action class do its work*/
    try {
      action.run(applicationContext, actionParameters);
    } catch (final Exception e) {
      logger.warn("Unexpected Exception performing action", e);
    } finally {
      applicationContext.close();
    }
  }
コード例 #9
0
ファイル: SpringJavaConfig.java プロジェクト: vamsigoli/basic
  public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.scan("com.vamsi");
    ctx.refresh();

    for (String beanname : ctx.getBeanDefinitionNames()) {

      System.out.println(beanname);
    }

    HollywoodServiceJSR330 hollywoodservice =
        ctx.getBean("hollywoodServiceJSR330", HollywoodServiceJSR330.class);

    hollywoodservice.getFriendlyAgents();

    Box<Integer> abox = ctx.getBean("intBox", Box.class);

    abox.setA(100);

    System.out.println("first bean " + abox.getA());

    Box<Integer> bbox = ctx.getBean("intBox", Box.class);

    System.out.println("second bean " + abox.getA());

    ctx.close();
  }
コード例 #10
0
 @Test
 public void hazelcastCacheWithMainHazelcastAutoConfigurationAndSeparateCacheConfig()
     throws IOException {
   String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
   String cacheConfig = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml";
   AnnotationConfigApplicationContext applicationContext =
       new AnnotationConfigApplicationContext();
   EnvironmentTestUtils.addEnvironment(
       applicationContext,
       "spring.cache.type=hazelcast",
       "spring.cache.hazelcast.config=" + cacheConfig,
       "spring.hazelcast.config=" + mainConfig);
   applicationContext.register(DefaultCacheConfiguration.class);
   applicationContext.register(HazelcastAndCacheConfiguration.class);
   applicationContext.refresh();
   this.context = applicationContext;
   HazelcastInstance hazelcastInstance = this.context.getBean(HazelcastInstance.class);
   HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class);
   HazelcastInstance cacheHazelcastInstance = getHazelcastInstance(cacheManager);
   assertThat(cacheHazelcastInstance, not(hazelcastInstance)); // Our custom
   assertThat(
       hazelcastInstance.getConfig().getConfigurationFile(),
       equalTo(new ClassPathResource(mainConfig).getFile()));
   assertThat(
       cacheHazelcastInstance.getConfig().getConfigurationFile(),
       equalTo(new ClassPathResource(cacheConfig).getFile()));
 }
コード例 #11
0
 public static void main(String[] args) {
   AnnotationConfigApplicationContext annotationConfigApplicationContext =
       new AnnotationConfigApplicationContext();
   annotationConfigApplicationContext.register(CustomizeScanTest.class);
   annotationConfigApplicationContext.refresh();
   ScanClass1 injectClass = annotationConfigApplicationContext.getBean(ScanClass1.class);
   injectClass.print();
 }
コード例 #12
0
 @Test
 public void annotatedService_PTC_false() {
   AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
   ctx.register(PTCFalse.class, NonAnnotatedServiceImpl.class);
   ctx.refresh();
   AnnotatedService s = ctx.getBean(AnnotatedService.class);
   assertTrue("expected a jdk proxy", AopUtils.isJdkDynamicProxy(s));
   assertThat(s, not(instanceOf(NonAnnotatedServiceImpl.class)));
 }
コード例 #13
0
 @Test
 public void annotatedService_PTC_true() {
   AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
   ctx.register(PTCTrue.class, NonAnnotatedServiceImpl.class);
   ctx.refresh();
   AnnotatedService s = ctx.getBean(AnnotatedService.class);
   assertTrue("expected a subclass proxy", AopUtils.isCglibProxy(s));
   assertThat(s, instanceOf(NonAnnotatedServiceImpl.class));
 }
コード例 #14
0
  @Test
  public void orderAttributeIsPropagated() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(OrderedAsyncConfig.class);
    ctx.refresh();

    AsyncAnnotationBeanPostProcessor bpp = ctx.getBean(AsyncAnnotationBeanPostProcessor.class);
    assertThat(bpp.getOrder(), is(Ordered.HIGHEST_PRECEDENCE));
  }
コード例 #15
0
 @Override
 protected ConfigurableApplicationContext getApplicationContext() {
   AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
   context.getBeanFactory().registerSingleton("cachingProvider", getCachingProvider());
   context.register(EnableCachingConfig.class);
   context.refresh();
   jCacheManager = context.getBean("jCacheManager", CacheManager.class);
   return context;
 }
コード例 #16
0
 private void load(Class<?> config, String... environment) {
   AnnotationConfigApplicationContext applicationContext =
       new AnnotationConfigApplicationContext();
   EnvironmentTestUtils.addEnvironment(applicationContext, environment);
   applicationContext.register(config);
   applicationContext.register(CacheAutoConfiguration.class);
   applicationContext.refresh();
   this.context = applicationContext;
 }
コード例 #17
0
 public static void main(String[] args) {
   AnnotationConfigApplicationContext annotationConfigApplicationContext =
       new AnnotationConfigApplicationContext();
   annotationConfigApplicationContext.register(ApplicationListenerTest.class);
   annotationConfigApplicationContext.refresh();
   PrintSomething printSomething =
       annotationConfigApplicationContext.getBean(PrintSomething.class);
   printSomething.print();
 }
コード例 #18
0
 private AnnotationConfigApplicationContext doLoad(Class<?>[] configs, String... environment) {
   AnnotationConfigApplicationContext applicationContext =
       new AnnotationConfigApplicationContext();
   applicationContext.register(configs);
   applicationContext.register(RabbitAutoConfiguration.class);
   EnvironmentTestUtils.addEnvironment(applicationContext, environment);
   applicationContext.refresh();
   return applicationContext;
 }
コード例 #19
0
  @Test
  public void asyncProcessorIsOrderedLowestPrecedenceByDefault() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(AsyncConfig.class);
    ctx.refresh();

    AsyncAnnotationBeanPostProcessor bpp = ctx.getBean(AsyncAnnotationBeanPostProcessor.class);
    assertThat(bpp.getOrder(), is(Ordered.LOWEST_PRECEDENCE));
  }
コード例 #20
0
ファイル: Launcher.java プロジェクト: walternate/spring-data
  public static void main(String[] args) {

    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ApplicationConfig.class);
    ctx.refresh();
    System.out.println("version: " + SpringVersion.getVersion());
    SpringUI ui = ctx.getBean(SpringUI.class);
    ui.init();
  }
コード例 #21
0
 @Test
 public void skipsFilterIfPropertyDisabled() throws Exception {
   AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
   EnvironmentTestUtils.addEnvironment(context, "endpoints.metrics.filter.enabled:false");
   context.register(Config.class, MetricFilterAutoConfiguration.class);
   context.refresh();
   assertThat(context.getBeansOfType(Filter.class).size()).isEqualTo(0);
   context.close();
 }
コード例 #22
0
 public void testWithConfigurationClass() throws Exception {
   AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
   parent.register(TransactionManagerConfiguration.class);
   parent.refresh();
   ClassPathXmlApplicationContext context =
       new ClassPathXmlApplicationContext(
           new String[] {"annotationDrivenConfigurationClassTests.xml"}, getClass(), parent);
   doTestWithMultipleTransactionManagers(context);
 }
コード例 #23
0
 @Test
 public void strictTraceId_defaultsToTrue() {
   context = new AnnotationConfigApplicationContext();
   addEnvironment(context, "zipkin.storage.type:mysql");
   context.register(
       PropertyPlaceholderAutoConfiguration.class, ZipkinMySQLStorageAutoConfiguration.class);
   context.refresh();
   assertThat(context.getBean(MySQLStorage.class).strictTraceId).isTrue();
 }
コード例 #24
0
 @Test
 public void transactionProxyIsCreated() {
   AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
   ctx.register(EnableTxConfig.class, TxManagerConfig.class);
   ctx.refresh();
   TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
   assertThat("testBean is not a proxy", AopUtils.isAopProxy(bean), is(true));
   Map<?, ?> services = ctx.getBeansWithAnnotation(Service.class);
   assertThat("Stereotype annotation not visible", services.containsKey("testBean"), is(true));
 }
コード例 #25
0
  @Test
  public void txManagerIsResolvedOnInvocationOfTransactionalMethod() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(EnableTxConfig.class, TxManagerConfig.class);
    ctx.refresh();
    TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);

    // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
    bean.findAllFoos();
  }
コード例 #26
0
  @Test
  public void providesStorageComponent_whenStorageTypeMySQL() {
    context = new AnnotationConfigApplicationContext();
    addEnvironment(context, "zipkin.storage.type:mysql");
    context.register(
        PropertyPlaceholderAutoConfiguration.class, ZipkinMySQLStorageAutoConfiguration.class);
    context.refresh();

    assertThat(context.getBean(MySQLStorage.class)).isNotNull();
  }
コード例 #27
0
  @Test
  public void txManagerIsResolvedCorrectlyWhenMultipleManagersArePresent() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(EnableTxConfig.class, MultiTxManagerConfig.class);
    ctx.refresh();
    TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);

    // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
    bean.findAllFoos();
  }
コード例 #28
0
  @Test
  public void proxyingOccurs() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(AsyncConfig.class);
    ctx.refresh();

    AsyncBean asyncBean = ctx.getBean(AsyncBean.class);
    assertThat(AopUtils.isAopProxy(asyncBean), is(true));
    asyncBean.work();
  }
コード例 #29
0
  @Test
  public void canOverridesProperty_username() {
    context = new AnnotationConfigApplicationContext();
    addEnvironment(context, "zipkin.storage.type:mysql", "zipkin.storage.mysql.username:robot");
    context.register(
        PropertyPlaceholderAutoConfiguration.class, ZipkinMySQLStorageAutoConfiguration.class);
    context.refresh();

    assertThat(context.getBean(ZipkinMySQLStorageProperties.class).getUsername())
        .isEqualTo("robot");
  }
コード例 #30
0
  @Test
  public void doesntProvidesStorageComponent_whenStorageTypeNotMySQL() {
    context = new AnnotationConfigApplicationContext();
    addEnvironment(context, "zipkin.storage.type:cassandra");
    context.register(
        PropertyPlaceholderAutoConfiguration.class, ZipkinMySQLStorageAutoConfiguration.class);
    context.refresh();

    thrown.expect(NoSuchBeanDefinitionException.class);
    context.getBean(MySQLStorage.class);
  }