Search in sources :

Example 6 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)

Example 7 with GenericApplicationContext

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

the class EndpointMBeanExporterTests method testRegistrationWithDifferentDomainAndIdentity.

@Test
public void testRegistrationWithDifferentDomainAndIdentity() throws Exception {
    Map<String, Object> properties = new HashMap<>();
    properties.put("domain", "test-domain");
    properties.put("ensureUniqueRuntimeObjectNames", true);
    this.context = new GenericApplicationContext();
    this.context.registerBeanDefinition("endpointMbeanExporter", new RootBeanDefinition(EndpointMBeanExporter.class, null, new MutablePropertyValues(properties)));
    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", true, this.context))).isNotNull();
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) MBeanExporter(org.springframework.jmx.export.MBeanExporter) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 8 with GenericApplicationContext

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

the class ApplicationContextExpressionTests method systemPropertiesSecurityManager.

@Test
public void systemPropertiesSecurityManager() {
    GenericApplicationContext ac = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
    GenericBeanDefinition bd = new GenericBeanDefinition();
    bd.setBeanClass(TestBean.class);
    bd.getPropertyValues().add("country", "#{systemProperties.country}");
    ac.registerBeanDefinition("tb", bd);
    SecurityManager oldSecurityManager = System.getSecurityManager();
    try {
        System.setProperty("country", "NL");
        SecurityManager securityManager = new SecurityManager() {

            @Override
            public void checkPropertiesAccess() {
                throw new AccessControlException("Not Allowed");
            }

            @Override
            public void checkPermission(Permission perm) {
            // allow everything else
            }
        };
        System.setSecurityManager(securityManager);
        ac.refresh();
        TestBean tb = ac.getBean("tb", TestBean.class);
        assertEquals("NL", tb.getCountry());
    } finally {
        System.setSecurityManager(oldSecurityManager);
        System.getProperties().remove("country");
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) TestBean(org.springframework.tests.sample.beans.TestBean) Permission(java.security.Permission) AccessControlException(java.security.AccessControlException) Test(org.junit.Test)

Example 9 with GenericApplicationContext

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

the class ApplicationContextExpressionTests method prototypeCreationIsFastEnough.

@Test
public void prototypeCreationIsFastEnough() {
    Assume.group(TestGroup.PERFORMANCE);
    Assume.notLogging(factoryLog);
    GenericApplicationContext ac = new GenericApplicationContext();
    RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    rbd.getConstructorArgumentValues().addGenericArgumentValue("#{systemProperties.name}");
    rbd.getPropertyValues().add("country", "#{systemProperties.country}");
    ac.registerBeanDefinition("test", rbd);
    ac.refresh();
    StopWatch sw = new StopWatch();
    sw.start("prototype");
    System.getProperties().put("name", "juergen");
    System.getProperties().put("country", "UK");
    try {
        for (int i = 0; i < 100000; i++) {
            TestBean tb = (TestBean) ac.getBean("test");
            assertEquals("juergen", tb.getName());
            assertEquals("UK", tb.getCountry());
        }
        sw.stop();
    } finally {
        System.getProperties().remove("country");
        System.getProperties().remove("name");
    }
    assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 6000);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) TestBean(org.springframework.tests.sample.beans.TestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test)

Example 10 with GenericApplicationContext

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

the class FormattingConversionServiceTests method formatFieldForAnnotationWithPlaceholders.

@Test
@SuppressWarnings("resource")
public void formatFieldForAnnotationWithPlaceholders() throws Exception {
    GenericApplicationContext context = new GenericApplicationContext();
    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    Properties props = new Properties();
    props.setProperty("dateStyle", "S-");
    props.setProperty("datePattern", "M-d-yy");
    ppc.setProperties(props);
    context.getBeanFactory().registerSingleton("ppc", ppc);
    context.refresh();
    context.getBeanFactory().initializeBean(formattingService, "formattingService");
    formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
    doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false);
}
Also used : PropertyPlaceholderConfigurer(org.springframework.beans.factory.config.PropertyPlaceholderConfigurer) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) JodaDateTimeFormatAnnotationFormatterFactory(org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)378 Test (org.junit.jupiter.api.Test)193 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)106 Test (org.junit.Test)74 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)72 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)50 ClassPathResource (org.springframework.core.io.ClassPathResource)36 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)16 QueueChannel (org.springframework.integration.channel.QueueChannel)16 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)15 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)15 GenericMessage (org.springframework.messaging.support.GenericMessage)14 Properties (java.util.Properties)13 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)13 File (java.io.File)12 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)12 Message (org.springframework.messaging.Message)12 InputStreamResource (org.springframework.core.io.InputStreamResource)11 MBeanExporter (org.springframework.jmx.export.MBeanExporter)11 BeforeEach (org.junit.jupiter.api.BeforeEach)10