Search in sources :

Example 61 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project spring-framework by spring-projects.

the class SimpleTransactionScopeTests method getFromScope.

@Test
@SuppressWarnings("resource")
public void getFromScope() throws Exception {
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());
    GenericBeanDefinition bd1 = new GenericBeanDefinition();
    bd1.setBeanClass(TestBean.class);
    bd1.setScope("tx");
    bd1.setPrimary(true);
    context.registerBeanDefinition("txScopedObject1", bd1);
    GenericBeanDefinition bd2 = new GenericBeanDefinition();
    bd2.setBeanClass(DerivedTestBean.class);
    bd2.setScope("tx");
    context.registerBeanDefinition("txScopedObject2", bd2);
    context.refresh();
    try {
        context.getBean(TestBean.class);
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        // expected - no synchronization active
        assertTrue(ex.getCause() instanceof IllegalStateException);
    }
    try {
        context.getBean(DerivedTestBean.class);
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        // expected - no synchronization active
        assertTrue(ex.getCause() instanceof IllegalStateException);
    }
    TestBean bean1 = null;
    DerivedTestBean bean2 = null;
    DerivedTestBean bean2a = null;
    DerivedTestBean bean2b = null;
    TransactionSynchronizationManager.initSynchronization();
    try {
        bean1 = context.getBean(TestBean.class);
        assertSame(bean1, context.getBean(TestBean.class));
        bean2 = context.getBean(DerivedTestBean.class);
        assertSame(bean2, context.getBean(DerivedTestBean.class));
        context.getBeanFactory().destroyScopedBean("txScopedObject2");
        assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2"));
        assertTrue(bean2.wasDestroyed());
        bean2a = context.getBean(DerivedTestBean.class);
        assertSame(bean2a, context.getBean(DerivedTestBean.class));
        assertNotSame(bean2, bean2a);
        context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
        assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2"));
        assertFalse(bean2a.wasDestroyed());
        bean2b = context.getBean(DerivedTestBean.class);
        assertSame(bean2b, context.getBean(DerivedTestBean.class));
        assertNotSame(bean2, bean2b);
        assertNotSame(bean2a, bean2b);
    } finally {
        TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED);
        TransactionSynchronizationManager.clearSynchronization();
    }
    assertFalse(bean2a.wasDestroyed());
    assertTrue(bean2b.wasDestroyed());
    assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
    try {
        context.getBean(TestBean.class);
        fail("Should have thrown IllegalStateException");
    } catch (BeanCreationException ex) {
        // expected - no synchronization active
        assertTrue(ex.getCause() instanceof IllegalStateException);
    }
    try {
        context.getBean(DerivedTestBean.class);
        fail("Should have thrown IllegalStateException");
    } catch (BeanCreationException ex) {
        // expected - no synchronization active
        assertTrue(ex.getCause() instanceof IllegalStateException);
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) BeanCreationException(org.springframework.beans.factory.BeanCreationException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Example 62 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project spring-boot by spring-projects.

the class EndpointMBeanExporterTests method testRegistrationWithDifferentDomain.

@Test
public void testRegistrationWithDifferentDomain() throws Exception {
    this.context = new GenericApplicationContext();
    this.context.registerBeanDefinition("endpointMbeanExporter", new RootBeanDefinition(EndpointMBeanExporter.class, null, new MutablePropertyValues(Collections.singletonMap("domain", "test-domain"))));
    this.context.registerBeanDefinition("endpoint1", new RootBeanDefinition(TestEndpoint.class));
    this.context.refresh();
    MBeanExporter mbeanExporter = this.context.getBean(EndpointMBeanExporter.class);
    assertThat(mbeanExporter.getServer().getMBeanInfo(getObjectName("test-domain", "endpoint1", false, this.context))).isNotNull();
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) MBeanExporter(org.springframework.jmx.export.MBeanExporter) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 63 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project spring-boot by spring-projects.

the class EndpointMBeanExporterTests method testSkipRegistrationOfDisabledEndpoint.

@Test
public void testSkipRegistrationOfDisabledEndpoint() throws Exception {
    this.context = new GenericApplicationContext();
    this.context.registerBeanDefinition("endpointMbeanExporter", new RootBeanDefinition(EndpointMBeanExporter.class));
    MutablePropertyValues mpv = new MutablePropertyValues();
    mpv.add("enabled", Boolean.FALSE);
    this.context.registerBeanDefinition("endpoint1", new RootBeanDefinition(TestEndpoint.class, null, mpv));
    this.context.refresh();
    MBeanExporter mbeanExporter = this.context.getBean(EndpointMBeanExporter.class);
    assertThat(mbeanExporter.getServer().isRegistered(getObjectName("endpoint1", this.context))).isFalse();
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) MBeanExporter(org.springframework.jmx.export.MBeanExporter) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 64 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project spring-boot by spring-projects.

the class EndpointMBeanExporterTests method registerLoggersEndpoint.

private MBeanExporter registerLoggersEndpoint() {
    this.context = new GenericApplicationContext();
    this.context.registerBeanDefinition("endpointMbeanExporter", new RootBeanDefinition(EndpointMBeanExporter.class));
    RootBeanDefinition bd = new RootBeanDefinition(LoggersEndpoint.class);
    ConstructorArgumentValues values = new ConstructorArgumentValues();
    values.addIndexedArgumentValue(0, new LogbackLoggingSystem(getClass().getClassLoader()));
    bd.setConstructorArgumentValues(values);
    this.context.registerBeanDefinition("loggersEndpoint", bd);
    this.context.refresh();
    return this.context.getBean(EndpointMBeanExporter.class);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) LogbackLoggingSystem(org.springframework.boot.logging.logback.LogbackLoggingSystem) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Example 65 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project spring-boot by spring-projects.

the class EndpointMBeanExporterTests method testRegistrationTwoEndpoints.

@Test
public void testRegistrationTwoEndpoints() throws Exception {
    this.context = new GenericApplicationContext();
    this.context.registerBeanDefinition("endpointMbeanExporter", new RootBeanDefinition(EndpointMBeanExporter.class));
    this.context.registerBeanDefinition("endpoint1", new RootBeanDefinition(TestEndpoint.class));
    this.context.registerBeanDefinition("endpoint2", new RootBeanDefinition(TestEndpoint2.class));
    this.context.refresh();
    MBeanExporter mbeanExporter = this.context.getBean(EndpointMBeanExporter.class);
    assertThat(mbeanExporter.getServer().getMBeanInfo(getObjectName("endpoint1", this.context))).isNotNull();
    assertThat(mbeanExporter.getServer().getMBeanInfo(getObjectName("endpoint2", this.context))).isNotNull();
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) MBeanExporter(org.springframework.jmx.export.MBeanExporter) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Aggregations

GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)245 Test (org.junit.Test)190 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)80 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)50 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)38 BeanCreationException (org.springframework.beans.factory.BeanCreationException)26 ClassPathResource (org.springframework.core.io.ClassPathResource)19 TestBean (org.springframework.tests.sample.beans.TestBean)17 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)15 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)12 MBeanExporter (org.springframework.jmx.export.MBeanExporter)11 BeansException (org.springframework.beans.BeansException)8 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)8 HashMap (java.util.HashMap)7 UnsatisfiedDependencyException (org.springframework.beans.factory.UnsatisfiedDependencyException)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 Before (org.junit.Before)6 Properties (java.util.Properties)5 EntityManager (javax.persistence.EntityManager)5 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)5