Questions tagged [spring-bean]

A simple JavaBean managed by the Spring IoC container.

A simple JavaBean managed by the Spring IoC container. These beans are created with the configuration metadata that is supplied to the container, for example, in the form of XML <bean/> definitions or @Bean annotation. Actually, the BeanFactory is the main IoC container which instantiates, configures, and manages these beans. These beans typically have associations with each other, and therefore have dependencies between themselves. These dependencies are configured in the configuration files used by the BeanFactory. Other dependencies that are invisible from the configuration file could be a function of programmatic interactions between beans at run-time.

663 questions
137
votes
3 answers

How to define @Value as optional

I have the following in a Spring bean: @Value("${myValue}") private String value; The value is correctly injected. However, the variable needs to be optional, it is passed in as a command line parameter (which is then added to the Spring context…
user1052610
  • 3,730
  • 7
  • 40
  • 79
130
votes
9 answers

Is there a way to @Autowire a bean that requires constructor arguments?

I'm using Spring 3.0.5 and am using @Autowire annotation for my class members as much as possible. One of the beans that I need to autowire requires arguments to its constructor. I've looked through the Spring docs, but cannot seem to find any…
Eric B.
  • 20,257
  • 43
  • 147
  • 269
70
votes
5 answers

SpringBoot - BeanDefinitionOverrideException: Invalid bean definition

I am trying to setup DynamoDB locally with Spring Boot. Initially I got the setup working and was able to write/save to DynamoDB via a repository. From that point I added more classes to build my application. Now when I try to start my application,…
Vino
  • 1,685
  • 2
  • 15
  • 34
51
votes
5 answers

Spring choose bean implementation at runtime

I'm using Spring Beans with annotations and I need to choose different implementation at runtime. @Service public class MyService { public void test(){...} } For example for windows's platform I need MyServiceWin extending MyService, for linux…
Tobia
  • 8,015
  • 24
  • 90
  • 182
46
votes
3 answers

Difference between JavaBean and Spring bean

I am new to Spring MVC and have a little idea of the usage of java beans in Java. What is the basic difference between a Java bean and Spring bean?
Arpit Shah
  • 1,969
  • 2
  • 12
  • 9
38
votes
1 answer

Is it possible to set a bean name using annotations in Spring Framework?

I have a bean like this: @Bean public String myBean(){ return "My bean"; } I want to autowire it: @Autowired @Qualifier("myBean") public void setMyBean(String myBean){ this.myBean=myBean; } I need something…
Oleksandr
  • 2,821
  • 6
  • 28
  • 59
37
votes
3 answers

Spring bean with runtime constructor arguments

I want to create a Spring bean in Spring Java configuration with some constructor arguments passed at runtime. I have created the following Java config, in which there is a bean fixedLengthReport that expects some arguments in…
suraj bahl
  • 2,195
  • 4
  • 24
  • 37
31
votes
2 answers

Spring autowiring order and @PostConstruct

I have a question about auto-wiring order and @PostConstruct logic in Spring. For example following demo code I have a main Spring Boot class: @SpringBootApplication public class Demo1Application { @Autowired BeanB beanb; public static…
cacert
  • 2,237
  • 6
  • 28
  • 50
26
votes
2 answers

Do Spring prototype beans need to be destroyed manually?

I noticed that the @PreDestroy hooks of my prototype scoped Spring beans were not getting executed. I have since read here that this is actually by design. The Spring container will destroy singleton beans but will not destroy prototype beans. It…
IqbalHamid
  • 1,570
  • 1
  • 12
  • 15
19
votes
4 answers

Spring @Value("${}") often null

I'm using Spring Boot application. In some @Component class @Value fields are loaded, instead on other classes they are always null. Seems that @Value(s) are loaded after my @Bean/@Component are created. I need to load some values from a properties…
drenda
  • 4,771
  • 8
  • 50
  • 103
16
votes
5 answers

Is it advantageous to create a Spring bean when I can access the only static method directly with class name

I think my understanding of spring beans is a bit off. I was working on my project and I was thinking about this situation. Say I have class Foo class Foo(){ public void doSomething(Object a , Object b){ // input parameters does not matter…
Karthik
  • 4,652
  • 4
  • 27
  • 62
15
votes
1 answer

Detecting unused Spring beans

Given a Spring configuration that exclusively contains eager (non-lazy) singleton beans, i.e. the defaults, is it possible to have Spring throw an exception in the case where any of those beans is not injected anywhere? I'm essentially looking for a…
jaco0646
  • 11,033
  • 7
  • 47
  • 64
14
votes
1 answer

How to reinitialize a Spring Bean?

Is it possible to reinitialize a Spring Bean on runtime? My Bean uses static settings which in some cases changes and then i have to reinitialize the bean.
Fip
  • 323
  • 1
  • 2
  • 10
14
votes
1 answer

Dynamically add new queues, bindings and exchanges as beans

I'm currently working on a rabbit-amqp implementation project and use spring-rabbit to programmatically setup all my queues, bindings and exchanges. (spring-rabbit-1.3.4 and spring-framework versions 3.2.0) The declaration in a javaconfiguration…
JustinV
  • 155
  • 1
  • 6
12
votes
2 answers

how to get multiple instances of same bean in spring?

By default, spring beans are singletons. I am wondering if there is a way to get multiple instances of same bean for processing. Here is what I do currently @Configuration public class ApplicationMain { @Value("${service.num: not…
brain storm
  • 25,842
  • 54
  • 187
  • 349
1
2 3
44 45