{"id":16,"date":"2012-03-14T15:27:13","date_gmt":"2012-03-14T14:27:13","guid":{"rendered":"https:\/\/overdespotiet.dk\/blog\/?p=16"},"modified":"2024-06-19T13:35:52","modified_gmt":"2024-06-19T11:35:52","slug":"gwtp-guice-and-spring-non-trivial-beans","status":"publish","type":"post","link":"https:\/\/overdespotiet.dk\/blog\/?p=16","title":{"rendered":"GWTP, Guice and Spring non-trivial beans"},"content":{"rendered":"<p>Having struggled a bit with the somewhat terse documentation and JavaDoc I finally figured out the tricks of using Guice and GWTP along with Spring. As much for my own future reference and for others that may have the same issues, here&#8217;s what I did.<\/p>\n<p><!--more-->First we need to make Guice aware of our Spring configuration. For this we need the <em>com.google.inject.extensions:guice-spring<\/em> artifact<\/p>\n<p>Also, it is vitally important that we explicitly autowire the beans apparently&#8230;<\/p>\n<pre class=\"brush: java; gutter: false\">public class ServerModule extends HandlerModule\r\n{\r\n    @Override\r\n    protected void configureHandlers()\r\n    {\r\n        \/\/ Bind spring beans to Guice\r\n        final ClassPathXmlApplicationContext beanFactory = new ClassPathXmlApplicationContext( \"applicationContext.xml\" );\r\n        AutowireCapableBeanFactory factory = beanFactory.getAutowireCapableBeanFactory();\r\n        factory.autowireBeanProperties( this, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT , true );\r\n\r\n        \/\/ Must bind BeanFactory for fromSpring( ... , ... ) to work!\r\n        bind( BeanFactory.class ).toInstance( beanFactory );\r\n        bind( UserDao.class ).toProvider( fromSpring( UserDaoImpl.class, \"userDao\" ) ).in( Singleton.class );\r\n    }\r\n}<\/pre>\n<p>Once we have these bindings, we can go on injecting:<\/p>\n<pre class=\"brush: java; gutter: false\">public class AuthenticateUserHandler extends BaseActionHandler&lt;AuthenticateUserAction,AuthenticateResult&gt;\r\n{\r\n    private UserDao userDao;\r\n\r\n    @Inject\r\n    public AuthenticateUserHandler( UserDao userDao )\r\n    {\r\n        super( AuthenticateUserHandler.class );\r\n        this.userDao = userDao;\r\n    }\r\n\r\n    ...\r\n}<\/pre>\n<p>Voila!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Having struggled a bit with the somewhat terse documentation and JavaDoc I finally figured out the tricks of using Guice and GWTP along with Spring. As much for my own future reference and for others that may have the same &hellip; <a href=\"https:\/\/overdespotiet.dk\/blog\/?p=16\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[17,5,3,4,6],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-coding","tag-dependency-injection","tag-guice","tag-gwt","tag-gwt-platform","tag-spring"],"_links":{"self":[{"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16"}],"version-history":[{"count":16,"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":250,"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions\/250"}],"wp:attachment":[{"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}