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.

767 questions
183
votes
4 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
  • 4,440
  • 13
  • 50
  • 101
144
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.
  • 23,425
  • 50
  • 169
  • 316
98
votes
8 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
  • 2,111
  • 4
  • 22
  • 42
77
votes
8 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
  • 9,165
  • 28
  • 114
  • 219
60
votes
4 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
  • 2,159
  • 2
  • 14
  • 9
50
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
  • 3,574
  • 8
  • 41
  • 78
43
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,864
  • 6
  • 31
  • 42
37
votes
3 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,677
  • 10
  • 33
  • 56
35
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
  • 2,324
  • 1
  • 18
  • 24
28
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 change and then I have to reinitialize the Bean.
Fip
  • 503
  • 1
  • 5
  • 11
24
votes
4 answers

A bean with that name has already been defined in class path resource [path] and overriding is disabled

I have the java configuration for the Spring Data Elaticsearch(using Transport Client) and ESTemplate. Here some except: @Configuration @EnableElasticsearchRepositories(basePackages =…
20
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
  • 15,303
  • 7
  • 59
  • 83
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
  • 5,846
  • 11
  • 68
  • 141
17
votes
5 answers

Create @MockBean with qualifier by annotating class?

In my Spring Boot test I'm using 2 mock beans with different qualifiers: @RunWith(SpringRunner.class) @SpringBootTest class HohoTest { @MockBean @Qualifier("haha") IHaha ahaha; @MockBean @Qualifier("hoho") IHaha ohoho; } Since I'm not using…
maslick
  • 2,903
  • 3
  • 28
  • 50
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,950
  • 6
  • 35
  • 65
1
2 3
51 52