【问题标题】:How do I use Guice Persist (Guice 3.0) with Wicket 1.5?如何在 Wicket 1.5 中使用 Guice Persist (Guice 3.0)?
【发布时间】:2011-12-15 13:57:07
【问题描述】:

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

我找不到任何“hello world”类型的示例来解释如何执行此操作,如果您可以链接/提供这样一个很好的示例,并且很高兴作为答案被接受。

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


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

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

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

警告:您正在尝试使用已弃用的 API(具体而言, 试图在急切地创建的内部 @Inject ServletContext 单身人士。虽然我们允许这样做是为了向后兼容,但请注意 如果您有多个,这可能会出现意外行为 在同一个 JVM 中运行的注入器(带有 ServletModule)。请咨询 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>

【问题讨论】:

  • 只看这个而不真正阅读它,我不得不说它可能过于本地化。您能否将您的问题概括为其他人也可能遇到的问题?
  • @dlamblin 考虑到我发布的代码量,我可以看到你来自哪里。我正在寻找的是一个使用 wicket 和 guice persist 的简单“hello world”类型示例。我发布的代码反映了我自己创建这样一个示例的程度。
  • 我会接受指向“hello world”示例的链接作为答案。帮助更正我的代码也很乐意接受,现在唯一的问题是它使用了已弃用的 API。如果我自己让我的代码正常工作,我只会回答我自己的问题:)

标签: jpa wicket guice guice-persist


【解决方案1】:

【讨论】:

    【解决方案2】:

    Wicket 1.5.3 和 Guice 3 工作。对于 wicket wicket-ioc,除了 wicket core 和 wicket-guice 之外,还使用了 wicket-util 和 wicket-dev-utils。在 Netbeans7.0 中下载并创建了一个库并添加到 Web 项目中。

    【讨论】:

      【解决方案3】:

      当 guice 与 shiro 集成时,同样的消息会出现,下面的邮件列表可能有用 "http://mail-archives.apache.org/mod_mbox/shiro-user/201108.mbox/%3C4E454692.8090806@peachjean.com%3E"

      【讨论】:

        【解决方案4】:

        当从 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));
            }
        

        【讨论】:

        • 感谢您的回答,过几天试试。
        猜你喜欢
        • 2012-06-29
        • 2013-11-07
        • 1970-01-01
        • 1970-01-01
        • 2012-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-24
        相关资源
        最近更新 更多