You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by myrz <my...@gmail.com> on 2012/04/29 23:58:19 UTC

How to override annotation @SpringBean

Hi everybody.

First I would like to thanks you for the help you gave me.

But I've got another question for you.
I would like to know if it's possible to override the annotation @SpringBean
?
Why do I need to do that?

Let try a little simple example:

I want to print this

"Hello Internal"

and in another page (or ajax panel) 

"Hello Commercial"


But I would like to use the same panel and not have to do a lots of "if
else" in my code. So here my panel code:

Panel.java

public class MyPanel extends CustomPanel{

  private transient HelloService service;
 
  public void initService(){
       service =
ServiceFactory.get("helloService",getCommercialOrInternalState());
  }

  public MyPanel(String id){
      initService();
      IModel model = new LoadableDetachableModel(){
           load(){
              service.sayHello();
           }
      };

      add(new Label("label",model));

  }


My services:

public interface HelloService{
    public String sayHello();
}

public HelloServiceInternalImpl implements HelloService{
    public String sayHello(){
        return "Hello Internal";
    }
}

public HelloServiceCommercialImpl implements HelloService{
    public String sayHello(){
        return "Hello Commercial";
    }
}

In my applicationContext.xml file
<bean id="helloServiceInternal" class="service.HelloServiceInternalImpl  /">
<bean id="helloServiceCommercial" class="service.HelloServiceCommercialImpl 
/">


Well, this way to do can work but sometime I don't know why i've some
NullpointerException then I'm asking myself if it's a good practice create
the service in the constructor.
And I think it's would be more elegant to write something like that:

@CustomSpringBean
private HelloService service;

If someone have an idea he would be awesome to share it.



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/How-to-override-annotation-SpringBean-tp4597030p4597030.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How to override annotation @SpringBean

Posted by Jürgen Lind <Ju...@iteratec.de>.
Hi,

in case like this where I would like to reuse a custom component in different contexts, I
usually make the component abstract and have the using components implement an
abstract method such as "getService". This way, I can pass in different beans
as needed...

Maybe that helps,

Jürgen


On 29.04.2012 23:58, myrz wrote:
> Hi everybody.
>
> First I would like to thanks you for the help you gave me.
>
> But I've got another question for you.
> I would like to know if it's possible to override the annotation @SpringBean
> ?
> Why do I need to do that?
>
> Let try a little simple example:
>
> I want to print this
>
> "Hello Internal"
>
> and in another page (or ajax panel)
>
> "Hello Commercial"
>
>
> But I would like to use the same panel and not have to do a lots of "if
> else" in my code. So here my panel code:
>
> Panel.java
>
> public class MyPanel extends CustomPanel{
>
>    private transient HelloService service;
>
>    public void initService(){
>         service =
> ServiceFactory.get("helloService",getCommercialOrInternalState());
>    }
>
>    public MyPanel(String id){
>        initService();
>        IModel model = new LoadableDetachableModel(){
>             load(){
>                service.sayHello();
>             }
>        };
>
>        add(new Label("label",model));
>
>    }
>
>
> My services:
>
> public interface HelloService{
>      public String sayHello();
> }
>
> public HelloServiceInternalImpl implements HelloService{
>      public String sayHello(){
>          return "Hello Internal";
>      }
> }
>
> public HelloServiceCommercialImpl implements HelloService{
>      public String sayHello(){
>          return "Hello Commercial";
>      }
> }
>
> In my applicationContext.xml file
> <bean id="helloServiceInternal" class="service.HelloServiceInternalImpl  /">
> <bean id="helloServiceCommercial" class="service.HelloServiceCommercialImpl
> /">
>
>
> Well, this way to do can work but sometime I don't know why i've some
> NullpointerException then I'm asking myself if it's a good practice create
> the service in the constructor.
> And I think it's would be more elegant to write something like that:
>
> @CustomSpringBean
> private HelloService service;
>
> If someone have an idea he would be awesome to share it.
>
>
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/How-to-override-annotation-SpringBean-tp4597030p4597030.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org