spring-bean

Static variable vs singleton

爷,独闯天下 提交于 2021-02-05 09:31:28
问题 I'm making Java REST application. I wonder how should I implement my services - should I use static service variables for whole application or make services as singletons like it was made in Spring MVC. Is there any difference between singleton object and initializing object only once during application? 回答1: should I use static service variables for whole application or make services as singletons It depends. You have to ask yourself two questions to come up with an answer: Where is that

SpringBoot retrieve existing prototype beans

不羁的心 提交于 2021-01-29 10:56:11
问题 As my title, is there a way to retrieve those existing prototype bean? I have a prototype bean called "A", and called applicationContext.getBean() method 10 times to create 10 instances. There is no variable referring to those instances. Ways I have tried but doesn't work: 1.autowiring a list of A as below: @autowired List<A> as; this can only get the last instance I created. if I using beanFactory to get bean, it will just create a new instance A. 回答1: these beans will not be managed by

Kotlin inner class as Spring Bean

被刻印的时光 ゝ 提交于 2021-01-29 07:41:06
问题 I used Kotlin with Spring boot and i met some errors about bean creation. error message was Index 0 out of bounds for length 0 in spring framework class i dont understand why caused exception about this java code(spring) for (int paramIndex = 0; paramIndex < paramTypes.length; paramIndex++) { Class<?> paramType = paramTypes[paramIndex]; String paramName = (paramNames != null ? paramNames[paramIndex] : ""); // Here! ... } is that possible? anyway, i want to know how create spring bean as

Issue with Spring AOP and Final class throwing “Could not generate CGLIB subclass”

孤街浪徒 提交于 2021-01-28 21:04:00
问题 1) Below is the small project I have where I do want logging with SpringAOP. Using spring-aop-4.1.6.RELEASE.jar and below is LoggingAspect.java with few point cuts. package com.myprj.aop.aspect; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import

Spring bean definition - get bean class

风格不统一 提交于 2021-01-27 22:11:46
问题 I'm trying the get Bean class name without initialize the bean. I need to know the class , I could get the bean from applicationContext and to check the class name from the bean instance, But I want to know the class with out actually creating/init the bean.. Is it possible ? Object bean = applicationContext.getBean("beanName"); bean.getClass(); 回答1: You can't do this after creating the ApplicationContext . Most ApplicationContext implementations will refresh() themselves and force

Java 11 - Replace Spring @PostConstruct with afterPropertiesSet or using initMethod

喜欢而已 提交于 2021-01-04 05:40:49
问题 I'm using spring applications which sometimes uses @PostConstruct for setup in code and tests It seems that annotation will be excluded from Java 11: Note that both @PostConstruct and @PreDestroy annotations are part of Java EE. And since Java EE has been deprecated in Java 9 and removed in Java 11 we have to add an additional dependency to use these annotations Article suggest replacing all @PostConstruct with afterPropertiesSet method I recommend you to change implementation from

Aliasing a bean outside the bean definition using Java config in Spring Boot

萝らか妹 提交于 2020-05-29 08:17:16
问题 How to alias a bean outside the bean definition using Java config in Spring Boot? 回答1: I have this as well, and solved it like this: @Component public class AliasConfiguration implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { beanFactory.registerAlias("originalBeanName", "newAlias"); beanFactory.registerAlias("originalBeanName", "newAlias2"); beanFactory.registerAlias("otherOriginalBeanName",

Spring dynamic bean creation depending on other bean

安稳与你 提交于 2020-02-02 11:13:52
问题 Is there a way to dynamically create a bean which is dependent on another one? I have a Spring Boot app which loads a configuration.xml file into a configuration bean. I want to have the configuration loaded once I try to create new dynamic beans dependent on that configuration bean. What I have tried so far is to implement BeanDefinitionRegistryPostProcessor , but in the moment when I try to create a new GenericBeanDefinition , the configuration is not loaded yet. Here is my code: @Component

Spring dynamic bean creation depending on other bean

拜拜、爱过 提交于 2020-02-02 11:13:08
问题 Is there a way to dynamically create a bean which is dependent on another one? I have a Spring Boot app which loads a configuration.xml file into a configuration bean. I want to have the configuration loaded once I try to create new dynamic beans dependent on that configuration bean. What I have tried so far is to implement BeanDefinitionRegistryPostProcessor , but in the moment when I try to create a new GenericBeanDefinition , the configuration is not loaded yet. Here is my code: @Component