java - Spring component-scan without autowiring? -


does makes sense use component-scan without using autowiring on spring?

i idea of having component scan on mvc environment controllers, hell avoid declaring daos, services ... on xml.

however not big fan of autowiring, so, still possible manually inject desired beans using xml config file or non-sense?

update : advantage of using @autowired instead of explicit declaration via xml (please not provide "less xml configuration" advantage)

i would

  • add @component implementation classes bean created each of them
  • not associate @autowired / @resource member declarations corresponding these implementations in other classes spring cannot autowire them.
  • define beans these component beans needed, in xml using <bean> node , <property> elements inject them through setters or <constructor-arg> inject them through constructors.

example:

interface:

package com.krovi.compscan;  public interface myinterface {     void method(); } 

implementation declared component spring di framework create bean this:

package com.krovi.compscan;  import org.springframework.stereotype.component;  @component public class myimpl implements myinterface {     public void method()     {         system.out.println("method definition in implementation");     } } 

a typical client use component-based bean injected explicitly through configuration file, through constructors.

package com.krovi.compscan;  import org.springframework.context.support.classpathxmlapplicationcontext;  public class myclient {     private myinterface myinterface;      public myclient(myinterface i)     {         this.myinterface = i;     }      public void clientmethod()     {         myinterface.method();     }      // main method test.         public static void main(         string[] args)     {         classpathxmlapplicationcontext context =             new classpathxmlapplicationcontext("classpath:meta-inf/compscan.xml");         myclient client =             context.getbean(myclient.class);         client.clientmethod();     } } 

another typical client use component-based bean injected explicitly through configuration file using setters:

package com.krovi.compscan;  import org.springframework.context.support.classpathxmlapplicationcontext;  public class myanotherclient {     private myinterface impl;      protected myanotherclient()     {      }      public myinterface getimpl()     {         return impl;     }      public void setimpl(         myinterface impl)     {         this.impl = impl;     }      public void clientmethod()     {         impl.method();     }      public static void main(         string[] args)     {         classpathxmlapplicationcontext context =             new classpathxmlapplicationcontext("classpath:meta-inf/compscan.xml");         myanotherclient client =             context.getbean(myanotherclient.class);         client.clientmethod();     } } 

bean configuration file:

<?xml version="1.0" encoding="utf-8"?> <beans      xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xmlns:context="http://www.springframework.org/schema/context"     xsi:schemalocation="         http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans.xsd          http://www.springframework.org/schema/context          http://www.springframework.org/schema/context/spring-context-3.0.xsd">      <!-- component scan create beans @component classes -->     <context:component-scan base-package="com.krovi.compscan" />      <!-- explicit bean declaration clients           @component classes injected      -->     <bean id="myclient" class="com.krovi.compscan.myclient">         <constructor-arg ref="myimpl" />     </bean>     <bean id="anotherclient" class="com.krovi.compscan.myanotherclient">         <property name="impl" ref="myimpl" />     </bean> </beans> 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -