/** * Register one or more annotated classes to be processed. * <p>Note that {@link #refresh()} must be called in order for the context * to fully process the new classes. * @param annotatedClasses one or more annotated classes, * e.g. {@link Configuration @Configuration} classes * @see #scan(String...) * @see #refresh() */ public void register(Class<?>... annotatedClasses) { Assert.notEmpty(annotatedClasses, "At least one annotated class must be specified"); this.reader.register(annotatedClasses); }
/** * Register one or more annotated classes to be processed. * <p>Note that {@link #refresh()} must be called in order for the context * to fully process the new classes. * @param annotatedClasses one or more annotated classes, * e.g. {@link Configuration @Configuration} classes * @see #scan(String...) * @see #refresh() */ public void register(Class<?>... annotatedClasses) { Assert.notEmpty(annotatedClasses, "At least one annotated class must be specified"); this.reader.register(annotatedClasses); }
/** * Register Beans * * @param registry {@link BeanDefinitionRegistry} * @param annotatedClasses {@link Annotation annotation} class */ public static void registerBeans(BeanDefinitionRegistry registry, Class<?>... annotatedClasses) { if (ObjectUtils.isEmpty(annotatedClasses)) { return; } boolean debugEnabled = logger.isDebugEnabled(); AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(registry); if (debugEnabled) { logger.debug(registry.getClass().getSimpleName() + " will register annotated classes : " + Arrays.asList(annotatedClasses) + " ."); } reader.register(annotatedClasses); } }
/** * Register Beans * * @param registry {@link BeanDefinitionRegistry} * @param annotatedClasses {@link Annotation annotation} class */ public static void registerBeans(BeanDefinitionRegistry registry, Class<?>... annotatedClasses) { if (ObjectUtils.isEmpty(annotatedClasses)) { return; } boolean debugEnabled = logger.isDebugEnabled(); AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(registry); if (debugEnabled) { logger.debug(registry.getClass().getSimpleName() + " will register annotated classes : " + Arrays.asList(annotatedClasses) + " ."); } reader.register(annotatedClasses); } }
private void register(AnnotatedBeanDefinitionReader reader, String configLocation) throws ClassNotFoundException, LinkageError { Class<?> clazz = ClassUtils.forName(configLocation, getClassLoader()); if (this.logger.isInfoEnabled()) { this.logger.info("Successfully resolved class for [" + configLocation + "]"); } reader.register(clazz); }
/** * 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); }
/** * Register classes in the supplied {@link GenericApplicationContext context} * from the classes in the supplied {@link MergedContextConfiguration}. * <p>Each class must represent an <em>annotated class</em>. An * {@link AnnotatedBeanDefinitionReader} is used to register the appropriate * bean definitions. * <p>Note that this method does not call {@link #createBeanDefinitionReader} * since {@code AnnotatedBeanDefinitionReader} is not an instance of * {@link BeanDefinitionReader}. * @param context the context in which the annotated classes should be registered * @param mergedConfig the merged configuration from which the classes should be retrieved * @see AbstractGenericContextLoader#loadBeanDefinitions */ @Override protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { Class<?>[] annotatedClasses = mergedConfig.getClasses(); if (logger.isDebugEnabled()) { logger.debug("Registering annotated classes: " + ObjectUtils.nullSafeToString(annotatedClasses)); } new AnnotatedBeanDefinitionReader(context).register(annotatedClasses); }
@Override public void initialize(GenericApplicationContext applicationContext) { new AnnotatedBeanDefinitionReader(applicationContext).register(GlobalConfig.class); } }
@Override public void initialize(GenericApplicationContext applicationContext) { new AnnotatedBeanDefinitionReader(applicationContext).register(FooConfig.class); } }
private void registerAnnotatedClasses(AnnotatedBeanDefinitionReader reader) { if (this.logger.isInfoEnabled()) { this.logger.info("Registering annotated classes: [" + StringUtils.collectionToCommaDelimitedString(this.annotatedClasses) + "]"); } reader.register(ClassUtils.toClassArray(this.annotatedClasses)); }
@Override protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { super.postProcessBeanFactory(beanFactory); if (!ObjectUtils.isEmpty(this.basePackages)) { this.scanner.scan(this.basePackages); } if (!this.annotatedClasses.isEmpty()) { this.reader.register(ClassUtils.toClassArray(this.annotatedClasses)); } }
private int load(Class<?> source) { if (isGroovyPresent() && GroovyBeanDefinitionSource.class.isAssignableFrom(source)) { // Any GroovyLoaders added in beans{} DSL can contribute beans here GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source, GroovyBeanDefinitionSource.class); load(loader); } if (isComponent(source)) { this.annotatedReader.register(source); return 1; } return 0; }
@Override protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { super.postProcessBeanFactory(beanFactory); if (this.basePackages != null && this.basePackages.length > 0) { this.scanner.scan(this.basePackages); } if (!this.annotatedClasses.isEmpty()) { this.reader.register(ClassUtils.toClassArray(this.annotatedClasses)); } }
@Test public void securityManagerDisallowsAccessToSystemEnvironmentButAllowsAccessToIndividualKeys() { SecurityManager securityManager = new SecurityManager() { @Override public void checkPermission(Permission perm) { // Disallowing access to System#getenv means that our // 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)); }
@Test public void securityManagerDisallowsAccessToSystemEnvironmentAndDisallowsAccessToIndividualKey() { SecurityManager securityManager = new SecurityManager() { @Override public void checkPermission(Permission perm) { // Disallowing access to System#getenv means that our // ReadOnlySystemAttributesMap will come into play. if ("getenv.*".equals(perm.getName())) { throw new AccessControlException("Accessing the system environment is disallowed"); } // Disallowing access to the spring.profiles.active property means that // the BeanDefinitionReader won't be able to determine which profiles are // active. We should see an INFO-level message in the console about this // and as a result, any components marked with a non-default profile will // be ignored. if (("getenv." + AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME).equals(perm.getName())) { throw new AccessControlException( format("Accessing system environment variable [%s] is disallowed", AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME)); } } }; System.setSecurityManager(securityManager); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(bf); reader.register(C1.class); assertThat(bf.containsBean("c1"), is(false)); }
@Test public void annotatedBeanDefinitionReader_inheritsEnvironmentFromEnvironmentCapableBDR() { GenericApplicationContext ctx = new GenericApplicationContext(); ctx.setEnvironment(prodEnv); new AnnotatedBeanDefinitionReader(ctx).register(Config.class); ctx.refresh(); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
@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()); }
@Test public void cronTaskWithScopedProxy() { BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class); context.registerBeanDefinition("postProcessor", processorDefinition); new AnnotatedBeanDefinitionReader(context).register(ProxiedCronTestBean.class, ProxiedCronTestBeanDependent.class); context.refresh(); ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class); assertEquals(1, postProcessor.getScheduledTasks().size()); ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar) new DirectFieldAccessor(postProcessor).getPropertyValue("registrar"); @SuppressWarnings("unchecked") List<CronTask> cronTasks = (List<CronTask>) new DirectFieldAccessor(registrar).getPropertyValue("cronTasks"); assertEquals(1, cronTasks.size()); CronTask task = cronTasks.get(0); ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable(); Object targetObject = runnable.getTarget(); Method targetMethod = runnable.getMethod(); assertEquals(context.getBean(ScopedProxyUtils.getTargetBeanName("target")), targetObject); assertEquals("cron", targetMethod.getName()); assertEquals("*/7 * * * * ?", task.getExpression()); }
@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(); }