Search in sources :

Example 6 with AnnotatedBeanDefinitionReader

use of org.springframework.context.annotation.AnnotatedBeanDefinitionReader in project spring-framework by spring-projects.

the class HybridContextLoader method loadBeanDefinitions.

@Override
protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) {
    // Order doesn't matter: <bean> always wins over @Bean.
    new XmlBeanDefinitionReader(context).loadBeanDefinitions(mergedConfig.getLocations());
    new AnnotatedBeanDefinitionReader(context).register(mergedConfig.getClasses());
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) AnnotatedBeanDefinitionReader(org.springframework.context.annotation.AnnotatedBeanDefinitionReader)

Example 7 with AnnotatedBeanDefinitionReader

use of org.springframework.context.annotation.AnnotatedBeanDefinitionReader in project spring-framework by spring-projects.

the class AnnotationConfigWebApplicationContext method loadBeanDefinitions.

/**
	 * Register a {@link org.springframework.beans.factory.config.BeanDefinition} for
	 * any classes specified by {@link #register(Class...)} and scan any packages
	 * specified by {@link #scan(String...)}.
	 * <p>For any values specified by {@link #setConfigLocation(String)} or
	 * {@link #setConfigLocations(String[])}, attempt first to load each location as a
	 * class, registering a {@code BeanDefinition} if class loading is successful,
	 * and if class loading fails (i.e. a {@code ClassNotFoundException} is raised),
	 * assume the value is a package and attempt to scan it for annotated classes.
	 * <p>Enables the default set of annotation configuration post processors, such that
	 * {@code @Autowired}, {@code @Required}, and associated annotations can be used.
	 * <p>Configuration class bean definitions are registered with generated bean
	 * definition names unless the {@code value} attribute is provided to the stereotype
	 * annotation.
	 * @param beanFactory the bean factory to load bean definitions into
	 * @see #register(Class...)
	 * @see #scan(String...)
	 * @see #setConfigLocation(String)
	 * @see #setConfigLocations(String[])
	 * @see AnnotatedBeanDefinitionReader
	 * @see ClassPathBeanDefinitionScanner
	 */
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) {
    AnnotatedBeanDefinitionReader reader = getAnnotatedBeanDefinitionReader(beanFactory);
    ClassPathBeanDefinitionScanner scanner = getClassPathBeanDefinitionScanner(beanFactory);
    BeanNameGenerator beanNameGenerator = getBeanNameGenerator();
    if (beanNameGenerator != null) {
        reader.setBeanNameGenerator(beanNameGenerator);
        scanner.setBeanNameGenerator(beanNameGenerator);
        beanFactory.registerSingleton(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR, beanNameGenerator);
    }
    ScopeMetadataResolver scopeMetadataResolver = getScopeMetadataResolver();
    if (scopeMetadataResolver != null) {
        reader.setScopeMetadataResolver(scopeMetadataResolver);
        scanner.setScopeMetadataResolver(scopeMetadataResolver);
    }
    if (!this.annotatedClasses.isEmpty()) {
        if (logger.isInfoEnabled()) {
            logger.info("Registering annotated classes: [" + StringUtils.collectionToCommaDelimitedString(this.annotatedClasses) + "]");
        }
        reader.register(this.annotatedClasses.toArray(new Class<?>[this.annotatedClasses.size()]));
    }
    if (!this.basePackages.isEmpty()) {
        if (logger.isInfoEnabled()) {
            logger.info("Scanning base packages: [" + StringUtils.collectionToCommaDelimitedString(this.basePackages) + "]");
        }
        scanner.scan(this.basePackages.toArray(new String[this.basePackages.size()]));
    }
    String[] configLocations = getConfigLocations();
    if (configLocations != null) {
        for (String configLocation : configLocations) {
            try {
                Class<?> clazz = getClassLoader().loadClass(configLocation);
                if (logger.isInfoEnabled()) {
                    logger.info("Successfully resolved class for [" + configLocation + "]");
                }
                reader.register(clazz);
            } catch (ClassNotFoundException ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Could not load class for config location [" + configLocation + "] - trying package scan. " + ex);
                }
                int count = scanner.scan(configLocation);
                if (logger.isInfoEnabled()) {
                    if (count == 0) {
                        logger.info("No annotated classes found for specified class/package [" + configLocation + "]");
                    } else {
                        logger.info("Found " + count + " annotated classes in package [" + configLocation + "]");
                    }
                }
            }
        }
    }
}
Also used : ClassPathBeanDefinitionScanner(org.springframework.context.annotation.ClassPathBeanDefinitionScanner) BeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator) ScopeMetadataResolver(org.springframework.context.annotation.ScopeMetadataResolver) AnnotatedBeanDefinitionReader(org.springframework.context.annotation.AnnotatedBeanDefinitionReader)

Example 8 with AnnotatedBeanDefinitionReader

use of org.springframework.context.annotation.AnnotatedBeanDefinitionReader in project spring-framework by spring-projects.

the class EnvironmentSecurityManagerIntegrationTests method securityManagerDisallowsAccessToSystemEnvironmentButAllowsAccessToIndividualKeys.

@Test
public void securityManagerDisallowsAccessToSystemEnvironmentButAllowsAccessToIndividualKeys() {
    SecurityManager securityManager = new SecurityManager() {

        @Override
        public void checkPermission(Permission perm) {
            // ReadOnlySystemAttributesMap will come into play.
            if ("getenv.*".equals(perm.getName())) {
                throw new AccessControlException("Accessing the system environment is disallowed");
            }
        }
    };
    System.setSecurityManager(securityManager);
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(bf);
    reader.register(C1.class);
    assertThat(bf.containsBean("c1"), is(true));
}
Also used : Permission(java.security.Permission) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) AccessControlException(java.security.AccessControlException) AnnotatedBeanDefinitionReader(org.springframework.context.annotation.AnnotatedBeanDefinitionReader) Test(org.junit.Test)

Example 9 with AnnotatedBeanDefinitionReader

use of org.springframework.context.annotation.AnnotatedBeanDefinitionReader in project spring-framework by spring-projects.

the class AnnotationConfigWebContextLoader method loadBeanDefinitions.

// --- AbstractGenericWebContextLoader -------------------------------------
/**
	 * Register classes in the supplied {@linkplain GenericWebApplicationContext context}
	 * from the classes in the supplied {@link WebMergedContextConfiguration}.
	 *
	 * <p>Each class must represent an <em>annotated class</em>. An
	 * {@link AnnotatedBeanDefinitionReader} is used to register the appropriate
	 * bean definitions.
	 *
	 * @param context the context in which the annotated classes should be registered
	 * @param webMergedConfig the merged configuration from which the classes should be retrieved
	 *
	 * @see AbstractGenericWebContextLoader#loadBeanDefinitions
	 */
@Override
protected void loadBeanDefinitions(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) {
    Class<?>[] annotatedClasses = webMergedConfig.getClasses();
    if (logger.isDebugEnabled()) {
        logger.debug("Registering annotated classes: " + ObjectUtils.nullSafeToString(annotatedClasses));
    }
    new AnnotatedBeanDefinitionReader(context).register(annotatedClasses);
}
Also used : AnnotatedBeanDefinitionReader(org.springframework.context.annotation.AnnotatedBeanDefinitionReader)

Example 10 with AnnotatedBeanDefinitionReader

use of org.springframework.context.annotation.AnnotatedBeanDefinitionReader in project spring-framework by spring-projects.

the class CustomRequestAttributesRequestContextHolderTests method setUp.

@Before
public void setUp() {
    ServletContext servletContext = new MockServletContext();
    MockHttpServletRequest mockRequest = new MockHttpServletRequest(servletContext);
    mockRequest.setAttribute(FROM_CUSTOM_MOCK, FROM_CUSTOM_MOCK);
    RequestContextHolder.setRequestAttributes(new ServletWebRequest(mockRequest, new MockHttpServletResponse()));
    this.wac.setServletContext(servletContext);
    new AnnotatedBeanDefinitionReader(this.wac).register(WebConfig.class);
    this.wac.refresh();
    this.mockMvc = webAppContextSetup(this.wac).defaultRequest(get("/").requestAttr(FROM_MVC_TEST_DEFAULT, FROM_MVC_TEST_DEFAULT)).alwaysExpect(status().isOk()).build();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.MockServletContext) ServletContext(javax.servlet.ServletContext) AnnotatedBeanDefinitionReader(org.springframework.context.annotation.AnnotatedBeanDefinitionReader) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Before(org.junit.Before)

Aggregations

AnnotatedBeanDefinitionReader (org.springframework.context.annotation.AnnotatedBeanDefinitionReader)10 Test (org.junit.Test)3 AccessControlException (java.security.AccessControlException)2 Permission (java.security.Permission)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 ServletContext (javax.servlet.ServletContext)1 Car (org.atinject.tck.auto.Car)1 Before (org.junit.Before)1 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)1 BeanNameGenerator (org.springframework.beans.factory.support.BeanNameGenerator)1 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)1 ClassPathBeanDefinitionScanner (org.springframework.context.annotation.ClassPathBeanDefinitionScanner)1 Jsr330ScopeMetadataResolver (org.springframework.context.annotation.Jsr330ScopeMetadataResolver)1 ScopeMetadataResolver (org.springframework.context.annotation.ScopeMetadataResolver)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1 MockServletContext (org.springframework.mock.web.MockServletContext)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1