org.springframework.beans.factory.annotation.Autowire Java Examples
The following examples show how to use
org.springframework.beans.factory.annotation.Autowire.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractPropertyAccessorTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void setEnumProperty() { EnumTester target = new EnumTester(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("autowire", "BY_NAME"); assertEquals(Autowire.BY_NAME, target.getAutowire()); accessor.setPropertyValue("autowire", " BY_TYPE "); assertEquals(Autowire.BY_TYPE, target.getAutowire()); try { accessor.setPropertyValue("autowire", "NHERITED"); fail("Should have thrown TypeMismatchException"); } catch (TypeMismatchException ex) { // expected } }
Example #2
Source File: AppConfiguration.java From searchanalytics-bigdata with MIT License | 6 votes |
@Bean(autowire = Autowire.BY_NAME, name = "setupIndexMasterActor") // @Scope("prototype") @DependsOn(value = { "actorSystem" }) public ActorRef setupIndexMasterActor() { final ActorSystem system = applicationContext .getBean(ActorSystem.class); final SetupIndexService setupIndexService = applicationContext .getBean(SetupIndexService.class); final SampleDataGeneratorService sampleDataGeneratorService = applicationContext .getBean(SampleDataGeneratorService.class); final IndexProductDataService indexProductData = applicationContext .getBean(IndexProductDataService.class); return system.actorOf( Props.create(SetupIndexMasterActor.class, setupIndexService, sampleDataGeneratorService, indexProductData) .withDispatcher("setupIndexMasterActorDispatch"), "setupIndexMasterActor"); }
Example #3
Source File: AbstractPropertyAccessorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void setEnumProperty() { EnumTester target = new EnumTester(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("autowire", "BY_NAME"); assertEquals(Autowire.BY_NAME, target.getAutowire()); accessor.setPropertyValue("autowire", " BY_TYPE "); assertEquals(Autowire.BY_TYPE, target.getAutowire()); try { accessor.setPropertyValue("autowire", "NHERITED"); fail("Should have thrown TypeMismatchException"); } catch (TypeMismatchException ex) { // expected } }
Example #4
Source File: AbstractPropertyAccessorTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void setEnumProperty() { EnumTester target = new EnumTester(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValue("autowire", "BY_NAME"); assertEquals(Autowire.BY_NAME, target.getAutowire()); accessor.setPropertyValue("autowire", " BY_TYPE "); assertEquals(Autowire.BY_TYPE, target.getAutowire()); try { accessor.setPropertyValue("autowire", "NHERITED"); fail("Should have thrown TypeMismatchException"); } catch (TypeMismatchException ex) { // expected } }
Example #5
Source File: MailConfig.java From phoenix.webui.framework with Apache License 2.0 | 5 votes |
@Bean(autowire = Autowire.BY_TYPE) public MailSender mailBean() { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost(mailHost); mailSender.setUsername(mailUserName); mailSender.setPassword(mailPassword); setUpMailPro(mailSender); return mailSender; }
Example #6
Source File: GspAutoConfiguration.java From grails-boot with Apache License 2.0 | 5 votes |
@Bean(autowire=Autowire.BY_NAME) @ConditionalOnMissingBean(name="groovyPagesTemplateEngine") GroovyPagesTemplateEngine groovyPagesTemplateEngine() { GroovyPagesTemplateEngine templateEngine = new GroovyPagesTemplateEngine(); templateEngine.setReloadEnabled(gspReloadingEnabled); return templateEngine; }
Example #7
Source File: BeanAnnotationAttributePropagationTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void autowireMetadataIsPropagated() { @Configuration class Config { @Bean(autowire=Autowire.BY_TYPE) Object foo() { return null; } } assertEquals("autowire mode was not propagated", AbstractBeanDefinition.AUTOWIRE_BY_TYPE, beanDef(Config.class).getAutowireMode()); }
Example #8
Source File: AnnotationUtilsTest.java From spring-context-support with Apache License 2.0 | 5 votes |
@Test public void testGetAttributes() { Bean annotation = getAnnotation("dummyBean", Bean.class); Map<String, Object> attributes = getAttributes(annotation, true); Assert.assertTrue(Arrays.equals(new String[]{"dummy-bean"}, (String[]) attributes.get("name"))); attributes = getAttributes(annotation, true); Assert.assertTrue(Arrays.equals(new String[]{"dummy-bean"}, (String[]) attributes.get("name"))); attributes = getAttributes(annotation, false); assertEquals(Autowire.NO, attributes.get("autowire")); assertEquals("", attributes.get("initMethod")); assertEquals(AbstractBeanDefinition.INFER_METHOD, attributes.get("destroyMethod")); MockEnvironment environment = new MockEnvironment(); attributes = getAttributes(annotation, environment, false); assertEquals(Autowire.NO, attributes.get("autowire")); assertEquals("", attributes.get("initMethod")); assertEquals(AbstractBeanDefinition.INFER_METHOD, attributes.get("destroyMethod")); annotation = getAnnotation("dummyBean2", Bean.class); attributes = getAttributes(annotation, true); Assert.assertTrue(attributes.isEmpty()); attributes = getAttributes(annotation, environment, true); Assert.assertTrue(attributes.isEmpty()); environment.setProperty("beanName", "Your Bean Name"); annotation = getAnnotation("dummyBean3", Bean.class); attributes = getAttributes(annotation, environment, true); Assert.assertTrue(Arrays.deepEquals(of(environment.getProperty("beanName")), (String[]) attributes.get("name"))); }
Example #9
Source File: BeanAnnotationAttributePropagationTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void autowireMetadataIsPropagated() { @Configuration class Config { @Bean(autowire=Autowire.BY_TYPE) Object foo() { return null; } } assertEquals("autowire mode was not propagated", AbstractBeanDefinition.AUTOWIRE_BY_TYPE, beanDef(Config.class).getAutowireMode()); }
Example #10
Source File: ConfigurationClassBeanDefinitionReader.java From spring4-understanding with Apache License 2.0 | 4 votes |
/** * Read the given {@link BeanMethod}, registering bean definitions * with the BeanDefinitionRegistry based on its contents. */ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) { ConfigurationClass configClass = beanMethod.getConfigurationClass(); MethodMetadata metadata = beanMethod.getMetadata(); String methodName = metadata.getMethodName(); // Do we need to mark the bean as skipped by its condition? if (this.conditionEvaluator.shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN)) { configClass.skippedBeanMethods.add(methodName); return; } if (configClass.skippedBeanMethods.contains(methodName)) { return; } // Consider name and any aliases AnnotationAttributes bean = AnnotationConfigUtils.attributesFor(metadata, Bean.class); List<String> names = new ArrayList<String>(Arrays.asList(bean.getStringArray("name"))); String beanName = (names.size() > 0 ? names.remove(0) : methodName); // Register aliases even when overridden for (String alias : names) { this.registry.registerAlias(beanName, alias); } // Has this effectively been overridden before (e.g. via XML)? if (isOverriddenByExistingDefinition(beanMethod, beanName)) { return; } ConfigurationClassBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass, metadata); beanDef.setResource(configClass.getResource()); beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource())); if (metadata.isStatic()) { // static @Bean method beanDef.setBeanClassName(configClass.getMetadata().getClassName()); beanDef.setFactoryMethodName(methodName); } else { // instance @Bean method beanDef.setFactoryBeanName(configClass.getBeanName()); beanDef.setUniqueFactoryMethodName(methodName); } beanDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); beanDef.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE); AnnotationConfigUtils.processCommonDefinitionAnnotations(beanDef, metadata); Autowire autowire = bean.getEnum("autowire"); if (autowire.isAutowire()) { beanDef.setAutowireMode(autowire.value()); } String initMethodName = bean.getString("initMethod"); if (StringUtils.hasText(initMethodName)) { beanDef.setInitMethodName(initMethodName); } String destroyMethodName = bean.getString("destroyMethod"); if (destroyMethodName != null) { beanDef.setDestroyMethodName(destroyMethodName); } // Consider scoping ScopedProxyMode proxyMode = ScopedProxyMode.NO; AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(metadata, Scope.class); if (attributes != null) { beanDef.setScope(attributes.getAliasedString("value", Scope.class, configClass.getResource())); proxyMode = attributes.getEnum("proxyMode"); if (proxyMode == ScopedProxyMode.DEFAULT) { proxyMode = ScopedProxyMode.NO; } } // Replace the original bean definition with the target one, if necessary BeanDefinition beanDefToRegister = beanDef; if (proxyMode != ScopedProxyMode.NO) { BeanDefinitionHolder proxyDef = ScopedProxyCreator.createScopedProxy( new BeanDefinitionHolder(beanDef, beanName), this.registry, proxyMode == ScopedProxyMode.TARGET_CLASS); beanDefToRegister = new ConfigurationClassBeanDefinition( (RootBeanDefinition) proxyDef.getBeanDefinition(), configClass, metadata); } if (logger.isDebugEnabled()) { logger.debug(String.format("Registering bean definition for @Bean method %s.%s()", configClass.getMetadata().getClassName(), beanName)); } this.registry.registerBeanDefinition(beanName, beanDefToRegister); }
Example #11
Source File: Application.java From blog-examples with Apache License 2.0 | 4 votes |
@Bean(autowire = Autowire.BY_TYPE) public JpaRepositoryFactoryBean<UserRepository, User, Long> jpaRepositoryFactoryBean() { JpaRepositoryFactoryBean factory = new JooqRepositoryBean<UserRepository, User, Long>(); factory.setRepositoryInterface(UserRepository.class); return factory; }
Example #12
Source File: GspAutoConfiguration.java From grails-boot with Apache License 2.0 | 4 votes |
@Bean(autowire = Autowire.BY_NAME) public TagLibraryResolverImpl jspTagLibraryResolver() { return new TagLibraryResolverImpl(); }
Example #13
Source File: AbstractPropertyAccessorTests.java From spring-analysis-note with MIT License | 4 votes |
public void setAutowire(Autowire autowire) { this.autowire = autowire; }
Example #14
Source File: AbstractPropertyAccessorTests.java From spring-analysis-note with MIT License | 4 votes |
public Autowire getAutowire() { return autowire; }
Example #15
Source File: AppConfiguration.java From searchanalytics-bigdata with MIT License | 4 votes |
/** * Actor system singleton for this application. */ @Bean(autowire = Autowire.BY_NAME, name = "actorSystem") public ActorSystem actorSystem() { return ActorSystem.create("SearchIndexingSystem", ConfigFactory.load("application-akka-es.conf") .getConfig("SearchIndexingSystem")); }
Example #16
Source File: FhirServerConfig.java From cqf-ruler with Apache License 2.0 | 4 votes |
/** * This interceptor adds some pretty syntax highlighting in responses when a browser is detected */ @Bean(autowire = Autowire.BY_TYPE) public ResponseHighlighterInterceptor responseHighlighterInterceptor() { return new ResponseHighlighterInterceptor(); }
Example #17
Source File: AbstractPropertyAccessorTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
public Autowire getAutowire() { return autowire; }
Example #18
Source File: AbstractPropertyAccessorTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
public void setAutowire(Autowire autowire) { this.autowire = autowire; }
Example #19
Source File: RedisConfiguration.java From heimdall with Apache License 2.0 | 4 votes |
/** * Configures and returns a {@link RedissonClient}. * * @return {@link RedissonClient} */ @Bean(autowire = Autowire.BY_NAME) public RedissonClient redissonClientRateLimitInterceptor() { return createConnection(ConstantsCache.RATE_LIMIT_DATABASE); }
Example #20
Source File: Config.java From careconnect-reference-implementation with Apache License 2.0 | 4 votes |
@Bean(autowire = Autowire.BY_TYPE) public DatabaseBackedPagingProvider databaseBackedPagingProvider() { DatabaseBackedPagingProvider retVal = new DatabaseBackedPagingProvider(); return retVal; }
Example #21
Source File: ConfigurationClassBeanDefinitionReader.java From lams with GNU General Public License v2.0 | 4 votes |
/** * Read the given {@link BeanMethod}, registering bean definitions * with the BeanDefinitionRegistry based on its contents. */ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) { ConfigurationClass configClass = beanMethod.getConfigurationClass(); MethodMetadata metadata = beanMethod.getMetadata(); String methodName = metadata.getMethodName(); // Do we need to mark the bean as skipped by its condition? if (this.conditionEvaluator.shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN)) { configClass.skippedBeanMethods.add(methodName); return; } if (configClass.skippedBeanMethods.contains(methodName)) { return; } // Consider name and any aliases AnnotationAttributes bean = AnnotationConfigUtils.attributesFor(metadata, Bean.class); List<String> names = new ArrayList<String>(Arrays.asList(bean.getStringArray("name"))); String beanName = (!names.isEmpty() ? names.remove(0) : methodName); // Register aliases even when overridden for (String alias : names) { this.registry.registerAlias(beanName, alias); } // Has this effectively been overridden before (e.g. via XML)? if (isOverriddenByExistingDefinition(beanMethod, beanName)) { if (beanName.equals(beanMethod.getConfigurationClass().getBeanName())) { throw new BeanDefinitionStoreException(beanMethod.getConfigurationClass().getResource().getDescription(), beanName, "Bean name derived from @Bean method '" + beanMethod.getMetadata().getMethodName() + "' clashes with bean name for containing configuration class; please make those names unique!"); } return; } ConfigurationClassBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass, metadata); beanDef.setResource(configClass.getResource()); beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource())); if (metadata.isStatic()) { // static @Bean method beanDef.setBeanClassName(configClass.getMetadata().getClassName()); beanDef.setFactoryMethodName(methodName); } else { // instance @Bean method beanDef.setFactoryBeanName(configClass.getBeanName()); beanDef.setUniqueFactoryMethodName(methodName); } beanDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); beanDef.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE); AnnotationConfigUtils.processCommonDefinitionAnnotations(beanDef, metadata); Autowire autowire = bean.getEnum("autowire"); if (autowire.isAutowire()) { beanDef.setAutowireMode(autowire.value()); } String initMethodName = bean.getString("initMethod"); if (StringUtils.hasText(initMethodName)) { beanDef.setInitMethodName(initMethodName); } String destroyMethodName = bean.getString("destroyMethod"); if (destroyMethodName != null) { beanDef.setDestroyMethodName(destroyMethodName); } // Consider scoping ScopedProxyMode proxyMode = ScopedProxyMode.NO; AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(metadata, Scope.class); if (attributes != null) { beanDef.setScope(attributes.getString("value")); proxyMode = attributes.getEnum("proxyMode"); if (proxyMode == ScopedProxyMode.DEFAULT) { proxyMode = ScopedProxyMode.NO; } } // Replace the original bean definition with the target one, if necessary BeanDefinition beanDefToRegister = beanDef; if (proxyMode != ScopedProxyMode.NO) { BeanDefinitionHolder proxyDef = ScopedProxyCreator.createScopedProxy( new BeanDefinitionHolder(beanDef, beanName), this.registry, proxyMode == ScopedProxyMode.TARGET_CLASS); beanDefToRegister = new ConfigurationClassBeanDefinition( (RootBeanDefinition) proxyDef.getBeanDefinition(), configClass, metadata); } if (logger.isDebugEnabled()) { logger.debug(String.format("Registering bean definition for @Bean method %s.%s()", configClass.getMetadata().getClassName(), beanName)); } this.registry.registerBeanDefinition(beanName, beanDefToRegister); }
Example #22
Source File: RestCatalogApplication.java From sos with Apache License 2.0 | 4 votes |
@Bean(autowire = Autowire.BY_TYPE) ControllerLinkBuilderFactory foo() { return new ControllerLinkBuilderFactory(); }
Example #23
Source File: AbstractPropertyAccessorTests.java From java-technology-stack with MIT License | 4 votes |
public void setAutowire(Autowire autowire) { this.autowire = autowire; }
Example #24
Source File: AnnotationUtilsTest.java From spring-context-support with Apache License 2.0 | 4 votes |
@Test public void testGetAnnotationAttributes() { MockEnvironment environment = new MockEnvironment(); Bean annotation = getAnnotation("dummyBean", Bean.class); // case 1 : PropertyResolver(null) , ignoreDefaultValue(true) , ignoreAttributeName(empty) AnnotationAttributes annotationAttributes = getAnnotationAttributes(annotation, true); assertArrayEquals(of("dummy-bean"), annotationAttributes.getStringArray("name")); // case 2 : PropertyResolver , ignoreDefaultValue(true) , ignoreAttributeName(empty) annotationAttributes = getAnnotationAttributes(annotation, environment, true); assertArrayEquals(of("dummy-bean"), annotationAttributes.getStringArray("name")); // case 3 : PropertyResolver , ignoreDefaultValue(true) , ignoreAttributeName(name) annotationAttributes = getAnnotationAttributes(annotation, environment, true, "name"); // case 4 : PropertyResolver(null) , ignoreDefaultValue(false) , ignoreAttributeName(empty) annotationAttributes = getAnnotationAttributes(annotation, false); assertArrayEquals(of("dummy-bean"), annotationAttributes.getStringArray("name")); assertEquals(Autowire.NO, annotationAttributes.get("autowire")); assertEquals("", annotationAttributes.getString("initMethod")); assertEquals(AbstractBeanDefinition.INFER_METHOD, annotationAttributes.getString("destroyMethod")); // case 5 : PropertyResolver , ignoreDefaultValue(false) , ignoreAttributeName(empty) annotationAttributes = getAnnotationAttributes(annotation, environment, false); assertArrayEquals(of("dummy-bean"), annotationAttributes.getStringArray("name")); assertEquals(Autowire.NO, annotationAttributes.get("autowire")); assertEquals("", annotationAttributes.getString("initMethod")); assertEquals(AbstractBeanDefinition.INFER_METHOD, annotationAttributes.getString("destroyMethod")); // case 6 : PropertyResolver , ignoreDefaultValue(false) , ignoreAttributeName(name,autowire,initMethod) annotationAttributes = getAnnotationAttributes(annotation, environment, false, "name", "autowire", "initMethod"); assertEquals(AbstractBeanDefinition.INFER_METHOD, annotationAttributes.getString("destroyMethod")); // getAnnotationAttributes(AnnotatedElement, java.lang.Class, PropertyResolver, boolean, String...) annotationAttributes = getAnnotationAttributes(getMethod("dummyBean"), Bean.class, environment, true); assertArrayEquals(of("dummy-bean"), annotationAttributes.getStringArray("name")); annotationAttributes = getAnnotationAttributes(getMethod("dummyBean"), Configuration.class, environment, true); assertNull(annotationAttributes); // getAnnotationAttributes(AnnotatedElement, java.lang.Class, PropertyResolver, boolean, boolean, String...) annotationAttributes = getAnnotationAttributes(getMethod("dummyBean"), Bean.class, environment, true, true); assertArrayEquals(of("dummy-bean"), annotationAttributes.getStringArray("name")); annotationAttributes = getAnnotationAttributes(getMethod("dummyBean"), Bean.class, environment, true, false); assertArrayEquals(of("dummy-bean"), annotationAttributes.getStringArray("name")); annotationAttributes = getAnnotationAttributes(getMethod("dummyBean"), Configuration.class, environment, true, true); assertNull(annotationAttributes); }
Example #25
Source File: AbstractPropertyAccessorTests.java From java-technology-stack with MIT License | 4 votes |
public Autowire getAutowire() { return autowire; }
Example #26
Source File: RedisConfiguration.java From heimdall with Apache License 2.0 | 4 votes |
@Bean(autowire = Autowire.BY_NAME) public RedissonClient redissonClientCacheInterceptor() { return createConnection(ConstantsCache.CACHE_INTERCEPTOR_DATABASE); }
Example #27
Source File: RedisConfiguration.java From heimdall with Apache License 2.0 | 4 votes |
/** * Returns a configured {@link RedissonClient}. * * @return {@link RedissonClient} */ @Bean(autowire = Autowire.BY_NAME) public RedissonClient redissonClientRateLimitInterceptor() { return createConnection(ConstantsCache.RATE_LIMIT_DATABASE); }
Example #28
Source File: RedisConfiguration.java From heimdall with Apache License 2.0 | 4 votes |
@Bean(autowire = Autowire.BY_NAME) public RedissonClient redissonClientCacheInterceptor() { return createConnection(ConstantsCache.CACHE_INTERCEPTOR_DATABASE); }
Example #29
Source File: SpringControllerFactory.java From pippo with Apache License 2.0 | 2 votes |
/** * Created a bean definition for a class. * Optionally configure all bean properties, like scope, prototype/singleton, etc using: * <p/> * <pre> * BeanDefinition definition = super.createBeanDefinition(beanClass); * definition.setScope(BeanDefinition.SCOPE_SINGLETON); * return definition; * </pre> * * @param controllerClass * @return */ protected BeanDefinition createBeanDefinition(Class<? extends Controller> controllerClass) { // optionally configure all bean properties, like scope, prototype/singleton, etc // return new RootBeanDefinition(beanClass); return new RootBeanDefinition(controllerClass, Autowire.BY_TYPE.value(), true); }