PDA

View Full Version : Implementing or extending interfaces



julio
Oct 22nd, 2007, 07:11 AM
Hi,

I have a generic service in my pojo-model with its interface (IMyService) and relative "impl" (MyService) class.

Interface reports "exported" methods for service. This method needs (like others services) of an "init" method.

And so I made "Initialize" interface with "public void init()" only.

If "IMyService" is injected in a bean like this:

<bean id="test"...>
<property name="myService" ref="myService" />
</bean>

where:

<bean id="myService" class="org.myapp.model.MyService" />

and I implements property in bean "test" class like this:

private IMyService myService;

obviously test-bean cannot see "init" method unless "IMyService extends Initialize"

Has it a sense extends interface or I missing anything:)?

Thanks,
Julio

Jörg Heinicke
Oct 23rd, 2007, 12:27 AM
As far as I understand init() is not specific to IMyService and that's why you keep it separated. Letting IMyService extending Initialize seems to break this separation again (why not adding it directly to IMyService then?). The question is if any interface is necessary for calling init(). Spring has some life cycle support (http://static.springframework.org/spring/docs/2.0.x/reference/beans.html#beans-factory-lifecycle-initializingbean) which allows you to configure it to call init() on the bean:



<bean id="myService" class="org.myapp.model.MyService" init-method="init"/>


Joerg

julio
Oct 23rd, 2007, 02:00 AM
Sorry, I was inaccurate on my question.

"init" method is called when client does a specific "post of data". And MyService does calculations around this "post of data". Before start this calculations, it needed to call "init" method. And so I cannot use *init-method="init"*.

And so could be better a "lazy" solution?

Thanks,
Julio

julio
Oct 23rd, 2007, 05:03 AM
ok solved, it was a "design" problem.

I'm working on a portal that interfaces an external java-application.

Incorrectly I was mixing logical code to use that application with my model-portal. Things that are simple "object values" for that java-application, for portal are real "entity" and so potentially requires factory, repository, etc. Adding last things always is more clean and error-prone-init are disappared...

Thanks,
Julio