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
4
votes
2 answers

Spring bean definition - get bean class

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…
john Smith
  • 1,565
  • 7
  • 34
  • 54
4
votes
1 answer

How do I know at runtime which implementing classes are set for my Spring beans?

In hybris, is there an easy way to know which implementing class is being used for a certain Spring bean? I mean, I can override a Bean by doing something like this:
Henrique Ordine
  • 3,337
  • 4
  • 44
  • 70
4
votes
1 answer

Dynamically adding a list of bean references to bean definition

I'm trying to dynamically register some spring beans in my ApplicationContext by using the ApplicationContextAware interface. I'm using the BeanDefitionBuilder to build the bean definitions and I register them with…
enpo
  • 128
  • 8
3
votes
4 answers

Changing the Spring beans implementation at runtime

I have an Interface and multiple implementation. I'm auto wiring the interface in classes for usage. I need to choose different implementation at runtime. public class Util { public void getClient(); } Implementations public class UtilOne…
Ajay Kumar
  • 33
  • 6
3
votes
1 answer

Spring Boot @MockBeans - How to use the same beans in multiple test classes

I have multiple security test classes where I am testing authorization to different REST endpoints, and I would like to mock the beans of all the repositories that ApplicationContext will spin up. I have the tests running and everything works…
Droem19
  • 59
  • 1
  • 8
3
votes
0 answers

How to get the caller Spring bean name in a Spring application?

I have a Spring service named EmailService like this: @Service public class EmailService { public void send (String recipient, String content) { // send out an email } } There are several other Spring services using EmailService,…
johnlinp
  • 853
  • 6
  • 22
3
votes
1 answer

Reflect env variable reload to java bean

public static final String STUDENTS = "studentsInfo"; @Bean(name = "students") public List getStudents() { String configuredStudents = env.getProperty("studentsInfo"); //JSON having list of students info …
supraja
  • 93
  • 2
  • 11
3
votes
1 answer

Why does @Cachable(...) work with @Bean return mock() but not with @MockedBean

Why does the cache get filled with Values when using @Autowired ServiceXY serviceXY @TestConfiguration static class AppDefCachingTestConfiguration { @Bean public ServiceXY ServiceXYMock() { return mock(ServiceXY.class); } } But…
3
votes
1 answer

Should we use Spring @Bean with static method?

Is this a good practice to use @Bean with static method? public class Foo { } @Configuration public FooFactory { @Bean public static Foo getFoo() { return new Foo(); } }
G_Basak
  • 45
  • 2
  • 8
3
votes
2 answers

Use @Conditional for inject request scope and prototype bean

I want to inject an object like Foo in some class. I have two bean for Foo. One of them is RequestScope and other is Prototype. I want to use @Conditional for this. Is there any annotation like ConditionalOnHttpRequestExist that separate these…
mohammad_1m2
  • 1,571
  • 5
  • 19
  • 38
3
votes
1 answer

How to create proxy object at runtime using LambdaMetaFactory?

How can I create proxy objects for SAM/functional interfaces using LambdaMetaFactory ie. equivalent of public static Object java.lang.reflect.Proxy.newProxyInstance(ClassLoader, Class[], InvocationHandler) Eg. I have multiple factory…
S M
  • 81
  • 7
3
votes
0 answers

Spring-beans DefaultListableBeanFactory - how allBeanNamesByType is populated

in spring beans class DefaultListableBeanFactory we have variable which is used inside method getBeanNamesForType. This is used as a cache. I want to understand how this is populated as in one of our application is not populating beans of the type…
sanjay pandey
  • 61
  • 2
  • 7
3
votes
4 answers

How to register bean dynamically in Spring Boot?

I am looking to override the SSLContext of Spring at runtime. Hence I am trying to find ways to register the below method as a bean dynamically. For Ex. When a GetMapping endpoint is invoked, the below method should be injected as a bean into…
Sapnesh Naik
  • 11,011
  • 7
  • 63
  • 98
3
votes
2 answers

SpringBoot and use / load a dynamic "applicationContext.xml" instead of hard coded one

I've seen a hundred examples of this: import org.springframework.boot.autoconfigure.SpringBootApplication; import…
granadaCoder
  • 26,328
  • 10
  • 113
  • 146
3
votes
1 answer

How to load a bean only if a property is false or not defined?

I've a bean that should be loaded only if a property is false or not defined. If I annotate it with: @ConditionalOnProperty(prefix = "myFeature1", name = "enable", havingValue = "false") the property myFeature1.enable has to be explicitly set to…
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240