область действия bean с прототипом создает исключение BeanInstantiationException - PullRequest
0 голосов
/ 08 октября 2018

У меня есть класс, определенный ниже

public class Caller {

    private GreetingService greetingService;
    private String name;

    public Caller(GreetingService greetingService) {
        System.out.println("Inside the constructor of Caller");
        this.greetingService = greetingService;

    }

    public void invokeGreet(String message) {
        greetingService.greet(message, "Pradeep");
    }

    public void myCustomInit() {
        System.out.println("Came inside the custom initializer method");
    }

    public void myCustomDestroy() {
        System.out.println("Came inside the custom destroy function..");
    }
}

GreetingService - это интерфейс

public interface GreetingService {


    public void greet(String message, String name);
}

Класс AwesomeGreet:

public class AwesomeGreet implements GreetingService {

    private String salutation;


    public String getSalutation() {
        return salutation;
    }


    public void setSalutation(String salutation) {
        this.salutation = salutation;
    }


    public void greet(String message, String name) {
        System.out.println("Hello !! "+salutation+" " +name +" "+message);
    }

}

Мой application-context.xmlФайл выглядит следующим образом:

<bean id="caller" class="com.classpath.spring.Caller" scope="prototype">
        <constructor-arg name="greetingService"
             ref="awesomeGreet" />
    </bean>
    <bean id="awesomeGreet" class="com.classpath.spring.AwesomeGreet" scope="prototype">
        <property name="salutation" value="Mr"></property>
    </bean>

Когда я пытаюсь загрузить application-context, я получаю сообщение об ошибке ниже:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.classpath.spring.Caller]: Illegal arguments for constructor; nested exception is java.lang.IllegalArgumentException: argument type mismatch
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:181)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:299)
    ... 12 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:172)
    ... 14 more

Я не уверен, где я иду не так.Тот же пример отлично работает, когда для области установлен Singleton.

1 Ответ

0 голосов
/ 08 октября 2018

Я думаю, определение бина должно иметь class="com.classpath.spring.GreetingService"

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...