java - Spring DI: from XML to Annotations -


for merely educational reasons i'm trying explore differences between using annotations or xml configuration di spring.

in order tot that, have example of messager app can use different implementations different kind of jobs (send email, send sms).

using test class can bean factory, injected different implementation if want.

this how looks:

imessageservice.java

    public interface imessageservice {         public void sendmessage(string message, string recipient);      } 

emailmessageserviceimpl.java

public class emailmessageserviceimpl implements imessageservice {      @override     public void sendmessage(string message, string recipient) {         //validations email, etc.         system.out.println(string.format("email message: %s. recipient: %s", message, recipient));     }  } 

smsmessageserviceimpl.java

public class smsmessageserviceimpl implements imessageservice {      @override     public void sendmessage(string message, string recipient) {         //phone validations         system.out.println(string.format("sms message: %s. sent to: %s", message, recipient));     }  } 

app.java

    public class app {          private imessageservice messageservice;          public void processmessages(string message, string recipient) {              messageservice.sendmessage(message, recipient);          }          public void setmessageservice(imessageservice messageservice) {             this.messageservice = messageservice;         }  } 

applicationcontext.xml

    <!--    declaring app , injecting 2 different implementations of imessageservice--> <bean id="appemail" class="org.invenio.tic.holamundospring.app">     <property name="messageservice" ref="emailimpl"/> </bean>  <bean id="appsms" class="org.invenio.tic.holamundospring.app">     <property name="messageservice" ref="smsimpl"/> </bean>   <!--    declaring beans implementations--> <bean id="emailimpl" class="org.invenio.tic.holamundospring.service.emailmessageserviceimpl"/>  <bean id="smsimpl" class="org.invenio.tic.holamundospring.service.smsmessageserviceimpl"/> 

and test class main.java

    public class main {          public static void main(string[] args) {          applicationcontext context = new classpathxmlapplicationcontext("applicationcontext.xml");          app app = (app) context.getbean("appemail");         app.processmessages("hi, email", "info@invenio.org");          app = (app) context.getbean("appsms");         app.processmessages("this sms", "88100452");      }  } 

this prints:

    email message: hi, email. recipient: info@invenio.org     sms message: sms. sent to: 88100452 

when try annotations, using autowiring , autodiscovery features of spring, i'm loosing ability app different implementations, since annotation @autowired let me choose implementation want, hardcoded app.java class.

app.java

    @component     public class app {      @autowired     @qualifier("emailserviceimpl")     private imessageservice messageservice;      public void processmessages(string message, string recipient) {          messageservice.sendmessage(message, recipient);      }      public void setmessageservice(imessageservice messageservice) {         this.messageservice = messageservice;     }  } 

is there way around issue? passing qualifier name in parameter or let me, in runtime, change implementation of messageservice?

or 1 of cons of using annotations?

thanks -alejandro.

try changing @qualifier("emailserviceimpl") @qualifier("emailimpl"), have defined in applicationcontext.xml.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -