johnny-b-goode
johnny-b-goode

Reputation: 3893

Unable to instantiate bean

I am trying to implement a simple web service using spring. Following bean declaration has been added to app-ctx.xml:

   <bean class="com.user.services.MessageService_BindingImpl" scope="request"/>

Everything works perfectly. After that i decide to try to use injection through a constructor - i've added a simple constructor with one argument (String type) and i've modified bean confoguration:

  <bean class="com.user.services.MessageService_BindingImpl" scope="request">
  <constructor-arg type="java.lang.String" value="Hello world"/></bean>

And after that i got following exception:

java.lang.InstantiationException: com.user.services.MessageService_BindingImpl

Looks like there is something with my constructor. After adding default non-arg constructor, exception disappears. How to use overloaded constructor? Thanks.

MessageService_BindingImpl -

public class MessageService_BindingImpl implements com.user.service.MessageService_PortType {

   public MessageService_BindingImpl (String hello) {
   }

   public ReadMessagesResponse readMessages(ReadMessagesRequest readMessagesRequest) throws RemoteException {
      MessageService mService = new MessageService();
      return mService.readmessages();
   }
}

Upvotes: 0

Views: 650

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279960

Maybe I misunderstood your question.

Are you asking why

<bean class="com.user.services.MessageService_BindingImpl" scope="request"/>

is failing with a class like

public class MessageService_BindingImpl implements com.user.service.MessageService_PortType {

   public MessageService_BindingImpl (String hello) {
   }

   ...
}

?

If so, then the answer is that by not providing any constructor-arg, Spring will try to use your class' no-argument constructor. Since you don't have one, it can't use it.

Upvotes: 1

Related Questions