Hey Martin,

Thanks. The init-method of MySession looks now like this:
--
        private void init() {
                BeanManager manager = null;
                try {
                        manager = (BeanManager) 
InitialContext.doLookup("java:comp/BeanManager");
                } catch (NamingException e) {
                        e.printStackTrace();
                }

                NonContextual.of(MySession.class, manager).inject(this);
       }
--

I don't now if this is the proper way of injecting into the session. I
don't really get how NonContextual works, but so far, this is how I
understand it: the class-parameter in the 'of'-method defines which
non-contextual classes can be injected in (managed by) the manager,
and inject actually makes the instance managed by the container.

And well... there might be an easier way to get the BeanManager, but
at least, it works now. Should I make BeanManager from MainApp
accessible so MySession can get it from there, or should I leave it
like it?

Thanks again, that was pretty easy,
Kurt


2012/11/25 Martin Grigorov <[email protected]>:
> Hi,
>
> Wicket is a bit inconsistent with its CDI integration.
> It doesn't use wicket-ioc Injector at all.
> You have to use NonContextual.of(...).inject(this) instead.
>
>
> On Sun, Nov 25, 2012 at 4:27 PM, Kurt Sys <[email protected]> wrote:
>
>> Hey all,
>> I'm trying to get cdi/injection to work with wicket. So far,
>> everything seems to be allright except for one thing: injecting into a
>> wicket-websession. Seems logical in some way, since a session is not a
>> component, so I tried using 'Injector.get().inject(this)', but
>> apparently, Injector.get() returns null. Injection into components
>> does work flawlessly (DaoService as well as other components are
>> injected into HomePage.java).
>> What am I missing?
>>
>> Thanks,
>> Kurt
>>
>>
>> Mainapp.java
>> --
>> package mypackage;
>>
>> import javax.enterprise.inject.spi.BeanManager;
>> import javax.naming.InitialContext;
>> import javax.naming.NamingException;
>> import org.apache.wicket.Application;
>> import org.apache.wicket.Session;
>> import org.apache.wicket.cdi.CdiConfiguration;
>> import org.apache.wicket.protocol.http.WebApplication;
>> import org.apache.wicket.request.Request;
>> import org.apache.wicket.request.Response;
>>
>> public class MainApp
>>         extends WebApplication {
>>
>>         public MainApp() {
>>         }
>>
>>         @Override
>>         public void init() {
>>                 super.init();
>>
>>                 BeanManager manager = null;
>>                 try {
>>                         manager = (BeanManager)
>> InitialContext.doLookup("java:comp/BeanManager");
>>                 } catch (NamingException e) {
>>                         e.printStackTrace();
>>                 }
>>                 new CdiConfiguration(manager).configure(this);
>>         }
>>
>>         @Override
>>         public void onDestroy() {
>>         }
>>
>>         public Class<? extends org.apache.wicket.Page> getHomePage() {
>>                 return HomePage.class;
>>         }
>>
>>         @Override
>>         public final Session newSession(Request req, Response res) {
>>                 return new MySession(req);
>>         }
>> }
>> --
>>
>> MySession.java
>> --
>> package mypackage;
>>
>> import javax.inject.Inject;
>> import org.apache.wicket.Session;
>> import org.apache.wicket.injection.Injector;
>> import org.apache.wicket.protocol.http.WebSession;
>> import org.apache.wicket.request.Request;
>>
>> public class MySession
>>         extends WebSession {
>>
>>         @Inject
>>         DaoService daoService;
>>
>>         public static MySession get() {
>>                 return (MySession) Session.get();
>>         }
>>
>>         MySession(Request req) {
>>                 super(req);
>>                 init();
>>         }
>>
>>         private void init() {
>>                 Injector.get().inject(this);
>>         }
>>
>>         private void test() {
>>                 daoService.someMethod();
>>     }
>> }
>> --
>> I added the private void test-method to test if daoService is null.
>> However, there is a nullpointerexception earlier, at line
>> 'Injector.get().inject(this)'. I avoid 'this' in the constructor, so I
>> put it in a init-method. That keeps the code a little cleaner, I
>> think.
>>
>> DaoService.java:
>> --
>> package mypackage;
>>
>> import java.util.List;
>> import javax.ejb.Stateless;
>> import javax.persistence.EntityManager;
>> import javax.persistence.PersistenceContext;
>>
>> @Stateless
>> public class DaoService {
>>
>>         @PersistenceContext(unitName="testPU")
>>         private EntityManager entityManager;
>>
>>         public void someMethod() {
>>         }
>>
>>         [...]
>> }
>> --
>> Injecting persistencecontext into daoservice does work fine.
>>
>> HomePage.java
>> --
>> package mypackage;
>>
>> import javax.inject.Inject;
>> import org.apache.wicket.markup.html.basic.Label
>> import org.apache.wicket.model.PropertyModel;
>>
>> public class HomePage
>>         extends BasePage {
>>
>>         @Inject
>>         DaoService daoService;
>>         @Inject
>>         Clock clock;
>>
>>         public HomePage() {
>>                 add(new Label("time", new PropertyModel(this,
>> "clock.time")));
>>                 daoService.someMethod();
>>         }
>> }
>> --
>> No nullpointerexception or any other problem here.
>>
>> I don't get any issues as long as I don't open the webapp (the
>> application starts fine). Some logs (line numbers are incorrect: line
>> number 28 is the one with 'Injector.get()'; I deleted some comments to
>> keep it a little more readable):
>> --
>> INFO: Server startup in 6005 ms
>> 25-nov-2012 16:00:59 org.apache.wicket.DefaultExceptionMapper internalMap
>> SEVERE: Unexpected error occurred
>> java.lang.NullPointerException
>>         at mypackage.MySession.init(MySession.java:28)
>>         at mypackage.MySession.<init>(MySession.java:24)
>>         at mypackage.MainApp.newSession(MainApp.java:45)
>>         at
>> org.apache.wicket.Application.fetchCreateAndSetSession(Application.java:1557)
>>         at org.apache.wicket.Session.get(Session.java:152)
>>         at org.apache.wicket.Restart
>> [...]
>> 25-nov-2012 16:00:59 org.apache.wicket.DefaultExceptionMapper map
>> SEVERE: unexpected exception when handling another exception: null
>> java.lang.NullPointerException
>>         at mypackage.MySession.init(MySession.java:28)
>>         at mypackage.MySession.<init>(MySession.java:24)
>>         at mypackage.MainApp.newSession(MainApp.java:45)
>>         at
>> org.apache.wicket.Application.fetchCreateAndSetSession(Application.java:1557)
>>         at org.apache.wicket.Session.get(Session.java:152)
>>         at
>> org.apache.wicket.RestartResponseAtInterceptPageException$InterceptData.get(RestartResponseAtInterceptPageException.java:146)
>> [...]
>> 25-nov-2012 16:00:59 org.apache.wicket.DefaultExceptionMapper map
>> SEVERE: unexpected exception when handling another exception: null
>> java.lang.NullPointerException
>>         at mypackage.MySession.init(MySession.java:28)
>>         at mypackage.MySession.<init>(MySession.java:24)
>>         at mypackage.MainApp.newSession(MainApp.java:45)
>>         at
>> org.apache.wicket.Application.fetchCreateAndSetSession(Application.java:1557)
>>         at org.apache.wicket.Session.get(Session.java:152)
>>         at
>> org.apache.wicket.RestartResponseAtInterceptPageException$InterceptData.get(RestartResponseAtInterceptPageException.java:146)
>> --
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>>
>
>
> --
> Martin Grigorov
> jWeekend
> Training, Consulting, Development
> http://jWeekend.com <http://jweekend.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to