/** * Create a new AnnotationConfigApplicationContext that needs to be populated * through {@link #register} calls and then manually {@linkplain #refresh refreshed}. */ public AnnotationConfigApplicationContext() { this.reader = new AnnotatedBeanDefinitionReader(this); this.scanner = new ClassPathBeanDefinitionScanner(this); }
/** * Create a new AnnotationConfigApplicationContext with the given DefaultListableBeanFactory. * @param beanFactory the DefaultListableBeanFactory instance to use for this context */ public AnnotationConfigApplicationContext(DefaultListableBeanFactory beanFactory) { super(beanFactory); this.reader = new AnnotatedBeanDefinitionReader(this); this.scanner = new ClassPathBeanDefinitionScanner(this); }
/** * Build an {@link AnnotatedBeanDefinitionReader} for the given bean factory. * <p>This should be pre-configured with the {@code Environment} (if desired) * but not with a {@code BeanNameGenerator} or {@code ScopeMetadataResolver} yet. * @param beanFactory the bean factory to load bean definitions into * @since 4.1.9 * @see #getEnvironment() * @see #getBeanNameGenerator() * @see #getScopeMetadataResolver() */ protected AnnotatedBeanDefinitionReader getAnnotatedBeanDefinitionReader(DefaultListableBeanFactory beanFactory) { return new AnnotatedBeanDefinitionReader(beanFactory, getEnvironment()); }
/** * 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); } }
/** * Create a new AnnotationConfigApplicationContext that needs to be populated * through {@link #register} calls and then manually {@linkplain #refresh refreshed}. */ public AnnotationConfigApplicationContext() { this.reader = new AnnotatedBeanDefinitionReader(this); this.scanner = new ClassPathBeanDefinitionScanner(this); }
/** * Create a new AnnotationConfigApplicationContext with the given DefaultListableBeanFactory. * @param beanFactory the DefaultListableBeanFactory instance to use for this context */ public AnnotationConfigApplicationContext(DefaultListableBeanFactory beanFactory) { super(beanFactory); this.reader = new AnnotatedBeanDefinitionReader(this); this.scanner = new ClassPathBeanDefinitionScanner(this); }
/** * Build an {@link AnnotatedBeanDefinitionReader} for the given bean factory. * <p>This should be pre-configured with the {@code Environment} (if desired) * but not with a {@code BeanNameGenerator} or {@code ScopeMetadataResolver} yet. * @param beanFactory the bean factory to load bean definitions into * @since 4.1.9 * @see #getEnvironment() * @see #getBeanNameGenerator() * @see #getScopeMetadataResolver() */ protected AnnotatedBeanDefinitionReader getAnnotatedBeanDefinitionReader(DefaultListableBeanFactory beanFactory) { return new AnnotatedBeanDefinitionReader(beanFactory, getEnvironment()); }
/** * Create a new {@link AnnotationConfigReactiveWebServerApplicationContext} with the * given {@code DefaultListableBeanFactory}. The context needs to be populated through * {@link #register} calls and then manually {@linkplain #refresh refreshed}. * @param beanFactory the DefaultListableBeanFactory instance to use for this context */ public AnnotationConfigReactiveWebServerApplicationContext( DefaultListableBeanFactory beanFactory) { super(beanFactory); this.reader = new AnnotatedBeanDefinitionReader(this); this.scanner = new ClassPathBeanDefinitionScanner(this); }
/** * 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); } }
@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()); }
public static Test suite() { GenericApplicationContext ac = new GenericApplicationContext(); AnnotatedBeanDefinitionReader bdr = new AnnotatedBeanDefinitionReader(ac); bdr.setScopeMetadataResolver(new Jsr330ScopeMetadataResolver()); bdr.registerBean(Convertible.class); bdr.registerBean(DriversSeat.class, Drivers.class); bdr.registerBean(Seat.class, Primary.class); bdr.registerBean(V8Engine.class); bdr.registerBean(SpareTire.class, "spare"); bdr.registerBean(Cupholder.class); bdr.registerBean(Tire.class, Primary.class); bdr.registerBean(FuelTank.class); ac.refresh(); Car car = ac.getBean(Car.class); return Tck.testsFor(car, false, true); }
@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(); }