Is it necessary to add getter and setter at bean definition of osgi blueprint file

358 Views Asked by At

My project based on Apache karaf osgi and dependency inject is done through blueprint file. I wish to know if getter and setter are really required for such beans. I tested it without getter and setter methods and it works, but not sure if that follow best practice. My motive is to just reduce LOC from that file.

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"..>
.
.
<bean id="emailServiceImpl" class="com.mycompany.EmailServiceImpl">
  <property name="applicationEnvironment" value="$(staging)" />
.
.
<bean id="orderDispatcherImpl" class="com.myCompany.OrderDispatcherImpl" 
  ext:field-injection="true" init-method="init">
   <property name="emailService" ref="emailServiceImpl"/>
1

There are 1 best solutions below

0
Neil Bartlett On

The Blueprint specification defines only injection of properties via JavaBeans-style setter methods.

Field injection is an extension which is specific to the Apache Aries implementation of Blueprint as used in Karaf. Therefore it will not work in other Blueprint implementations.

If you wish your blueprint container definition to be portable across implementations then it would be better to use JavaBeans-style setter methods. If you don't care about this, then you can use field injection and forget about the setter methods.

However, note that another reason to keep the setter methods may be for unit testing purposes.