Home > Net >  How does @Value annotation inject values when passed in parameters of a method?
How does @Value annotation inject values when passed in parameters of a method?

Time:07-06

I am new to Spring, I was working with @Value annotation and found out that it can be applied to fields or constructor/method parameters, but as I was trying to inject value using parameters it was not injecting the value for my parameters and I was getting values as null .

I have used @Value in the parameter of the method below

public void setName(@Value("Adventure of War") String name) {
        System.out.println("Setting Company Name");
        this.name = name;
}

Complete code(Company.java)

package gd.rf.anuragsaini.stereotype;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Company {
    String name;
    String motive;

    public void setName(@Value("Adventure of War") String name) {
        System.out.println("Setting Company Name");
        this.name = name;
    }

    public void setMotive(@Value("A place for War") String motive) {
        System.out.println("Setting Company Motive");
        this.motive = motive;
    }

    @Override
    public String toString() {
        return "Company{"  
                "name='"   name   '\''  
                ", motive='"   motive   '\''  
                '}';
    }
}

Main File(App.java)

package gd.rf.anuragsaini.stereotype;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        ApplicationContext IOC = new ClassPathXmlApplicationContext("config.xml");
        Company c1 = IOC.getBean("company", Company.class);
        System.out.println(c1);
    }
}

Output

Company{name='null', motive='null'}

CodePudding user response:

Like that the setters will not be invoked.When constructing the bean of type Company, the frameWork use by default the default constructor of the class Company.

So you should use @Autowired for the setters to garantee that the setters will be invoked by the frameWork when constructing those beans :

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Company {
    String name;
    String motive;

    @Autowired
    public void setName(@Value("Adventure of War") String name) {
        System.out.println("Setting Company Name");
        this.name = name;
    }

    @Autowired
    public void setMotive(@Value("A place for War") String motive) {
        System.out.println("Setting Company Motive");
        this.motive = motive;
    }

    @Override
    public String toString() {
        return "Company{"   "name='"   name   '\''   ", motive='"   motive   '\''   '}';
    }
}

Output :

Company{name='Adventure of War', motive='A place for War'}

CodePudding user response:

When using @Value you need to pass a placeholder ${} that contains the key of a property that will be resolved against PropertySource. What you try to do here is to pass the value Adventure of War. You should change it to: ${adventure.value} and add the key in your properties file (application.properties / application.yaml / ...).

  • Related