@Autowired vs @Autowired with Setter

autowired vs inject
org springframework beans factory annotation autowired(required=true in spring boot)
spring autowire interface multiple implementations
spring autowire multiple beans of same type
bean vs autowired
how to autowire parameterized constructor in spring boot
spring dependency injection annotation example mkyong
default autowiring in spring

I have a spring boot app that I am using with the Web Plugin.

In one class I have:

package com.test.company
@Component
@RestController
public class CompanyService {

   @Autowired
   private MongoTemplate mongoTemplate;

   @Autowired
   private Environment env;

And in another class I have:

package com.test.company
@Component
@RestController
public class CustomerSignUpService {

   private static MongoTemplate mongoTemplate;

   @Autowired
   private Environment env;

   @Autowired
   public void setMongoTemplate(MongoTemplate mongoTemplate) {
          this.mongoTemplate = mongoTemplate;
   }

Both classes work but if I try to inject mongo into the CusomterSignUpService class like I did in the CompanyService class, the env is injected fine, but mongo doesn't inject and I get a null pointer exception if I try to use it.

Any thoughts? Main package is com.test.

I believe your Controller might need to look like (removed static from property):

package com.test.company
@Component
@RestController
public class CustomerSignUpService {

   @Autowired
   private MongoTemplate mongoTemplate;

   @Autowired
   private Environment env;

   ...
   ...
}

Spring @Autowired Annotation, bean on the setter method just like @Required annotation, constructor, a property or methods with arbitrary names and/or multiple arguments. But @Autowired and @Inject fails as they try to find a bean name specified in @Qualifier. So @Autowired and @Inject works very similar but @Resource has bit different feature. The order of choosing injection type @Autowired and @Inject. 1.Match by Type-> bean with same Data type of the variable should be available in spring container. 2.

You can use @Autowired both in the attribute and in the setter, but your attribute must be an instance variable, not a static one.

So do this and your code should run fine:

package com.test.company
@Component
@RestController
public class CustomerSignUpService {

   private MongoTemplate mongoTemplate;

   @Autowired
   private Environment env;

   @Autowired
   public void setMongoTemplate(MongoTemplate mongoTemplate) {
          this.mongoTemplate = mongoTemplate;
   }

Note that the static reserved word was taken from your attribute declaration.

What does @autowired do?, is basically Spring injecting (doing the initializing of the variable) the variable in based on configurations you defined in classes with the @Component annotation. The @Autowired and @Qualifier annotations can be used on top of a field, setter methods or on top of a constructor of a class. If we declare the autowire annotation on top of the method or property, the IOC container always searches whether a dependency property name and the bean id in XML are matched or not, if those are matched the automatic

Remove static from the property and try it without it

package com.test.company
@Component
@RestController
public class CustomerSignUpService {

   @Autowired
   private MongoTemplate mongoTemplate;

   @Autowired
   private Environment env;

   ...
   ...
}

Difference between @Autowired and @Inject , Once annotation injection is enabled, autowiring can be used on properties, setters, and constructors. 3.1. @Autowired on Properties. The @Autowired annotation can be used on setter methods. In the below example, when the annotation is used on the setter method, the setter method is called with the instance of FooFormatter when FooService is created:

Avoid autowiring - Java queries, Learn the differences of using @Autowired on abstract classes vs. These execution paths are applicable to both setter and field injection. Marks a constructor, field, setter method, or config method as to be autowired by Spring's dependency injection facilities. This is an alternative to the JSR-330 Inject annotation, adding required-vs-optional semantics. Only one constructor of any given bean class may declare this annotation with the required () attribute set to true

Guide to Spring @Autowired, The @Autowired annotation can be used to autowire bean on the setter method just like @Required annotation, constructor, a property or methods with arbitrary​  @Autowired annotation is used when you want to autowire a bean. @Autowired is not limited to setter. It can be used with a constructor and a field as well. If you use @Autowired annotation on a field, that field will be autowired with bean having matching data type. @Required checks if a particular property has been set or not.

Wiring in Spring: @Autowired, @Resource and @Inject, Each bean must have a setter and each setter must be decorated with an @​Autowired or @Inject annotation. Although this boilerplate definitely  Field injection is a very popular practice in Dependency Injection frameworks, such as Spring. It has, however, several serious trade-offs and should generally be avoided. Injection Types. There are three main ways to inject your dependencies into your class. Constructor, Setter (Method) and Field injection.

Comments
  • Remove static.