Wicket1.5.3 の change Log から、番号 WICKET-4149 の wicket-ioc 機能追加を発見!
コンポーネントだけでなく、ビヘビアの中もインジェクトを可能にする?
前に、JSR-330 未対応について、自分で以下を書いていた。。
http://blog.zaq.ne.jp/oboe2uran/article/507/
http://blog.zaq.ne.jp/oboe2uran/article/531/
それなら、ここで JSR-330 対応として書いていた オリジナルの GuiceComponentInjector
を参考に作った CustomGuiceComponentInjector を wicket-1.5.3 に合わせて以下のようにする。
import org.apache.wicket.Application;
import org.apache.wicket.Component;
import org.apache.wicket.IBehaviorInstantiationListener;
import org.apache.wicket.application.IComponentInstantiationListener;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.guice.GuiceInjectorHolder;
import org.apache.wicket.injection.IFieldValueFactory;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Stage;
/**
* CustomGuiceComponentInjector. for JSR-330 and guice-Inject
* Injects field members of components using Guice.
*/
public class CustomGuiceComponentInjector extends org.apache.wicket.injection.Injector
implements IComponentInstantiationListener,IBehaviorInstantiationListener{
private final IFieldValueFactory fieldValueFactory;
/**
* Creates a new Wicket CustomGuiceComponentInjector instance.
*/
public CustomGuiceComponentInjector(final Application app){
this(app, new Module[0]);
}
/**
* Creates a new Wicket CustomGuiceComponentInjector instance, using the supplied Guice {@link Module}
* instances to create a new Guice {@link Injector} instance internally.
*/
public CustomGuiceComponentInjector(final Application app, final Module... modules){
this(app, Guice.createInjector(app.usesDeploymentConfig() ? Stage.PRODUCTION : Stage.DEVELOPMENT, modules), true);
}
/**
* Constructor
*/
public CustomGuiceComponentInjector(final Application app, final Injector injector){
this(app, injector, true);
}
/**
* Creates a new Wicket CustomGuiceComponentInjector instance, using the provided Guice
* {@link Injector} instance.
*/
public CustomGuiceComponentInjector(final Application app, final Injector injector,final boolean wrapInProxies){
app.setMetaData(GuiceInjectorHolder.INJECTOR_KEY, new GuiceInjectorHolder(injector));
this.fieldValueFactory = new CustomGuiceFieldValueFactory(wrapInProxies);
app.getBehaviorInstantiationListeners().add(this);
bind(app);
}
@Override
public void inject(final Object object){
inject(object,this.fieldValueFactory);
}
@Override
public void onInstantiation(final Component component){
inject(component);
}
@Override
public void onInstantiation(Behavior behavior){
inject(behavior);
}
}
------------------------------------
この中の、CustomGuiceFieldValueFactory については、
http://blog.zaq.ne.jp/oboe2uran/article/531/
で書いたとおりで変更の必要はない。