Contenu connexe Similaire à Enterprise Guice 20090217 Bejug Similaire à Enterprise Guice 20090217 Bejug (20) Enterprise Guice 20090217 Bejug5. class GothamCity {
@Inject GothamCity(Hero hero) {
...
}
}
class HeroModule implements Module {
public void configure(Binder binder) {
binder.bind(Hero.class).to(Batman.class);
}
}
14. Java Virtual Machine
Web Container
Servlet
Filter pipeline
HTTP Request
HTTP Request Servlet
HTTP Request
Servlet
15. Java Virtual Machine
Web Container
Servlet
HTTP Request GuiceFilter Filter pipeline
HTTP Request Servlet
HTTP Request
Servlet
24. Properties
database.url = jdbc:mysql://localhost/test
database.driver = com.mysql.jdbc.Driver
database.user = test
database.password = test
25. class PropertiesModule extends AbstractModule {
protected void configure() {
Names.bindProperties(binder(),
load(“database.properties”));
}
}
class UsesDatabase {
@Inject @Named(“database.host”)
private String host;
}
27. Compile-time Smiling
class AppModule extends AbstractModule {
protected void configure() {
install(new DataModule());
install(new WebModule());
}
}
<beans>
<import resource=“data-context.xml”/>
<import resource=“web-context.xml”/>
</beans>
36. class PersistenceModule extends ServletModule {
protected void configureServlets() {
install(PersistenceService.usingHibernate()
.across(UnitOfWork.REQUEST)
.buildModule());
bind(Configuration.class).toInstance(
new AnnotationConfiguration()
.addAnnotatedClass(Product.class)
.setProperties(load(quot;hibernate.propertiesquot;))
);
filter(“/*”).through(PersistenceFilter.class);
}
}
Guice.createInjector(new PersistenceModule());
37. # configure hibernate to use the HSQL in-memory database
hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.url=jdbc:hsqldb:mem
hibernate.connection.username=sa
hibernate.connection.password=
hibernate.connection.pool_size=1
hibernate.dialect=org.hibernate.dialect.HSQLDialect
# creates tables for us automatically
hibernate.hbm2ddl.auto=create
# let Warp Persist apply the open-session-in-view pattern
hibernate.current_session_context_class=managed
# don’t use 2nd level caching
hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
38. public class ProductManager {
@Inject Provider<Session> session;
@Transactional
public void create(Product p) {
session.get().save(p);
}
}
39. public interface ProductAccess {
@Transactional
@Finder(namedQuery=“all products”)
List<Product> listAll();
@Transactional
@Finder(query=“from Product where pn = :name”)
Product find(@Named(“name”) String name);
}
40. @Transactional(rollbackOn = IOException.class,
exceptOn = FileNotFoundException.class)
public class ProductService {
@Inject Provider<Session> session;
public void process(Product p) throws IOException {
session.get().save(p);
// do IO stuff
}
}
43. public class ProductManager {
@Inject @Product
Provider<Session> session;
@Transactional(unit=Product.class)
public void create(Product p) {
session.get().save(p);
}
}
44. class PersistenceModule extends ServletModule {
protected void configureServlets() {
HibernatePersistenceStrategy hibernate =
HibernatePersistenceStrategy.builder()
.annotatedWith(Product.class).build();
// Connect to the product database
PersistenceService.using(hibernate)
.across(UnitOfWork.TRANSACTION)
.buildModule();
bind(Configuration.class).annotatedWith(Product.class).toInstance(
new AnnotationConfiguration()
.addAnnotatedClass(Product.class)
.setProperties(load(quot;hibernate.propertiesquot;))
);
...
}
}
52. No Reflection
MyType.class
klazz.getAnnotation(...)
Class.forName(...)
53. GIN
is
Statically Compiled Guice