Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property values in bean class are null in constructor

I'm trying to use values from my application.properties file in my service implementation class/bean. But when the bean is initialized through my config class, the property values are all null.

Config class:

@Configuration
public class AppConfig {
    @Bean AppServiceImpl appServiceImpl() {
        return new AppServiceImpl();
    }
}

Service class:

@Component
public class AppServiceImpl implements AppService {
    @Value("${value.one}")
    String value_one;

    @Value("${value.two}")
    String value_two;

    @Value("${value.three}")
    String value_three;

    //values are null here
    public AppServiceImpl() {
        functionOne(value_one, value_two, value_three);
    }
}

application.properties(under src/main/resources):

value.one=1
value.two=2
value.three=3

Doing some debugging i can see that the AppConfig class has found the properties file and if i try to declare @Value("${value.one}") String value_one; there it shows it has been given the value 1 as expected.

But in my AppServiceImpl class, all the values are null. What am I doing wrong here? How should this be done properly in Springboot? Or just Spring even.

Thanks.

like image 829
mTv Avatar asked May 25 '18 08:05

mTv


People also ask

Can a bean be null?

Internal representation of a null bean instance, e.g. for a null value returned from FactoryBean. getObject() or from a factory method. Each such null bean is represented by a dedicated NullBean instance which are not equal to each other, uniquely differentiating each bean as returned from all variants of BeanFactory.

Can spring bean have constructor?

No, you are not required to use default (no arg) constructors. If you want to reference another bean in your application context, you can do it using the ref attribute of the constructor-arg element rather than the val attribute.

Which annotation is used to inject property values into beans?

Overview. In this quick tutorial, we're going to have a look at the @Value Spring annotation. This annotation can be used for injecting values into fields in Spring-managed beans, and it can be applied at the field or constructor/method parameter level.

What is null property?

Null can be something that is not valid or canceled out. Maybe it means the property is no longer available for sale.


2 Answers

If you use the values in the constructor they won't be available right away. Indeed they are injected on attribute. What's happening here is after spring created an instance then it will update the attribute value.

If you want to use those values in the constructor you should use constructor injection. Injection by constructor is a best practice.

public class AppServiceImpl implements AppService {
    String value_one;
    String value_two;
    String value_three;

    //values are null here
    public AppServiceImpl(String value1, String value2, String value3) {
        value_one = value1;
        value_two = value2;
        value_three = value3;
        functionOne(value_one, value_two, value_three);
    }
}

And your configuration class

@Configuration
public class AppConfig {
    @Bean
    AppServiceImpl appServiceImpl(@Value("${value.one}") String value1,  @Value("${value.two}") String value2,  @Value("${value.three}") String value3) {
        return new AppServiceImpl(value1, value2, value3);
    }
}
like image 115
JEY Avatar answered Sep 17 '22 13:09

JEY


Those values are injected after the instance is created. So they are null in the constructor.

To execute a method after the values are injected use @javax.annotation.PostConstruct:

@PostConstruct
public void init(){ // method name doesn't matter
     functionOne(value_one, value_two, value_three);
}
like image 39
Lino Avatar answered Sep 20 '22 13:09

Lino