Ver Mensaje Individual
  #3 (permalink)  
Antiguo 12/10/2014, 01:38
rupertomartinez
 
Fecha de Ingreso: octubre-2014
Mensajes: 2
Antigüedad: 9 años, 6 meses
Puntos: 0
Respuesta: Duda concepto: inyección de dependencia y acoplamiento en Spring

Hola chuidiang, muchísimas gracias por responderme.

He hecho lo que me has dicho, me ha quedado esto:

Main.java
HelloWorldHelper helloWorldHelper = new HelloWorldImpl();
HelloWorldHelper.hello();


HelloWorldHelper
public class HelloWorldHelper{

HelloWorldInterface helloWorldInterface;

public HelloWorldHelper(){
helloWorldInterface = new HelloWorldImpl();
}

public void setHelloWorldInterface(HelloWorldInterface helloWorldInterface){
this.helloWorldInterface = helloWorldInterface;
}

}

beans.xml
<bean id="helloWorld" class="com.pp.HelloWorld">
<property name="helloWorldInterface" ref="HelloWorldImpl" />
</bean>
<bean id="HelloWorldImpl" class="com.pp.HelloWorldImpl" />



Pero no entiendo que ventaja tiene usar un helper, frente a no usarlo. Si no lo usase me quedaría el código así:

Main.java
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloWorldInterface helloWorld = (HelloWorldInterface) context.getBean("helloWorld");
helloWorld.hello();

beans.xml
<bean id="helloWorld" class="com.pp.springBeans.HelloWorldImpl">
</bean>


En ambos casos utilizo una esta clase y una interfaz:
HelloWorldImpl
public class HelloWorldImpl implements HelloWorldInterface{
public void hello() {
System.out.println("Hello world! ");
}
}

HelloWorldInterface.java
public interface HelloWorldInterface {
public void hello();
}



Muchiiiiiiiiiiiiiiiiiisimas gracias!! estos conceptos se me hacen un poco duros.