如何在Wicket 1.5中使用Guice Persist(Guice 3.0)?

时间:2011-10-26 08:56:58

标签: jpa wicket guice guice-persist

我想了解如何在Wicket 1.5中使用Guice Persist(Guice 3.0)。

我无法找到任何'hello world'类型的例子来解释如何做到这一点,如果你可以链接/提供这样一个很好的例子,并乐意接受作为答案。

与此同时,我将尝试自己创建一个“hello world”类型示例,在此处发布代码。帮助我使代码正常运行也将被接受为答案。


我已经设置了一个简单的wicket项目,非常类似于Wicket Examples中的'hello world'guice example,它使用guice进行依赖注入。我现在想扩展这个项目也使用JPA和Guice Persist,而不是“Hello World”我想从数据库中获取用户并显示其用户名。我正在尝试使用Guice wiki关于Guice persist的说明来实现这一点。

更新:所以,我有点工作了。在WebApplication.init()中,我注入了一个类似于此getComponentInstantiationListeners().add(new GuiceComponentInjector(this, new MyServletModule()));的ServetModule,并且在wickets过滤之前,我还在web.xml文件的顶部添加了GuiceFilter。

现在,当我运行应用程序时,一切正常,但我收到有关使用弃用方法的警告。将进一步研究。

  

警告:您正在尝试使用已弃用的API(具体而言,   试图在一个热切创建的@Inject ServletContext中   单身。虽然我们允许此向后兼容性,但请注意   如果你有多个,这可能会有意想不到的行为   注入器(与ServletModule)在同一个JVM中运行。请咨询   Guice文档在   http://code.google.com/p/google-guice/wiki/Servlets了解更多信息   信息。

目录树

.
├── pom.xml
└── src
    └── main
        ├── java
        │   └── se
        │       └── lil
        │           ├── HomePage.html
        │           ├── HomePage.java
        │           ├── MyServletModule.java
        │           ├── WicketApplication.java
        │           ├── domain
        │           │   └── User.java
        │           └── service
        │               ├── IService.java
        │               └── JpaService.java
        ├── resources
        │   ├── META-INF
        │   │   └── persistence.xml
        │   └── log4j.properties
        └── webapp
            └── WEB-INF
                └── web.xml

WicketApplication.java

public class WicketApplication extends WebApplication {
    @Override
    protected void init() {
        super.init();
        getComponentInstantiationListeners().add(new GuiceComponentInjector(this,
                new MyServletModule()));
    }

    @Override
    public Class<? extends Page> getHomePage() {
        return se.lil.HomePage.class;
    }
}

HomePage.java

public class HomePage extends WebPage {
    private static final long serialVersionUID = -918138816287955837L;

    @Inject
    private IService service;

    private IModel<User> model = new LoadableDetachableModel<User>() {
        private static final long serialVersionUID = 1913317225318224531L;

        @Override
        protected User load() {
            return service.getUser();
        }
    };

    public HomePage() {
        setDefaultModel(new CompoundPropertyModel<User>(model));
        add(new Label("name"));
    }
}

HomePage.html

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:wicket="http://wicket.apache.org">
<head>
<title>Wicket Examples - guice</title>
</head>
<body>
    <hr />
        Value: <b wicket:id="name">name goes here</b> <br />
    <hr />
</body>
</html>

MyServletModule.java

public class MyServletModule extends ServletModule {
    protected void configureServlets() {
        install(new JpaPersistModule("manager1"));
        filter("/*").through(PersistFilter.class);
    }
}

IService.java

@ImplementedBy(JpaService.class)
public interface IService {
    public User getUser();
}

JpaService.java

public class JpaService implements IService {
    @Inject
    private EntityManager em;

    @Override
    @Transactional
    public User getUser() {
        Query q = em.createQuery("FROM User");
        q.setMaxResults(1);
        User u = (User) q.getSingleResult();
        return u;
    }
}

User.java

@Entity
@Table (name = "users")
public class User {
    private Long id;
    private String name;

    public User() {
    }

    public User(String name) {
        this.name = name;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }
    @SuppressWarnings("unused")
    private void setId(Long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }   
}

的web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <display-name>wicketwithguice</display-name>

    <filter>
        <filter-name>guiceFilter</filter-name>
        <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>guiceFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>wicket.wicketwithguice</filter-name>
        <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
        <init-param>
            <param-name>applicationClassName</param-name>
            <param-value>se.lil.WicketApplication</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>wicket.wicketwithguice</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

的persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
    <persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>se.lil.domain.User</class> 
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="validate"/>
            <property name="hibernate.show_sql" value="true"/>

            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/test"/>
            <property name="javax.persistence.jdbc.user" value="test"/>
            <property name="javax.persistence.jdbc.password" value="1234"/>
        </properties>
    </persistence-unit>
</persistence>

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>se.lil</groupId>
    <artifactId>wicketwithquice</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <!-- TODO project name -->
    <name>quickstart</name>
    <description></description>
    <!-- TODO <organization> <name>company name</name> <url>company url</url> </organization> -->

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <hibernate-core.version>3.6.4.Final</hibernate-core.version>
        <mysql-connector-java.version>5.1.16</mysql-connector-java.version>
        <slf4j.version>1.6.1</slf4j.version>
        <log4j.version>1.6.1</log4j.version>
        <guice.version>3.0</guice.version>
        <wicket.version>1.5.2</wicket.version>
    </properties>

    <dependencies>
        <!--GUICE DEPENDENCIES -->
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>${guice.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject.extensions</groupId>
            <artifactId>guice-servlet</artifactId>
            <version>${guice.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject.extensions</groupId>
            <artifactId>guice-persist</artifactId>
            <version>${guice.version}</version>
        </dependency>
        <!-- HIBERNATE DEPENDENCIES -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate-core.version}</version>
        </dependency>
        <!-- MYSQL DEPENDENCIES -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector-java.version}</version>
        </dependency>
        <!-- WICKET DEPENDENCIES -->
        <dependency>
            <groupId>org.apache.wicket</groupId>
            <artifactId>wicket-core</artifactId>
            <version>${wicket.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.wicket</groupId>
            <artifactId>wicket-guice</artifactId>
            <version>${wicket.version}</version>
        </dependency>

        <!-- LOGGING DEPENDENCIES - LOG4J -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${log4j.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <optimize>true</optimize>
                    <debug>true</debug>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>Apache Nexus</id>
            <url>https://repository.apache.org/content/repositories/snapshots/</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

4 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

Wicket 1.5.3和Guice 3工作。对于wicket wicket-ioc,除了wicket核心和wicket-guice之外还使用了wicket-util和wicket-dev-utils.Jars作为Netbeans7.0中的库被下载并创建并添加到web项目中。

答案 2 :(得分:0)

当guice与shiro集成时,由于学习了相同的消息,以下邮件列表可能会有用 “http://mail-archives.apache.org/mod_mbox/shiro-user/201108.mbox/%3C4E454692.8090806@peachjean.com%3E”

答案 3 :(得分:0)

当从ServletContext获取Injector并将其添加到GuiceComponentInjector时,不会出现此类消息。此处提供的AppListener和其他代码:

package cookbook;

import com.google.inject.persist.jpa.*;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.google.inject.persist.PersistFilter;
public class AppListener extends GuiceServletContextListener{
    @Override
    public Injector getInjector() {
    return Guice.createInjector(
      new  JpaPersistModule("WickGui3PU"  ),
      new ServletModule() {
        @Override
        protected void configureServlets() {



          filter("/*").through(PersistFilter.class);


          bind(BookDAO.class).to (BookDAOImpl.class);
        }
    });
    } 
}

添加web.xml

<listener>
      <listener-class>cookbook.AppListener</listener-class>
  </listener>

WicketApplication中的init()应该如下:

 protected void init() {
        super.init();

                Injector injector = (Injector)getServletContext().getAttribute(Injector.class.getName());

        getComponentInstantiationListeners().add(new GuiceComponentInjector(this,injector));
    }