Search Method - Embedding a Bean Prototype in a Singleton Bean Release

I am developing code for the code Injecting a prototype bean into a singleton bean, so far I have developed the code as shown below, and when I run the main method, I see that the error is below.

Jan 04, 2017 2:59:41 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6ed322: startup date [Wed Jan 04 14:59:41 IST 2017]; root of context hierarchy
Jan 04, 2017 2:59:41 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Constructor:: RequestProcessor instance created!
Constructor:: RequestProcessor instance created!
Request ID : 1212
Exception in thread "main" java.lang.AbstractMethodError: com.injection.testing.RequestProcessor.createValidator()Lcom/injection/testing/RequestValidator;
    at com.injection.testing.RequestProcessor.handleRequest(RequestProcessor.java:12)
    at com.injection.testing.MainDemo.main(MainDemo.java:13)

spring.xml

<!-- Lookup way  -->
    <bean id="requestProcessor" class="com.injection.testing.RequestProcessor" >
        <lookup-method name="getValidator" bean="validator" />
    </bean>

    <bean id="validator" class="com.injection.testing.RequestValidator" scope="prototype" />

RequestProcessor.java

public abstract class RequestProcessor {
    private RequestValidator validator;

    public RequestProcessor(){
        System.out.println("Constructor:: RequestProcessor instance created!");
    }

    public void handleRequest(String requestId){
        System.out.println("Request ID : "+ requestId);
        RequestValidator validator = createValidator(); //here Spring will create new instance of prototype bean
        validator.validate(requestId);
    }

    public RequestValidator getValidator() {
        return validator;
    }

    public void setValidator(RequestValidator validator) {
        this.validator= validator;
    }

    protected abstract RequestValidator createValidator();
}

RequestValidator.java

public class RequestValidator {
    private List<String> errorMessages = new ArrayList<String>();

    public RequestValidator() {
        System.out.println("Constructor:: RequestValidator instance created!");
    }

    // Validates the request and populates error messages
    public void validate(String requestId){
        System.out.println("RequestValidator :"+requestId);
    }

    public List<String> getErrorMessages() {
        return errorMessages;
    }
}

MainDemo.java

public class MainDemo {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

        //RequestValidator requestValidator = (RequestValidator) context.getBean("validator");

        RequestProcessor processor = (RequestProcessor) context.getBean("requestProcessor");
        processor.handleRequest("1212");
        System.out.println("------------------------");
        processor.handleRequest("1213");
    }
}

pom.xml

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>3.2.4</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
0
source share
1 answer

Update spring.xmlas below

<bean id="requestProcessor" class="com.injection.testing.RequestProcessor">
    <lookup-method name="createValidator" bean="validator" />
</bean>

Basically you need to specify the name of the method name abstractin the lookup-methodxml attribute .

I got below result after corrected configuration

04, 2017 4:11:20 PM org.springframework.context.support.ClassPamlApplicationContext prepareRefresh INFO: org.springframework.context.support.ClassPamlApplicationContext@13c675d: [Wed Jan 04 16:11:20 IST 2017]; 04, 2017 4:11:20 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: XML bean [spring.xml]
Constructor:: RequestProcessor!
ID : 1212
:: RequestValidator!
RequestValidator: 1212
ID : 1213
:: RequestValidator!
RequestValidator: 1213

+1

Source: https://habr.com/ru/post/1665690/


All Articles