I want to find out how to use Guice Persist (Guice 3.0) with Wicket 1.5.
I have not been able to find any 'hello world' type examples explaining how to do this, if you can link/provide such an example that would be great, and happily accepted as an answer.
In the meantime I'll be trying to create a 'hello world' type example myself, posting the code here as I progress. Help with making my code function properly will also be accepted as an answer.
I have set up a simple wicket project, very similar to the 'hello world' guice example from Wicket Examples, that uses guice for dependency injection. I now want to extend this project to also use JPA and Guice Persist, instead of "Hello World" I want to fetch a User from the database and display its username. I'm trying to achieve this using the instructions from the Guice wiki about Guice persist.
UPDATE: So, I kinda got it working. In WebApplication.init()
I injected a ServetModule like this getComponentInstantiationListeners().add(new GuiceComponentInjector(this, new MyServletModule()));
and I also added GuiceFilter at the top of the web.xml file, before wickets filter.
Now when I run the application everything works, but I get this warning about using deprecated methods. Will look into this further.
WARNING: You are attempting to use a deprecated API (specifically, attempting to @Inject ServletContext inside an eagerly created singleton. While we allow this for backwards compatibility, be warned that this MAY have unexpected behavior if you have more than one injector (with ServletModule) running in the same JVM. Please consult the Guice documentation at http://code.google.com/p/google-guice/wiki/Servlets for more information.
Directory tree
.
├── 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>
You should have a look on this post and the comments:
http://blog.yanivkessler.com/2010/05/wicket-and-guice-alternate-route.html