/** * Define an inner bean definition. * @param type the bean type * @return the bean definition */ public GenericBeanDefinition bean(Class<?> type) { GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); beanDefinition.setBeanClass(type); return beanDefinition; }
/** * Create a new GenericBeanDefinition for the given parent name and class name, * eagerly loading the bean class if a ClassLoader has been specified. * @param parentName the name of the parent bean, if any * @param className the name of the bean class, if any * @param classLoader the ClassLoader to use for loading bean classes * (can be {@code null} to just register bean classes by name) * @return the bean definition * @throws ClassNotFoundException if the bean class could not be loaded */ public static AbstractBeanDefinition createBeanDefinition( @Nullable String parentName, @Nullable String className, @Nullable ClassLoader classLoader) throws ClassNotFoundException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setParentName(parentName); if (className != null) { if (classLoader != null) { bd.setBeanClass(ClassUtils.forName(className, classLoader)); } else { bd.setBeanClassName(className); } } return bd; }
/** * Define an inner bean definition. * @param type the bean type * @return the bean definition */ public GenericBeanDefinition bean(Class<?> type) { GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); beanDefinition.setBeanClass(type); return beanDefinition; }
@Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); beanDefinition.setBeanClassName(String.class.getName()); registry.registerBeanDefinition("registrarImportedBean", beanDefinition); GenericBeanDefinition beanDefinition2 = new GenericBeanDefinition(); beanDefinition2.setBeanClass(OtherImportedConfig.class); registry.registerBeanDefinition("registrarImportedConfig", beanDefinition2); Assert.state(!called, "ImportedRegistrar called twice"); called = true; } }
private GenericBeanDefinition createObjectMapperFactoryDefinition(@Nullable Object source) { GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); beanDefinition.setBeanClass(Jackson2ObjectMapperFactoryBean.class); beanDefinition.setSource(source); beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); return beanDefinition; }
/** * Register a singleton bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerSingleton(String name, Class<?> clazz) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(clazz); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
/** * Register a prototype bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerPrototype(String name, Class<?> clazz) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE); bd.setBeanClass(clazz); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
/** * Register a singleton bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerSingleton(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(clazz); bd.setPropertyValues(pvs); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
private GenericBeanDefinition createObjectMapperFactoryDefinition(@Nullable Object source) { GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); beanDefinition.setBeanClass(Jackson2ObjectMapperFactoryBean.class); beanDefinition.setSource(source); beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); return beanDefinition; }
/** * Register a singleton bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerSingleton(String name, Class<?> clazz) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(clazz); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
/** * Register a prototype bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerPrototype(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE); bd.setBeanClass(clazz); bd.setPropertyValues(pvs); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
/** * Register a singleton bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerSingleton(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(clazz); bd.setPropertyValues(pvs); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
/** * Register a prototype bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerPrototype(String name, Class<?> clazz) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE); bd.setBeanClass(clazz); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
/** * register aspectJ for disconf get request * * @param registry */ private void registerAspect(BeanDefinitionRegistry registry) { GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); beanDefinition.setBeanClass(DisconfAspectJ.class); beanDefinition.setLazyInit(false); beanDefinition.setAbstract(false); beanDefinition.setAutowireCandidate(true); beanDefinition.setScope("singleton"); registry.registerBeanDefinition("disconfAspectJ", beanDefinition); }
/** * Register a prototype bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerPrototype(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE); bd.setBeanClass(clazz); bd.setPropertyValues(pvs); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
jacksonFactoryDef.setBeanClass(Jackson2ObjectMapperFactoryBean.class); jacksonFactoryDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); jacksonFactoryDef.setSource(source);
@Test public void stringConcatenationWithDebugLogging() { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(); GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(String.class); bd.getConstructorArgumentValues().addGenericArgumentValue("test-#{ T(java.lang.System).currentTimeMillis() }"); ac.registerBeanDefinition("str", bd); ac.refresh(); String str = ac.getBean("str", String.class); assertTrue(str.startsWith("test-")); }
@Test public void systemPropertiesSecurityManager() { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(); GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(TestBean.class); bd.getPropertyValues().add("country", "#{systemProperties.country}"); ac.registerBeanDefinition("tb", bd); SecurityManager oldSecurityManager = System.getSecurityManager(); try { System.setProperty("country", "NL"); SecurityManager securityManager = new SecurityManager() { @Override public void checkPropertiesAccess() { throw new AccessControlException("Not Allowed"); } @Override public void checkPermission(Permission perm) { // allow everything else } }; System.setSecurityManager(securityManager); ac.refresh(); TestBean tb = ac.getBean("tb", TestBean.class); assertEquals("NL", tb.getCountry()); } finally { System.setSecurityManager(oldSecurityManager); System.getProperties().remove("country"); } }
private WebApplicationContext initApplicationContext(String scope) { MockServletContext sc = new MockServletContext(); GenericWebApplicationContext ac = new GenericWebApplicationContext(sc); GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(DerivedTestBean.class); bd.setScope(scope); ac.registerBeanDefinition(NAME, bd); ac.refresh(); return ac; }
@Test public void testQualifiedByParentValue() { StaticApplicationContext parent = new StaticApplicationContext(); GenericBeanDefinition parentLarry = new GenericBeanDefinition(); parentLarry.setBeanClass(Person.class); parentLarry.getPropertyValues().add("name", "ParentLarry"); parentLarry.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "parentLarry")); parent.registerBeanDefinition("someLarry", parentLarry); GenericBeanDefinition otherLarry = new GenericBeanDefinition(); otherLarry.setBeanClass(Person.class); otherLarry.getPropertyValues().add("name", "OtherLarry"); otherLarry.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "otherLarry")); parent.registerBeanDefinition("someOtherLarry", otherLarry); parent.refresh(); StaticApplicationContext context = new StaticApplicationContext(parent); BeanDefinitionReader reader = new XmlBeanDefinitionReader(context); reader.loadBeanDefinitions(CONFIG_LOCATION); context.registerSingleton("testBean", QualifiedByParentValueTestBean.class); context.refresh(); QualifiedByParentValueTestBean testBean = (QualifiedByParentValueTestBean) context.getBean("testBean"); Person person = testBean.getLarry(); assertEquals("ParentLarry", person.getName()); }