List of usage examples for org.springframework.beans.factory.support GenericBeanDefinition setScope
@Override public void setScope(@Nullable String scope)
From source file:com.haulmont.cuba.core.sys.SpringBeanLoader.java
public void updateContext(Collection<Class> classes) { if (beanFactory != null) { boolean needToRefreshRemotingContext = false; for (Class clazz : classes) { Service serviceAnnotation = (Service) clazz.getAnnotation(Service.class); ManagedBean managedBeanAnnotation = (ManagedBean) clazz.getAnnotation(ManagedBean.class); Component componentAnnotation = (Component) clazz.getAnnotation(Component.class); Controller controllerAnnotation = (Controller) clazz.getAnnotation(Controller.class); String beanName = null; if (serviceAnnotation != null) { beanName = serviceAnnotation.value(); } else if (managedBeanAnnotation != null) { beanName = managedBeanAnnotation.value(); } else if (componentAnnotation != null) { beanName = componentAnnotation.value(); } else if (controllerAnnotation != null) { beanName = controllerAnnotation.value(); }/*from w ww . j a va 2 s . c o m*/ if (StringUtils.isNotBlank(beanName)) { GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); beanDefinition.setBeanClass(clazz); Scope scope = (Scope) clazz.getAnnotation(Scope.class); if (scope != null) { beanDefinition.setScope(scope.value()); } beanFactory.registerBeanDefinition(beanName, beanDefinition); } if (StringUtils.isNotBlank(beanName)) { needToRefreshRemotingContext = true; } } if (needToRefreshRemotingContext) { ApplicationContext remotingContext = RemotingContextHolder.getRemotingApplicationContext(); if (remotingContext != null && remotingContext instanceof ConfigurableApplicationContext) { ((ConfigurableApplicationContext) remotingContext).refresh(); } } } }
From source file:com.fitbur.jestify.junit.spring.IntegrationTestReifier.java
@Override public Object reifyCut(CutDescriptor cutDescriptor, Object[] arguments) { return AccessController.doPrivileged((PrivilegedAction<Object>) () -> { try {//from w w w. j a v a 2 s. c o m Cut cut = cutDescriptor.getCut(); Field field = cutDescriptor.getField(); Type fieldType = field.getGenericType(); String fieldName = field.getName(); field.setAccessible(true); Constructor<?> constructor = cutDescriptor.getConstructor(); constructor.setAccessible(true); ResolvableType resolver = ResolvableType.forType(fieldType); Class rawType; if (resolver.hasGenerics()) { if (resolver.isAssignableFrom(Provider.class) || resolver.isAssignableFrom(Optional.class)) { rawType = resolver.getRawClass(); } else { rawType = resolver.resolve(); } } else { rawType = resolver.resolve(); } Module module = testInstance.getClass().getDeclaredAnnotation(Module.class); GenericBeanDefinition bean = new GenericBeanDefinition(); bean.setBeanClass(rawType); bean.setAutowireCandidate(false); bean.setScope(SCOPE_PROTOTYPE); bean.setPrimary(true); bean.setLazyInit(true); bean.setRole(RootBeanDefinition.ROLE_APPLICATION); bean.setAutowireMode(RootBeanDefinition.AUTOWIRE_NO); appContext.registerBeanDefinition(fieldName, bean); if (module != null) { appContext.register(module.value()); } appContext.refresh(); Object instance = appContext.getBean(fieldName, arguments); if (cut.value()) { instance = spy(instance); } field.set(testInstance, instance); return instance; } catch (SecurityException | IllegalAccessException | IllegalArgumentException e) { throw new RuntimeException(e); } }); }
From source file:com.techtrip.spring.beans.factory.ContextAwareBeanFactory.java
/** * Register bean./*from w w w . ja v a 2 s . co m*/ * * @param beanToRegister the bean to register * @param beanName the bean name * @param scope the scope * @param setLazyInit the set lazy init * @param setAutowireCandidate the set autowire candidate */ public void registerBean(Class<?> beanToRegister, String beanName, String scope /* "session" */, boolean setLazyInit, boolean setAutowireCandidate) { GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); beanDefinition.setBeanClass(beanToRegister); beanDefinition.setLazyInit(setLazyInit); beanDefinition.setAbstract(false); beanDefinition.setAutowireCandidate(setAutowireCandidate); beanDefinition.setScope(scope); registry.registerBeanDefinition(beanName, beanDefinition); }
From source file:org.romaz.spring.scripting.ext.ExtScriptBeanDefinitionParser.java
/** * Parses the dynamic object element and returns the resulting bean definition. * Registers a {@link ScriptFactoryPostProcessor} if needed. *//* w w w.j a v a 2 s. c om*/ @Override protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { // Resolve the script source. String value = resolveScriptSource(element, parserContext.getReaderContext()); if (value == null) { return null; } // Set up infrastructure. LangNamespaceUtils.registerScriptFactoryPostProcessorIfNecessary(parserContext.getRegistry()); // Create script factory bean definition. GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClassName(this.scriptFactoryClassName); bd.setSource(parserContext.extractSource(element)); // Determine bean scope. String scope = element.getAttribute(SCOPE_ATTRIBUTE); if (StringUtils.hasLength(scope)) { bd.setScope(scope); } // Determine autowire mode. String autowire = element.getAttribute(AUTOWIRE_ATTRIBUTE); int autowireMode = parserContext.getDelegate().getAutowireMode(autowire); // Only "byType" and "byName" supported, but maybe other default inherited... if (autowireMode == GenericBeanDefinition.AUTOWIRE_AUTODETECT) { autowireMode = GenericBeanDefinition.AUTOWIRE_BY_TYPE; } else if (autowireMode == GenericBeanDefinition.AUTOWIRE_CONSTRUCTOR) { autowireMode = GenericBeanDefinition.AUTOWIRE_NO; } bd.setAutowireMode(autowireMode); // Determine dependency check setting. String dependencyCheck = element.getAttribute(DEPENDENCY_CHECK_ATTRIBUTE); bd.setDependencyCheck(parserContext.getDelegate().getDependencyCheck(dependencyCheck)); // Retrieve the defaults for bean definitions within this parser context BeanDefinitionDefaults beanDefinitionDefaults = parserContext.getDelegate().getBeanDefinitionDefaults(); // Determine init method and destroy method. String initMethod = element.getAttribute(INIT_METHOD_ATTRIBUTE); if (StringUtils.hasLength(initMethod)) { bd.setInitMethodName(initMethod); } else if (beanDefinitionDefaults.getInitMethodName() != null) { bd.setInitMethodName(beanDefinitionDefaults.getInitMethodName()); } String destroyMethod = element.getAttribute(DESTROY_METHOD_ATTRIBUTE); if (StringUtils.hasLength(destroyMethod)) { bd.setDestroyMethodName(destroyMethod); } else if (beanDefinitionDefaults.getDestroyMethodName() != null) { bd.setDestroyMethodName(beanDefinitionDefaults.getDestroyMethodName()); } // Attach any refresh metadata. String refreshCheckDelay = element.getAttribute(REFRESH_CHECK_DELAY_ATTRIBUTE); if (StringUtils.hasText(refreshCheckDelay)) { bd.setAttribute(ScriptFactoryPostProcessor.REFRESH_CHECK_DELAY_ATTRIBUTE, new Long(refreshCheckDelay)); } // Add constructor arguments. ConstructorArgumentValues cav = bd.getConstructorArgumentValues(); int constructorArgNum = 0; cav.addIndexedArgumentValue(constructorArgNum++, value); if (element.hasAttribute(SCRIPT_INTERFACES_ATTRIBUTE)) { cav.addIndexedArgumentValue(constructorArgNum++, element.getAttribute(SCRIPT_INTERFACES_ATTRIBUTE)); } if (element.hasAttribute(SCRIPT_CONFIG_ATTRIBUTE)) { cav.addIndexedArgumentValue(constructorArgNum++, new RuntimeBeanReference(element.getAttribute(SCRIPT_CONFIG_ATTRIBUTE))); } // Add any property definitions that need adding. parserContext.getDelegate().parsePropertyElements(element, bd); return bd; }
From source file:com.fitbur.testify.di.spring.SpringServiceLocator.java
@Override public void addService(ServiceDescriptor descriptor) { checkState(!context.containsBeanDefinition(descriptor.getName()), "Service with the name '%s' already exists.", descriptor.getName()); GenericBeanDefinition bean = new GenericBeanDefinition(); bean.setBeanClass(descriptor.getType()); bean.setAutowireCandidate(descriptor.getDiscoverable()); bean.setPrimary(descriptor.getPrimary()); bean.setLazyInit(descriptor.getLazy()); bean.setRole(ROLE_APPLICATION);/*w w w . ja v a2s .c om*/ if (descriptor.getInjectable()) { bean.setAutowireMode(AUTOWIRE_CONSTRUCTOR); } else { bean.setAutowireMode(AUTOWIRE_NO); ConstructorArgumentValues values = new ConstructorArgumentValues(); Object[] arguments = descriptor.getArguments(); for (int i = 0; i < arguments.length; i++) { Object arg = arguments[i]; if (arg == null) { //TODO: warn user that the argument was not specified and there //for the real instance will be injected. continue; } values.addIndexedArgumentValue(i, arg); } bean.setConstructorArgumentValues(values); } ServiceScope scope = descriptor.getScope(); switch (scope) { case PROTOTYPE: bean.setScope("prototype"); break; case SINGLETON: bean.setScope("singleton"); break; case REQUEST: bean.setScope("request"); break; case SESSION: bean.setScope("session"); break; case APPLICATION: bean.setScope("application"); break; default: checkState(false, "Scope '{}' is not supported by Spring IoC.", scope.name()); } DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory(); beanFactory.registerBeanDefinition(descriptor.getName(), bean); }
From source file:org.beangle.spring.bind.AutoConfigProcessor.java
/** * @param beanName/*from www .j av a 2 s .co m*/ * @param clazz * @param registry * @param definition */ protected BeanDefinition register(String beanName, Class<?> clazz, String scope, BeanDefinitionRegistry registry) { GenericBeanDefinition def = new GenericBeanDefinition(); def.setBeanClass(clazz); def.setScope(scope); registry.registerBeanDefinition(beanName, def); bindRegistry.register(clazz, beanName); logger.debug("Register definition {} for {}", beanName, clazz); return def; }
From source file:org.springframework.aop.framework.autoproxy.target.AbstractBeanFactoryBasedTargetSourceCreator.java
@Override @Nullable/*from w w w.j a v a 2s . com*/ public final TargetSource getTargetSource(Class<?> beanClass, String beanName) { AbstractBeanFactoryBasedTargetSource targetSource = createBeanFactoryBasedTargetSource(beanClass, beanName); if (targetSource == null) { return null; } if (logger.isDebugEnabled()) { logger.debug("Configuring AbstractBeanFactoryBasedTargetSource: " + targetSource); } DefaultListableBeanFactory internalBeanFactory = getInternalBeanFactoryForBean(beanName); // We need to override just this bean definition, as it may reference other beans // and we're happy to take the parent's definition for those. // Always use prototype scope if demanded. BeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName); GenericBeanDefinition bdCopy = new GenericBeanDefinition(bd); if (isPrototypeBased()) { bdCopy.setScope(BeanDefinition.SCOPE_PROTOTYPE); } internalBeanFactory.registerBeanDefinition(beanName, bdCopy); // Complete configuring the PrototypeTargetSource. targetSource.setTargetBeanName(beanName); targetSource.setBeanFactory(internalBeanFactory); return targetSource; }
From source file:org.springframework.context.expression.ApplicationContextExpressionTests.java
@Test public void genericApplicationContext() throws Exception { GenericApplicationContext ac = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(ac); ac.getBeanFactory().registerScope("myScope", new Scope() { @Override/*ww w .j av a2s .co m*/ public Object get(String name, ObjectFactory<?> objectFactory) { return objectFactory.getObject(); } @Override public Object remove(String name) { return null; } @Override public void registerDestructionCallback(String name, Runnable callback) { } @Override public Object resolveContextualObject(String key) { if (key.equals("mySpecialAttr")) { return "42"; } else { return null; } } @Override public String getConversationId() { return null; } }); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties placeholders = new Properties(); placeholders.setProperty("code", "123"); ppc.setProperties(placeholders); ac.addBeanFactoryPostProcessor(ppc); GenericBeanDefinition bd0 = new GenericBeanDefinition(); bd0.setBeanClass(TestBean.class); bd0.getPropertyValues().add("name", "myName"); bd0.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "original")); ac.registerBeanDefinition("tb0", bd0); GenericBeanDefinition bd1 = new GenericBeanDefinition(); bd1.setBeanClassName("#{tb0.class}"); bd1.setScope("myScope"); bd1.getConstructorArgumentValues().addGenericArgumentValue("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ"); bd1.getConstructorArgumentValues().addGenericArgumentValue("#{mySpecialAttr}"); ac.registerBeanDefinition("tb1", bd1); GenericBeanDefinition bd2 = new GenericBeanDefinition(); bd2.setBeanClassName("#{tb1.class.name}"); bd2.setScope("myScope"); bd2.getPropertyValues().add("name", "{ XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ }"); bd2.getPropertyValues().add("age", "#{mySpecialAttr}"); bd2.getPropertyValues().add("country", "${code} #{systemProperties.country}"); ac.registerBeanDefinition("tb2", bd2); GenericBeanDefinition bd3 = new GenericBeanDefinition(); bd3.setBeanClass(ValueTestBean.class); bd3.setScope("myScope"); ac.registerBeanDefinition("tb3", bd3); GenericBeanDefinition bd4 = new GenericBeanDefinition(); bd4.setBeanClass(ConstructorValueTestBean.class); bd4.setScope("myScope"); ac.registerBeanDefinition("tb4", bd4); GenericBeanDefinition bd5 = new GenericBeanDefinition(); bd5.setBeanClass(MethodValueTestBean.class); bd5.setScope("myScope"); ac.registerBeanDefinition("tb5", bd5); GenericBeanDefinition bd6 = new GenericBeanDefinition(); bd6.setBeanClass(PropertyValueTestBean.class); bd6.setScope("myScope"); ac.registerBeanDefinition("tb6", bd6); System.getProperties().put("country", "UK"); try { ac.refresh(); TestBean tb0 = ac.getBean("tb0", TestBean.class); TestBean tb1 = ac.getBean("tb1", TestBean.class); assertEquals("XXXmyNameYYY42ZZZ", tb1.getName()); assertEquals(42, tb1.getAge()); TestBean tb2 = ac.getBean("tb2", TestBean.class); assertEquals("{ XXXmyNameYYY42ZZZ }", tb2.getName()); assertEquals(42, tb2.getAge()); assertEquals("123 UK", tb2.getCountry()); ValueTestBean tb3 = ac.getBean("tb3", ValueTestBean.class); assertEquals("XXXmyNameYYY42ZZZ", tb3.name); assertEquals(42, tb3.age); assertEquals(42, tb3.ageFactory.getObject().intValue()); assertEquals("123 UK", tb3.country); assertEquals("123 UK", tb3.countryFactory.getObject()); System.getProperties().put("country", "US"); assertEquals("123 UK", tb3.country); assertEquals("123 US", tb3.countryFactory.getObject()); System.getProperties().put("country", "UK"); assertEquals("123 UK", tb3.country); assertEquals("123 UK", tb3.countryFactory.getObject()); assertSame(tb0, tb3.tb); tb3 = (ValueTestBean) SerializationTestUtils.serializeAndDeserialize(tb3); assertEquals("123 UK", tb3.countryFactory.getObject()); ConstructorValueTestBean tb4 = ac.getBean("tb4", ConstructorValueTestBean.class); assertEquals("XXXmyNameYYY42ZZZ", tb4.name); assertEquals(42, tb4.age); assertEquals("123 UK", tb4.country); assertSame(tb0, tb4.tb); MethodValueTestBean tb5 = ac.getBean("tb5", MethodValueTestBean.class); assertEquals("XXXmyNameYYY42ZZZ", tb5.name); assertEquals(42, tb5.age); assertEquals("123 UK", tb5.country); assertSame(tb0, tb5.tb); PropertyValueTestBean tb6 = ac.getBean("tb6", PropertyValueTestBean.class); assertEquals("XXXmyNameYYY42ZZZ", tb6.name); assertEquals(42, tb6.age); assertEquals("123 UK", tb6.country); assertSame(tb0, tb6.tb); } finally { System.getProperties().remove("country"); } }