How to inject fields in bean methods?


kaylil_01:

Sonar-linter thinks the correct way to inject is:

@Bean
public Example example(DataSource datasource) {
   return new Example(datasource)
}

But if only one method is using this field. I'm curious, why is there only one way? Maybe @Autowired is better?

Quoting Sonar's rules:

When using @Autowired, dependencies need to be resolved when the class is instantiated, which can lead to early initialization of beans or to context lookups for beans that should not be looked up. To avoid this tricky problem and optimize the way contexts are loaded, dependencies should be requested as late as possible. This means using parameter injection instead of field injection for dependencies that are only used in a single @Bean method

medTech:

As stated in your quote, @Autowired requires dependencies to be resolved when the class is initialized. Parameter injection instantiates beans only when needed. This increases the effectiveness of your code. The reason for the single method is caused by the bean's life cycle. By default, beans are singletons, which means they are shared like static objects. So if multiple objects will use the bean, it will be more efficient to inject them via autowiring, as the probability that it will be used is much higher, so you can provide the Shared instance at startup.

Related


How to inject fields in bean methods?

kaylil_01: Sonar-linter thinks the correct way to inject is: @Bean public Example example(DataSource datasource) { return new Example(datasource) } But if only one method is using this field. I'm curious, why is there only one way? Maybe @Autowired is bett

How to inject primitive class into bean

Zgin, go down Lets say I have a class like this: public class MyClass { ... private Map<String, Class> eventsMapping = new HashMap<String, Class>(); ... } public void setEventsMapping(Map<String, Class> mappings){ this.eventsMapping = mappings;

How to inject primitive class into bean

Zgin, go down Lets say I have a class like this: public class MyClass { ... private Map<String, Class> eventsMapping = new HashMap<String, Class>(); ... } public void setEventsMapping(Map<String, Class> mappings){ this.eventsMapping = mappings;

Can Spring @Bean methods return instance fields?

mkjeldsen : The following Spring bean declaration appears to work and behave "as expected": @Configuration public class AppConfig { private final Foo foo; public AppConfig() { foo = new Foo(); } @Bean public Foo foo() { r

Hibernate/JPA - Annotating bean methods vs fields

benstpierre: I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wit

Hibernate/JPA - Annotating bean methods vs fields

benstpierre: I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wit

Can Spring @Bean methods return instance fields?

mkjeldsen : The following Spring bean declaration appears to work and behave "as expected": @Configuration public class AppConfig { private final Foo foo; public AppConfig() { foo = new Foo(); } @Bean public Foo foo() { r

Can Spring @Bean methods return instance fields?

mkjeldsen : The following Spring bean declaration appears to work and behave "as expected": @Configuration public class AppConfig { private final Foo foo; public AppConfig() { foo = new Foo(); } @Bean public Foo foo() { r

Can Spring @Bean methods return instance fields?

mkjeldsen : The following Spring bean declaration appears to work and behave "as expected": @Configuration public class AppConfig { private final Foo foo; public AppConfig() { foo = new Foo(); } @Bean public Foo foo() { r

Hibernate/JPA - Annotating bean methods vs fields

Benstpierre : I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wi

Spring: How to inject String bean into constructor?

Wesley: I have a class Config: configuration file public class Config { private final String p = "Prop"; @Bean public String getP(){return p;} } How can I inject it into some constructor, ie: public class SomeC { private String p; public

How do you inject a bean into a @Controller class

GriffeyDog: I'm somewhat new to Spring (using 3.0), so hoping for an easy answer. If I have a controller annotated with @Controllerand @RequestMappingand I want to set properties via dependency injection, how can I do that? The controller class does not have t

How to inject bean without interface in Mongock changelog

Olivier: I am migrating a Spring Boot application from 2.2.2 to 2.2.3. I also upgraded mongock to 4.1.16 as the 2.0.2 version used so far is no longer compatible. I have this changelog which works fine in 2.0.2 but not in 4.1.16: @ChangeSet(order = "001",

How to inject Spring bean into jsp 2.0 SimpleTag?

Crush Violence: Currently, I need the jsp 2.0 tag of spring bean to use the following code: ac = WebApplicationContextUtils.getWebApplicationContext( servletContext); ac.getBeansOfType(MyRequestedClass.class); I just got the first matching bean. This code wor

How to inject Spring bean into jsp 2.0 SimpleTag?

Crush Violence: Currently, I need the jsp 2.0 tag of spring bean to use the following code: ac = WebApplicationContextUtils.getWebApplicationContext( servletContext); ac.getBeansOfType(MyRequestedClass.class); I just got the first matching bean. This code wor

How do you inject a bean into a @Controller class

GriffeyDog: I'm somewhat new to Spring (using 3.0), so hoping for an easy answer. If I have a controller annotated with @Controllerand @RequestMappingand I want to set properties via dependency injection, how can I do that? The controller class does not have t

How to inject bean without interface in Mongock changelog

Olivier: I am migrating a Spring Boot application from 2.2.2 to 2.2.3. I also upgraded mongock to 4.1.16 as the 2.0.2 version used so far is no longer compatible. I have this changelog which works fine in 2.0.2 but not in 4.1.16: @ChangeSet(order = "001",

How to inject a bean's property source in a test

Jacques Dusenko I'm writing unit tests for my service in Spring in Java. I mock all dependencies in the test class and instantiate the test class in the constructor and then pass the mocked class to the class. The problem is that the class under test injects p

How to inject stateless session bean into servlet?

Severin My high level goal is to consume the NetBeans generated JPA code by using the Create "RESTful Web Service from Database" wizard in my servlet. To be more precise, I would like to access the facade directly from the servlet to avoid having to load some

How to inject a bean into a group of beans in spring?

fountain of love My app's dao layer needs a hibernate reference. so all concanavales need it I do every configuration and if there is a way in spring to inject a bean into a set of beans, am I wandering? Like a pointcut expression. nikhilgupta86 For this you c