Bean @Property injection
synclpz Aug 27, 2013 10:50 AMImagine we have a bean implementation which involves property usage, specified in switchyard.xml component/composite properties.
It looks like this at the moment:
@Service(SomeService.class) public class SomeServiceImpl implements SomeService { @Property(name = "string-property") String stringProperty; @Property(name = "int-property") String intProperty; //could be only String, but is integer semantically! public void someServiceMethod() { // there we should do int intPropValue = 0; try { intPropValue = Integer.parseInt(intProperty); } catch (Throwable t) { intPropValue = SOME_DEFAULT_FOR_EXAMPLE; } } }
This is not a good way, because properties are set only while deployment, but should be parsed (integer is simple examle, but there could be more complex) any time service is invoked.
I tried to achieve reading properties upon deployment via @PostConstruct
@Service(SomeService.class) public class SomeServiceImpl implements SomeService { @Property(name = "string-property") String stringProperty; @Property(name = "int-property") String intProperty; //could be only String, but is integer semantically! int intPropValue; @PostConstruct void init() { try { intPropValue = Integer.parseInt(intProperty); } catch (Throwable t) { intPropValue = SOME_DEFAULT_FOR_EXAMPLE; } } public void someServiceMethod() { //just use intPropValue } }
But no success, because it seems like they are injected after @PostConstruct.
So, the questions are:
1. Why not inject properties before @PostConstruct? They are not changing while bean is being invoked.
2. Are there any methods of extracting implementation configuration parameters anywhere but properties supported internally in SY?
Now I resolve this issue by using code:
@Service(SomeService.class) public class SomeServiceImpl implements SomeService { @Property(name = "string-property") String stringProperty; @Property(name = "int-property") String intProperty; //could be only String, but is integer semantically! int intPropValue; private boolean init = false; private void init() { try { intPropValue = Integer.parseInt(intProperty); } catch (Throwable t) { intPropValue = SOME_DEFAULT_FOR_EXAMPLE; } init = true; } public void someServiceMethod() { if (!init) { init() } } }