List of usage examples for org.springframework.beans.factory.support RootBeanDefinition setFactoryBeanName
@Override public void setFactoryBeanName(@Nullable String factoryBeanName)
From source file:marshalsec.gadgets.SpringUtil.java
public static BeanFactory makeMethodTrigger(Object o, String method) throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition caller = new RootBeanDefinition(); caller.setFactoryBeanName("obj"); caller.setFactoryMethodName(method); Reflections.setFieldValue(caller.getMethodOverrides(), "overrides", new HashSet<>()); bf.registerBeanDefinition("caller", caller); Reflections.getField(DefaultListableBeanFactory.class, "beanClassLoader").set(bf, null); Reflections.getField(DefaultListableBeanFactory.class, "alreadyCreated").set(bf, new HashSet<>()); Reflections.getField(DefaultListableBeanFactory.class, "singletonsCurrentlyInCreation").set(bf, new HashSet<>()); Reflections.getField(DefaultListableBeanFactory.class, "inCreationCheckExclusions").set(bf, new HashSet<>()); Reflections.getField(DefaultListableBeanFactory.class, "logger").set(bf, new NoOpLog()); Reflections.getField(DefaultListableBeanFactory.class, "prototypesCurrentlyInCreation").set(bf, new ThreadLocal<>()); @SuppressWarnings("unchecked") Map<String, Object> objs = (Map<String, Object>) Reflections.getFieldValue(bf, "singletonObjects"); objs.put("obj", o); return bf;/*from w w w. ja v a 2s. c om*/ }
From source file:org.exoplatform.container.spring.SpringContainer.java
/** * {@inheritDoc}/* ww w. ja v a2s . c o m*/ */ @Override @SuppressWarnings({ "rawtypes", "unchecked" }) public void start() { ConfigurationManager cm = super.getComponentInstanceOfType(ConfigurationManager.class, false); // We check if the component has been defined in the configuration of the current container // The goal is to enable the SpringContainer only if it is needed Component component = cm.getComponent(ApplicationContextProvider.class); if (component == null) { if (LOG.isDebugEnabled()) { LOG.debug( "No ApplicationContextProvider has been defined, thus the SpringContainer will be disabled." + " To enable the Spring Integration please define an ApplicationContextProvider"); } } else { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); Collection<ComponentAdapter<?>> adapters = delegate.getComponentAdapters(); for (ComponentAdapter<?> adapter : adapters) { Object key = adapter.getComponentKey(); String name = keyToBeanName(key); String factoryName = name + "#factory"; RootBeanDefinition def = new RootBeanDefinition(adapter.getComponentImplementation(), AbstractBeanDefinition.AUTOWIRE_NO, false); def.setScope(BeanDefinition.SCOPE_PROTOTYPE); def.setFactoryBeanName(factoryName); def.setFactoryMethodName("getInstance"); def.setLazyInit(true); def.setTargetType(adapter.getComponentImplementation()); if (key instanceof String) { def.addQualifier(new AutowireCandidateQualifier(Named.class, key)); } else if (key instanceof Class<?> && ((Class<?>) key).isAnnotation()) { def.addQualifier(new AutowireCandidateQualifier((Class<?>) key)); } else { def.setPrimary(true); } bf.registerBeanDefinition(name, def); bf.registerSingleton(factoryName, new ComponentAdapterFactoryBean(adapter)); } GenericApplicationContext parentContext = new GenericApplicationContext(bf); parentContext.refresh(); ApplicationContextProvider provider = super.getComponentInstanceOfType(ApplicationContextProvider.class, false); ctx = provider.getApplicationContext(parentContext); LOG.info("A SpringContainer has been enabled using the ApplicationContextProvider " + provider.getClass()); } super.start(); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testNonStaticFactoryMethodFoundByNonEagerTypeMatching() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class); lbf.registerBeanDefinition("factory", factoryBd); RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class); rbd.setFactoryBeanName("factory"); rbd.setFactoryMethodName("createTestBeanNonStatic"); lbf.registerBeanDefinition("x1", rbd); TestBeanFactory.initialized = false; String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); assertEquals(1, beanNames.length);// ww w. j a va 2s .c o m assertEquals("x1", beanNames[0]); assertFalse(lbf.containsSingleton("x1")); assertTrue(lbf.containsBean("x1")); assertFalse(lbf.containsBean("&x1")); assertTrue(lbf.isSingleton("x1")); assertFalse(lbf.isSingleton("&x1")); assertFalse(lbf.isPrototype("x1")); assertFalse(lbf.isPrototype("&x1")); assertTrue(lbf.isTypeMatch("x1", TestBean.class)); assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); assertEquals(TestBean.class, lbf.getType("x1")); assertEquals(null, lbf.getType("&x1")); assertFalse(TestBeanFactory.initialized); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testNonStaticPrototypeFactoryMethodFoundByNonEagerTypeMatching() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class); lbf.registerBeanDefinition("factory", factoryBd); RootBeanDefinition rbd = new RootBeanDefinition(); rbd.setFactoryBeanName("factory"); rbd.setFactoryMethodName("createTestBeanNonStatic"); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); lbf.registerBeanDefinition("x1", rbd); TestBeanFactory.initialized = false; String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); assertEquals(1, beanNames.length);/*from ww w . j a v a2 s . c o m*/ assertEquals("x1", beanNames[0]); assertFalse(lbf.containsSingleton("x1")); assertTrue(lbf.containsBean("x1")); assertFalse(lbf.containsBean("&x1")); assertTrue(lbf.containsLocalBean("x1")); assertFalse(lbf.containsLocalBean("&x1")); assertFalse(lbf.isSingleton("x1")); assertFalse(lbf.isSingleton("&x1")); assertTrue(lbf.isPrototype("x1")); assertFalse(lbf.isPrototype("&x1")); assertTrue(lbf.isTypeMatch("x1", TestBean.class)); assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); assertTrue(lbf.isTypeMatch("x1", Object.class)); assertFalse(lbf.isTypeMatch("&x1", Object.class)); assertEquals(TestBean.class, lbf.getType("x1")); assertEquals(null, lbf.getType("&x1")); assertFalse(TestBeanFactory.initialized); lbf.registerAlias("x1", "x2"); assertTrue(lbf.containsBean("x2")); assertFalse(lbf.containsBean("&x2")); assertTrue(lbf.containsLocalBean("x2")); assertFalse(lbf.containsLocalBean("&x2")); assertFalse(lbf.isSingleton("x2")); assertFalse(lbf.isSingleton("&x2")); assertTrue(lbf.isPrototype("x2")); assertFalse(lbf.isPrototype("&x2")); assertTrue(lbf.isTypeMatch("x2", TestBean.class)); assertFalse(lbf.isTypeMatch("&x2", TestBean.class)); assertTrue(lbf.isTypeMatch("x2", Object.class)); assertFalse(lbf.isTypeMatch("&x2", Object.class)); assertEquals(TestBean.class, lbf.getType("x2")); assertEquals(null, lbf.getType("&x2")); assertEquals(1, lbf.getAliases("x1").length); assertEquals("x2", lbf.getAliases("x1")[0]); assertEquals(1, lbf.getAliases("&x1").length); assertEquals("&x2", lbf.getAliases("&x1")[0]); assertEquals(1, lbf.getAliases("x2").length); assertEquals("x1", lbf.getAliases("x2")[0]); assertEquals(1, lbf.getAliases("&x2").length); assertEquals("&x1", lbf.getAliases("&x2")[0]); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
/** * @param singleton whether the bean created from the factory method on * the bean instance should be a singleton or prototype. This flag is * used to allow checking of the new ability in 1.2.4 to determine the type * of a prototype created from invoking a factory method on a bean instance * in the factory.// w ww. jav a 2 s. com */ private void findTypeOfPrototypeFactoryMethodOnBeanInstance(boolean singleton) { String expectedNameFromProperties = "tony"; String expectedNameFromArgs = "gordon"; DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition instanceFactoryDefinition = new RootBeanDefinition(BeanWithFactoryMethod.class); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", expectedNameFromProperties); instanceFactoryDefinition.setPropertyValues(pvs); lbf.registerBeanDefinition("factoryBeanInstance", instanceFactoryDefinition); RootBeanDefinition factoryMethodDefinitionWithProperties = new RootBeanDefinition(); factoryMethodDefinitionWithProperties.setFactoryBeanName("factoryBeanInstance"); factoryMethodDefinitionWithProperties.setFactoryMethodName("create"); if (!singleton) { factoryMethodDefinitionWithProperties.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); } lbf.registerBeanDefinition("fmWithProperties", factoryMethodDefinitionWithProperties); RootBeanDefinition factoryMethodDefinitionGeneric = new RootBeanDefinition(); factoryMethodDefinitionGeneric.setFactoryBeanName("factoryBeanInstance"); factoryMethodDefinitionGeneric.setFactoryMethodName("createGeneric"); if (!singleton) { factoryMethodDefinitionGeneric.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); } lbf.registerBeanDefinition("fmGeneric", factoryMethodDefinitionGeneric); RootBeanDefinition factoryMethodDefinitionWithArgs = new RootBeanDefinition(); factoryMethodDefinitionWithArgs.setFactoryBeanName("factoryBeanInstance"); factoryMethodDefinitionWithArgs.setFactoryMethodName("createWithArgs"); ConstructorArgumentValues cvals = new ConstructorArgumentValues(); cvals.addGenericArgumentValue(expectedNameFromArgs); factoryMethodDefinitionWithArgs.setConstructorArgumentValues(cvals); if (!singleton) { factoryMethodDefinitionWithArgs.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); } lbf.registerBeanDefinition("fmWithArgs", factoryMethodDefinitionWithArgs); assertEquals(4, lbf.getBeanDefinitionCount()); List<String> tbNames = Arrays.asList(lbf.getBeanNamesForType(TestBean.class)); assertTrue(tbNames.contains("fmWithProperties")); assertTrue(tbNames.contains("fmWithArgs")); assertEquals(2, tbNames.size()); TestBean tb = (TestBean) lbf.getBean("fmWithProperties"); TestBean second = (TestBean) lbf.getBean("fmWithProperties"); if (singleton) { assertSame(tb, second); } else { assertNotSame(tb, second); } assertEquals(expectedNameFromProperties, tb.getName()); tb = (TestBean) lbf.getBean("fmGeneric"); second = (TestBean) lbf.getBean("fmGeneric"); if (singleton) { assertSame(tb, second); } else { assertNotSame(tb, second); } assertEquals(expectedNameFromProperties, tb.getName()); TestBean tb2 = (TestBean) lbf.getBean("fmWithArgs"); second = (TestBean) lbf.getBean("fmWithArgs"); if (singleton) { assertSame(tb2, second); } else { assertNotSame(tb2, second); } assertEquals(expectedNameFromArgs, tb2.getName()); }
From source file:org.springframework.security.config.http.AuthenticationConfigBuilder.java
private void createOpenIDProvider() { Element openIDLoginElt = DomUtils.getChildElementByTagName(httpElt, Elements.OPENID_LOGIN); BeanDefinitionBuilder openIDProviderBuilder = BeanDefinitionBuilder .rootBeanDefinition(OPEN_ID_AUTHENTICATION_PROVIDER_CLASS); RootBeanDefinition uds = new RootBeanDefinition(); uds.setFactoryBeanName(BeanIds.USER_DETAILS_SERVICE_FACTORY); uds.setFactoryMethodName("authenticationUserDetailsService"); uds.getConstructorArgumentValues()/*from w w w . ja v a2 s . c o m*/ .addGenericArgumentValue(openIDLoginElt.getAttribute(ATT_USER_SERVICE_REF)); openIDProviderBuilder.addPropertyValue("authenticationUserDetailsService", uds); BeanDefinition openIDProvider = openIDProviderBuilder.getBeanDefinition(); openIDProviderRef = new RuntimeBeanReference( pc.getReaderContext().registerWithGeneratedName(openIDProvider)); }
From source file:org.springframework.security.config.http.AuthenticationConfigBuilder.java
private void createX509Provider() { Element x509Elt = DomUtils.getChildElementByTagName(httpElt, Elements.X509); BeanDefinition provider = new RootBeanDefinition(PreAuthenticatedAuthenticationProvider.class); RootBeanDefinition uds = new RootBeanDefinition(); uds.setFactoryBeanName(BeanIds.USER_DETAILS_SERVICE_FACTORY); uds.setFactoryMethodName("authenticationUserDetailsService"); uds.getConstructorArgumentValues().addGenericArgumentValue(x509Elt.getAttribute(ATT_USER_SERVICE_REF)); provider.getPropertyValues().addPropertyValue("preAuthenticatedUserDetailsService", uds); x509ProviderRef = new RuntimeBeanReference(pc.getReaderContext().registerWithGeneratedName(provider)); }
From source file:org.springframework.security.config.http.AuthenticationConfigBuilder.java
private void createJeeProvider() { Element jeeElt = DomUtils.getChildElementByTagName(httpElt, Elements.JEE); BeanDefinition provider = new RootBeanDefinition(PreAuthenticatedAuthenticationProvider.class); RootBeanDefinition uds; if (StringUtils.hasText(jeeElt.getAttribute(ATT_USER_SERVICE_REF))) { uds = new RootBeanDefinition(); uds.setFactoryBeanName(BeanIds.USER_DETAILS_SERVICE_FACTORY); uds.setFactoryMethodName("authenticationUserDetailsService"); uds.getConstructorArgumentValues().addGenericArgumentValue(jeeElt.getAttribute(ATT_USER_SERVICE_REF)); } else {//from w w w . j a v a 2 s . c o m uds = new RootBeanDefinition(PreAuthenticatedGrantedAuthoritiesUserDetailsService.class); } provider.getPropertyValues().addPropertyValue("preAuthenticatedUserDetailsService", uds); jeeProviderRef = new RuntimeBeanReference(pc.getReaderContext().registerWithGeneratedName(provider)); }
From source file:org.springframework.security.config.http.RememberMeBeanDefinitionParser.java
public BeanDefinition parse(Element element, ParserContext pc) { CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), pc.extractSource(element));//from ww w .j ava 2s . c o m pc.pushContainingComponent(compositeDef); String tokenRepository = element.getAttribute(ATT_TOKEN_REPOSITORY); String dataSource = element.getAttribute(ATT_DATA_SOURCE); String userServiceRef = element.getAttribute(ATT_USER_SERVICE_REF); String successHandlerRef = element.getAttribute(ATT_SUCCESS_HANDLER_REF); String rememberMeServicesRef = element.getAttribute(ATT_SERVICES_REF); String tokenValiditySeconds = element.getAttribute(ATT_TOKEN_VALIDITY); String useSecureCookie = element.getAttribute(ATT_SECURE_COOKIE); String remembermeParameter = element.getAttribute(ATT_FORM_REMEMBERME_PARAMETER); String remembermeCookie = element.getAttribute(ATT_REMEMBERME_COOKIE); Object source = pc.extractSource(element); RootBeanDefinition services = null; boolean dataSourceSet = StringUtils.hasText(dataSource); boolean tokenRepoSet = StringUtils.hasText(tokenRepository); boolean servicesRefSet = StringUtils.hasText(rememberMeServicesRef); boolean userServiceSet = StringUtils.hasText(userServiceRef); boolean useSecureCookieSet = StringUtils.hasText(useSecureCookie); boolean tokenValiditySet = StringUtils.hasText(tokenValiditySeconds); boolean remembermeParameterSet = StringUtils.hasText(remembermeParameter); boolean remembermeCookieSet = StringUtils.hasText(remembermeCookie); if (servicesRefSet && (dataSourceSet || tokenRepoSet || userServiceSet || tokenValiditySet || useSecureCookieSet || remembermeParameterSet || remembermeCookieSet)) { pc.getReaderContext() .error(ATT_SERVICES_REF + " can't be used in combination with attributes " + ATT_TOKEN_REPOSITORY + "," + ATT_DATA_SOURCE + ", " + ATT_USER_SERVICE_REF + ", " + ATT_TOKEN_VALIDITY + ", " + ATT_SECURE_COOKIE + ", " + ATT_FORM_REMEMBERME_PARAMETER + " or " + ATT_REMEMBERME_COOKIE, source); } if (dataSourceSet && tokenRepoSet) { pc.getReaderContext() .error("Specify " + ATT_TOKEN_REPOSITORY + " or " + ATT_DATA_SOURCE + " but not both", source); } boolean isPersistent = dataSourceSet | tokenRepoSet; if (isPersistent) { Object tokenRepo; services = new RootBeanDefinition(PersistentTokenBasedRememberMeServices.class); if (tokenRepoSet) { tokenRepo = new RuntimeBeanReference(tokenRepository); } else { tokenRepo = new RootBeanDefinition(JdbcTokenRepositoryImpl.class); ((BeanDefinition) tokenRepo).getPropertyValues().addPropertyValue("dataSource", new RuntimeBeanReference(dataSource)); } services.getConstructorArgumentValues().addIndexedArgumentValue(2, tokenRepo); } else if (!servicesRefSet) { services = new RootBeanDefinition(TokenBasedRememberMeServices.class); } String servicesName; if (services != null) { RootBeanDefinition uds = new RootBeanDefinition(); uds.setFactoryBeanName(BeanIds.USER_DETAILS_SERVICE_FACTORY); uds.setFactoryMethodName("cachingUserDetailsService"); uds.getConstructorArgumentValues().addGenericArgumentValue(userServiceRef); services.getConstructorArgumentValues().addGenericArgumentValue(key); services.getConstructorArgumentValues().addGenericArgumentValue(uds); // tokenRepo is already added if it is a // PersistentTokenBasedRememberMeServices if (useSecureCookieSet) { services.getPropertyValues().addPropertyValue("useSecureCookie", Boolean.valueOf(useSecureCookie)); } if (tokenValiditySet) { boolean isTokenValidityNegative = tokenValiditySeconds.startsWith("-"); if (isTokenValidityNegative && isPersistent) { pc.getReaderContext().error(ATT_TOKEN_VALIDITY + " cannot be negative if using" + " a persistent remember-me token repository", source); } services.getPropertyValues().addPropertyValue("tokenValiditySeconds", tokenValiditySeconds); } if (remembermeParameterSet) { services.getPropertyValues().addPropertyValue("parameter", remembermeParameter); } if (remembermeCookieSet) { services.getPropertyValues().addPropertyValue("cookieName", remembermeCookie); } services.setSource(source); servicesName = pc.getReaderContext().generateBeanName(services); pc.registerBeanComponent(new BeanComponentDefinition(services, servicesName)); } else { servicesName = rememberMeServicesRef; } if (StringUtils.hasText(element.getAttribute(ATT_SERVICES_ALIAS))) { pc.getRegistry().registerAlias(servicesName, element.getAttribute(ATT_SERVICES_ALIAS)); } this.rememberMeServicesId = servicesName; BeanDefinitionBuilder filter = BeanDefinitionBuilder .rootBeanDefinition(RememberMeAuthenticationFilter.class); filter.getRawBeanDefinition().setSource(source); if (StringUtils.hasText(successHandlerRef)) { filter.addPropertyReference("authenticationSuccessHandler", successHandlerRef); } filter.addConstructorArgValue(authenticationManager); filter.addConstructorArgReference(servicesName); pc.popAndRegisterContainingComponent(); return filter.getBeanDefinition(); }
From source file:org.springframework.security.config.method.GlobalMethodSecurityBeanDefinitionParser.java
private RootBeanDefinition registerWithDefaultRolePrefix(ParserContext pc, Class<? extends AbstractGrantedAuthorityDefaultsBeanFactory> beanFactoryClass) { RootBeanDefinition beanFactoryDefinition = new RootBeanDefinition(beanFactoryClass); String beanFactoryRef = pc.getReaderContext().generateBeanName(beanFactoryDefinition); pc.getRegistry().registerBeanDefinition(beanFactoryRef, beanFactoryDefinition); RootBeanDefinition bean = new RootBeanDefinition(); bean.setFactoryBeanName(beanFactoryRef); bean.setFactoryMethodName("getBean"); return bean;/*from w w w . ja v a 2s . c o m*/ }