spring-bean

Spring dynamic bean creation depending on other bean

筅森魡賤 提交于 2020-02-02 11:12:27
问题 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 annotation conditionalOnBean not working

拜拜、爱过 提交于 2020-01-23 05:37:09
问题 I defined a class with annotation Configuration @Configuration @AutoConfigureAfter(EndpointAutoConfiguration.class) public class EndpointConfiguration { @Resource private MetricsEndpoint metricsEndpoint; @Bean public MetricsFormatEndpoint metricsFormatEndpoint() { return new MetricsFormatEndpoint(metricsEndpoint); } } the MetricsFormatEndpoint works well. but I use the annotation conditionalOnBean, it doesn't work at all. @Bean @ConditionalOnBean(MetricsEndpoint.class) public

How to create Spring Beans in a dynamical way. Using Quartz SchedulerFactoryBean

风流意气都作罢 提交于 2020-01-13 22:40:35
问题 I have a QuartzJobConfig class where I register my Spring-Quartz-Beans . I followed the instruction of the SchedulerFactoryBean , JobDetailFactoryBean and CronTriggerFactoryBean . My Jobs are configured in a yaml file outside the application. Means I have to create the Beans dynamically when the application starts. My Config: channelPartnerConfiguration: channelPartners: - code: Job1 jobConfigs: - schedule: 0 * * ? * MON-FRI name: Job1 daily hotel: false allotment: true enabled: true -

Spring: how to initialize related lazy beans after main bean creation

早过忘川 提交于 2020-01-03 13:57:26
问题 I have code with lazy initialized beans: @Component @Lazy class Resource {...} @Component @Lazy @CustomProcessor class ResourceProcessorFoo{ @Autowired public ResourceProcessor(Resource resource) {...} } @Component @Lazy @CustomProcessor class ResourceProcessorBar{ @Autowired public ResourceProcessor(Resource resource) {...} } After initialize application context, there's no instances of this beans. When bean Resource is created by application context (as example, applicationContext.getBean

Spring Boot create a list of managed prototype beans from database configuration

大憨熊 提交于 2020-01-03 04:26:24
问题 I'm currently developing a Spring server application using Spring Boot. I need to develop a system where some InputStream will be sent from either the local File System , or from FTP , or some other source to specific InputStreamConsumer instances, all this configured in database. The InputStreamConsumer already are managed beans. My InputStreamProviders are likely to be Prototype beans . They will not be used by other beans, but they'll need to use a TaskScheduler and periodically send

Spring can't see beans between servlet-context and contextConfigLocation beans

让人想犯罪 __ 提交于 2020-01-02 04:09:06
问题 I have a spring mvc project set up like so: <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-contexts/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-contexts/configuration-context.xml<

Why does Spring continue to initialize the context even if a bean fails to instantiate?

浪子不回头ぞ 提交于 2019-12-24 02:36:04
问题 I found a strange spring behavior in instantiating the beans during initial context Loading. I had a bean that loads large ML models. Due to insufficient memory the bean failed to instantiate throwing Out java OutOfMemoryError java heap space exception. But this doesn't stop the application from instantiating, instead it continues to load the application. Why does this happen? Is this expected? checked the spring AbstractAutowireCapableBeanFactory , try { // Mark this bean as currently in

dynamically replace the value to placeholder in config xml file in Spring Batch

末鹿安然 提交于 2019-12-23 21:41:23
问题 I am using Spring Batch 2 version. I am reading data from database using JdbcCursorItemReader .I have successfully fetched the data and also written it to a file. Below is itemReader bean defined in Job.xml File:: <bean id="itemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step"> <property name="dataSource" ref="dataSource" /> <property name="sql" value="select u.ID, u.USER_LOGIN, u.PASSWORD, u.AGE from USERS u" /> </property> <property name="rowMapper">

Create request scoped beans from a Java 8 Function

青春壹個敷衍的年華 提交于 2019-12-23 08:47:09
问题 Based on this answer I try to configure a request scope bean using java.util.Function interface. My Configuration looks like this: @Configuration public class RequestConfig { @Bean public Function<? extends BaseRequest, RequestWrapper<? extends BaseRequest, ? extends BaseResponse>> requestWrapperFactory() { return request -> requestWrapper(request); } @Bean @RequestScope public RequestWrapper<? extends BaseRequest, ? extends BaseResponse> requestWrapper( BaseRequest request) { RequestWrapper<

Spring Bean Inheritance Using Annotations Bean reference back issue

为君一笑 提交于 2019-12-19 17:43:03
问题 I want to initialise classes which are inherited using spring Bean. I have followed this stackoverflow question to implement inheritance Spring Inheritance - Annotation but I am getting exception This is my parent class public class DefaultScenarioResultAnalyzer implements ScenarioResultAnalyser, CommonConstants{ @Autowired NotificationEmail notificationEmail; @Bean public DefaultScenarioResultAnalyzer defaultScenarioResultAnalyzer() { return new DefaultScenarioResultAnalyzer(); } public