2014-03-19 4 views
1

http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-method-injection에있는 Lookup method injection의 기사를 읽었습니다. 이에Spring에서 조회 메소드 삽입

, 성명

If the method is abstract, the dynamically-generated subclass implements the method. 
Otherwise, the dynamically-generated subclass overrides the concrete method defined in the original class. 

내가 예와 함께 설명하십시오이 two.Can 사람의 차이를 이해하지 못했다가?

답변

0

여기에서 Spring은 CGLib을 사용합니다. 그건 중요하지 않습니다. 요점은 Spring이 인스턴스를 클라이언트에 전달하는 프록시 객체를 작성한다는 것을 이해해야한다는 것입니다. 봄은 당신이 CGLIB를 사용하는 생성

프록시 객체는, 어느 추상 메서드 또는 재정 구체적인 방법을 구현할 수 있습니다. 따라서 abstract로 표시된 메소드를 제공하거나 abstract로 표시되지 않은 메소드를 제공 한 경우 (따라서 메소드가 대체 됨) 클래스를 검사하여 클래스를 검사합니다.

프록시는 꽤 많은 컨테이너에서 사용됩니다. 그것들은 Java Enterprise 세계에서도 런타임에 공통적으로 사용됩니다. 클래스가 런타임 프록시에 의해 서브 클래 싱 될 수있는 곳에서는 인자를 가진 public 생성자를 원할 경우에는 인수가없는 생성자 (public이 아닌 경우)를 제공해야합니다.

+0

고맙습니다.하지만 내가 추상적이거나 비 추상적 인 방법을 제공한다면 어떤 차이가 있습니까? 찬성/반대? – Anand

2
Lookup Method DI:- 

What is Lookup Method- 
Here lookup method means 
if a method, if it is not having any implementation or 
if a method, if it is required any depedency we can consider that method as a lookup method. 

for ex. 
1. In case of interface 

interface Test{ 

public void a(); //lookup method 
public void b(); //lookup method 

} 

2. In case of abstract class 

abstract class Test{ 

    abstract public void a(); //lookup method 

    public void b(){ 

    } 
} 

3. In case of concrete class 

class Test{ 

/* if you want to override method a() then you can call this method also like lookup method */ 
    public void a(){ 
     //implementation 
    } 

    public void b(){ 
     //implementation 
    } 
} 

Note:-if you want to provide implementation to that method you can call that method as lookup method. 

By using spring you can provide implementation, 
for abstract classes you can provide implementation, 
for interface you can provide implementation and 
in case if you don’t satisfy existing implementation from concreate class that implementation also you can override. 

**Example:-** 

*Required jar file 
commons-logging-1.1.3.jar 
org.springframework.asm-3.0.1.RELEASE-A.jar 
org.springframework.beans-3.0.1.RELEASE-A.jar 
org.springframework.context-3.0.1.RELEASE-A.jar 
org.springframework.core-3.0.1.RELEASE-A.jar 
org.springframework.expression-3.0.1.RELEASE-A.jar 
cglib-nodep-2.2.jar :- cglib jar will help to generate runtime proxy. 

**Engine class** 

package beans; 

public class Engine { 
    private String name; 

    public void setName(String name) { 
     this.name = name; 
    } 
    public String getName() { 
     return name; 
    } 
} 

**Car interface** 

package beans; 

public interface Car { 

    //return Engine class object 
    public Engine myCarEngine(); 
} 

**Bus abstract class** 

package beans; 

abstract public class Bus { 

    //return Engine class object 
    abstract public Engine myBusEngine(); 
} 

**Truk concrete class** 

package beans; 

public class Truck { 

    //if you don't satisfy this existing implementation you can override by using lookup method. 
    public Engine myTrukEngine(){ 
     Engine e=new Engine(); 
     e.setName("Eicher-Truck"); 
     return e; 
    } 
} 

**spring.xml** 

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 

<beans> 

    <!-- for car interface provide lookup method --> 
    <bean id="c" class="beans.Car"> 
     <lookup-method name="myCarEngine" bean="e" /> 
    </bean> 
    <bean id="e" class="beans.Engine"> 
     <property name="name" value="swift Car Engine" /> 
    </bean> 


    <!-- for bus abstract provide lookup method --> 
    <bean id="b" class="beans.Bus"> 
     <lookup-method name="myBusEngine" bean="e1" /> 
    </bean> 
    <bean id="e1" class="beans.Engine"> 
     <property name="name" value="TATA BusEngine" /> 
    </bean> 


    <!-- for Truck concrete provide lookup method --> 
    <bean id="t" class="beans.Truck"> 
     <lookup-method name="myTrukEngine" bean="e2" /> 
    </bean> 
    <bean id="e2" class="beans.Engine"> 
     <property name="name" value="BENZ Truck Engine" /> 
    </bean> 

</beans> 

**Client class** 

package test; 

import java.util.*; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

import beans.Bus; 
import beans.Car; 
import beans.Truck; 

public class Client { 

    public static void main(String[] args) { 

     ApplicationContext ap= new ClassPathXmlApplicationContext("resource/spring.xml"); 
     System.out.println("-----------Car----------"); 
     Car c=(Car)ap.getBean("c"); 
     System.out.println("Name of Class generated by spring at runtime="+c.getClass().getCanonicalName()); 
     System.out.println("Engine Name="+c.myCarEngine().getName()); 

     System.out.println("-----------Bus----------"); 
     Bus b=(Bus)ap.getBean("b"); 
     System.out.println("Name of Class generated by spring at runtime="+b.getClass().getCanonicalName()); 
     System.out.println("Engine Name="+b.myBusEngine().getName()); 

     System.out.println("-----------Truk----------"); 
     Truck t=(Truck)ap.getBean("t"); 
     System.out.println("Name of Class generated by spring at runtime="+t.getClass().getCanonicalName()); 
     System.out.println("Engine Name="+t.myTrukEngine().getName()); 

    } 


} 

**OutPut:-** 

———–Car———- 
Name of Class generated by spring at runtime=beans.Car$$EnhancerByCGLIB$$68fda491 
Engine Name=swift Car Engine 

———–Bus———- 
Name of Class generated by spring at runtime=beans.Bus$$EnhancerByCGLIB$$cfce5a7 
Engine Name=TATA BusEngine 

———–Truk———- 
Name of Class generated by spring at runtime=beans.Truck$$EnhancerByCGLIB$$dc82ada3 
Engine Name=BENZ Truck Engine 

How to spring provide implementation :- 
if we load spring.xml file into Ioc Container ,then Ioc container generate runtime proxy class by using cglib jar for provide implementation. 
like.. 


//This class generate by Spring at runtime 
class CarProxy extends Car{ 
    @Override 
    public Engine myCarEngine(){ 

     //implementation 
     return e; 
    } 
} 
0

나는 차이점은 일반적인 개념은 주입 방법을 원하는대로 룩업 방법을 구현하지 않는다는 것입니다. 하지만 이것으로 인해 문제가 발생합니다 : 1) 이미 abstract 인 클래스에서이 메소드를 추상으로 정의하면 하위 클래스가이를 구현해야합니다. 2) 비 추상 클래스에서 이러한 메서드를 정의하는 경우이 클래스를 추상화해야합니다. 이러한 강제 변경을 피하려면 몸체를 제공하는 것이 더 편리합니다.

관련 문제