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); }
@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)); }
@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(); }
@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)); }
private void loadContexts(Element element, BeanDefinitionRegistry beanDefinitionRegistry) { String externalDatabase = element.getAttribute("externalDatabase"); if (StringUtils.isBlank(externalDatabase) || externalDatabase.equals(Boolean.FALSE.toString())) { AnnotatedBeanDefinitionReader definitionReader = new AnnotatedBeanDefinitionReader(beanDefinitionRegistry); definitionReader.register(ScoreDefaultDatasourceContext.class); definitionReader.register(ScoreDatabaseContext.class); } String repositoriesContextPath = "META-INF/spring/score/context/scoreRepositoryContext.xml"; String ignoreEngineJobs = element.getAttribute("ignoreEngineJobs"); if(StringUtils.isNotBlank(ignoreEngineJobs) && ignoreEngineJobs.equals(Boolean.TRUE.toString())){ new XmlBeanDefinitionReader(beanDefinitionRegistry).loadBeanDefinitions(repositoriesContextPath); } else{ new XmlBeanDefinitionReader(beanDefinitionRegistry).loadBeanDefinitions(repositoriesContextPath,ENGINE_JOBS_CONTEXT_LOCATION); } }
@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)); }
/** * Create a new {@link BeanDefinitionLoader} that will load beans into the specified * {@link BeanDefinitionRegistry}. * @param registry the bean definition registry that will contain the loaded beans * @param sources the bean sources */ BeanDefinitionLoader(BeanDefinitionRegistry registry, Object... sources) { Assert.notNull(registry, "Registry must not be null"); Assert.notEmpty(sources, "Sources must not be empty"); this.sources = sources; this.annotatedReader = new AnnotatedBeanDefinitionReader(registry); this.xmlReader = new XmlBeanDefinitionReader(registry); if (isGroovyPresent()) { this.groovyReader = new GroovyBeanDefinitionReader(registry); } this.scanner = new ClassPathBeanDefinitionScanner(registry); this.scanner.addExcludeFilter(new ClassExcludeFilter(sources)); }
@Override public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedContextConfiguration) { BeanDefinitionRegistry registry = getBeanDefinitionRegistry(context); AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader( registry); registerCleanupPostProcessor(registry, reader); registerImportsConfiguration(registry, reader); }
private void registerCleanupPostProcessor(BeanDefinitionRegistry registry, AnnotatedBeanDefinitionReader reader) { BeanDefinition definition = registerBean(registry, reader, ImportsCleanupPostProcessor.BEAN_NAME, ImportsCleanupPostProcessor.class); definition.getConstructorArgumentValues().addIndexedArgumentValue(0, this.testClass); }
@SuppressWarnings("unchecked") private BeanDefinition registerBean(BeanDefinitionRegistry registry, AnnotatedBeanDefinitionReader reader, String beanName, Class<?> type) { reader.registerBean(type, beanName); BeanDefinition definition = registry.getBeanDefinition(beanName); return definition; }
private static void initAnnotatedConf(GenericApplicationContext context){ Class componentScanAnnotated=ProcessInfo.getAnnotationInitClass(); if(componentScanAnnotated!=null){ AnnotatedGenericBeanDefinition def=new AnnotatedGenericBeanDefinition(ProcessInfo.getAnnotationInitClass()); AnnotatedBeanDefinitionReader reader=new AnnotatedBeanDefinitionReader(context); AnnotationScopeMetadataResolver resolver=new AnnotationScopeMetadataResolver(); resolver.resolveScopeMetadata(def); reader.setScopeMetadataResolver(resolver); } }
@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)); }
@Override public AnnotatedClassViewRegistrar register (Class<?> first, Class<?>... rest) { try (GenericWebApplicationContext context = createContext ()) { AnnotatedBeanDefinitionReader reader = createAnnotatedReader (context); reader.register (first); reader.register (rest); context.refresh (); mergeViews (context); } return this; }
@Test public void testAnnotatedBeanDefinitionReader() { AnnotatedBeanDefinitionReader annotatedReader = new AnnotatedBeanDefinitionReader(beanDefinitionRegistry); annotatedReader.register(CustomComponentProviderApplication.class); }
public SpringBatchApplicationContext(boolean allowBeanDefinitionOverriding) { super(); this.annotatedBeanReader = new AnnotatedBeanDefinitionReader(this); this.xmlBeanReader = new XmlBeanDefinitionReader(this); setAllowBeanDefinitionOverriding(allowBeanDefinitionOverriding); }
@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()); }
@Override public void initialize(GenericApplicationContext applicationContext) { new AnnotatedBeanDefinitionReader(applicationContext).register(GlobalConfig.class); }
private void registerImportsConfiguration(BeanDefinitionRegistry registry, AnnotatedBeanDefinitionReader reader) { BeanDefinition definition = registerBean(registry, reader, ImportsConfiguration.BEAN_NAME, ImportsConfiguration.class); definition.setAttribute(TEST_CLASS_ATTRIBUTE, this.testClass); }
protected void loadBeanDefinitions(GenericWebApplicationContext context, MergedContextConfiguration mergedConfig) { new AnnotatedBeanDefinitionReader(context).register(mergedConfig.getClasses()); loadBeanDefinitions(context, mergedConfig.getLocations()); }
protected AnnotatedBeanDefinitionReader createAnnotatedReader (GenericWebApplicationContext context) { AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader (context); reader.setEnvironment (getApplicationContext ().getEnvironment ()); return reader; }
/** * 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); }
/** * 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); }
/** * 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()); }