Amith
Amith

Reputation: 116

Spring @Value does not inject property of one bean into another bean's property

I have a bean A, with a property pA. Now, i want to inject this property into another bean B's property pB. Tried using @Value, with both $ and #, but its not working. This is my class B.

@Component
public class B
{

    @Value("${a.aP}")
    private boolean bP;

}

Class A looks like below:

@Component
public class A
{
    private boolean aP;

if(some condition){

aP = true;

}

}

Upvotes: 1

Views: 567

Answers (1)

yname
yname

Reputation: 2245

You can inject bean A to B and then use @PostConstruct to set bP:

@Component
public class B {

   private boolean bP;

   @Autowired
   private A a;

   @PostConstruct
   public void postConstructMenthod() {
      bP = a.getAP();
   }
}

Upvotes: 2

Related Questions