Search in sources :

Example 31 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spock by spockframework.

the class SpringMockTestExecutionListener method beforeTestMethod.

public void beforeTestMethod(TestContext testContext) throws Exception {
    Object testInstance = testContext.getTestInstance();
    if (testInstance instanceof Specification) {
        Specification specification = (Specification) testInstance;
        ScanScopedBeans scanScopedBeans = ReflectionUtil.getAnnotationRecursive(specification.getClass(), ScanScopedBeans.class);
        Set<String> scopes = scanScopedBeans == null ? Collections.<String>emptySet() : new HashSet<String>(Arrays.asList(scanScopedBeans.value()));
        ApplicationContext applicationContext = testContext.getApplicationContext();
        String[] mockBeanNames = applicationContext.getBeanDefinitionNames();
        List<Object> mockedBeans = new ArrayList<Object>();
        for (String beanName : mockBeanNames) {
            BeanDefinition beanDefinition = ((BeanDefinitionRegistry) applicationContext).getBeanDefinition(beanName);
            if (beanDefinition.isAbstract()) {
                continue;
            }
            if (beanDefinition.isSingleton() || scanScopedBean(scanScopedBeans, scopes, beanDefinition)) {
                Object bean = applicationContext.getBean(beanName);
                if (mockUtil.isMock(bean)) {
                    mockUtil.attachMock(bean, specification);
                    mockedBeans.add(bean);
                }
            }
        }
        testContext.setAttribute(MOCKED_BEANS_LIST, mockedBeans);
    } else {
        throw new IllegalArgumentException("SpringMockTestExecutionListener is only applicable for spock specifications.");
    }
}
Also used : BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) Specification(spock.lang.Specification) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ApplicationContext(org.springframework.context.ApplicationContext)

Example 32 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-security by spring-projects.

the class ClearCredentialsMethodInvokingFactoryBean method createPortMapper.

private BeanReference createPortMapper(Element elt, ParserContext pc) {
    // Register the portMapper. A default will always be created, even if no element
    // exists.
    BeanDefinition portMapper = new PortMappingsBeanDefinitionParser().parse(DomUtils.getChildElementByTagName(elt, Elements.PORT_MAPPINGS), pc);
    String portMapperName = pc.getReaderContext().generateBeanName(portMapper);
    pc.registerBeanComponent(new BeanComponentDefinition(portMapper, portMapperName));
    return new RuntimeBeanReference(portMapperName);
}
Also used : BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 33 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-security by spring-projects.

the class AuthenticationConfigBuilder method createOpenIDProvider.

private void createOpenIDProvider() {
    Element openIDLoginElt = DomUtils.getChildElementByTagName(httpElt, Elements.OPENID_LOGIN);
    BeanDefinitionBuilder openIDProviderBuilder = BeanDefinitionBuilder.rootBeanDefinition(OPEN_ID_AUTHENTICATION_PROVIDER_CLASS);
    RootBeanDefinition uds = new RootBeanDefinition();
    uds.setFactoryBeanName(BeanIds.USER_DETAILS_SERVICE_FACTORY);
    uds.setFactoryMethodName("authenticationUserDetailsService");
    uds.getConstructorArgumentValues().addGenericArgumentValue(openIDLoginElt.getAttribute(ATT_USER_SERVICE_REF));
    openIDProviderBuilder.addPropertyValue("authenticationUserDetailsService", uds);
    BeanDefinition openIDProvider = openIDProviderBuilder.getBeanDefinition();
    openIDProviderRef = new RuntimeBeanReference(pc.getReaderContext().registerWithGeneratedName(openIDProvider));
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 34 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-boot by spring-projects.

the class AtomikosDependsOnBeanFactoryPostProcessorTests method assertDependsOn.

private void assertDependsOn(String bean, String... expected) {
    BeanDefinition definition = this.context.getBeanDefinition(bean);
    if (definition.getDependsOn() == null) {
        assertThat(expected).as("No dependsOn expected for " + bean).isEmpty();
        return;
    }
    HashSet<String> dependsOn = new HashSet<>(Arrays.asList(definition.getDependsOn()));
    assertThat(dependsOn).isEqualTo(new HashSet<>(Arrays.asList(expected)));
}
Also used : BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) HashSet(java.util.HashSet)

Example 35 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-framework by spring-projects.

the class RequiredAnnotationBeanPostProcessorTests method testWithRequiredPropertyOmitted.

@Test
public void testWithRequiredPropertyOmitted() {
    try {
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        BeanDefinition beanDef = BeanDefinitionBuilder.genericBeanDefinition(RequiredTestBean.class).addPropertyValue("name", "Rob Harrop").addPropertyValue("favouriteColour", "Blue").addPropertyValue("jobTitle", "Grand Poobah").getBeanDefinition();
        factory.registerBeanDefinition("testBean", beanDef);
        factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
        factory.preInstantiateSingletons();
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        String message = ex.getCause().getMessage();
        assertTrue(message.contains("Property"));
        assertTrue(message.contains("age"));
        assertTrue(message.contains("testBean"));
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Aggregations

BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)593 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)188 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)117 Test (org.junit.jupiter.api.Test)105 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)89 Element (org.w3c.dom.Element)73 Test (org.junit.Test)70 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)60 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)54 ManagedList (org.springframework.beans.factory.support.ManagedList)40 ClassPathScanningCandidateComponentProvider (org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider)39 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)36 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)35 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)33 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)33 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)30 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)29 ArrayList (java.util.ArrayList)28 Method (java.lang.reflect.Method)27 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)27