jsug - google guice by jan zarnikov

13

Click here to load reader

Upload: christoph-pickl

Post on 06-May-2015

1.490 views

Category:

Technology


2 download

DESCRIPTION

visit http://jsug.fsinf.at

TRANSCRIPT

Page 1: JSUG - Google Guice by Jan Zarnikov

Google Guice

Depencency injection framework

Page 2: JSUG - Google Guice by Jan Zarnikov

Basic facts

Pure Java no external configuration files (compared to Spring)bindings configuration in modules (Java classes)

Uses annotations and generics

type safeeasy refactoring in your IDErequires Java 5

Open source under Apache License 2.0

Integration with Servlets, Struts2 ans Spring possible

Page 3: JSUG - Google Guice by Jan Zarnikov

Simple example

Simple application with database and JUnit tests

Page 4: JSUG - Google Guice by Jan Zarnikov

Simple example

Class UserController subject of JUnit testsrequires a database

Interface DAO - abstraction of all database operations

Class JdbcDAO - "real" databseClass TestDAO - mockup implementation for JUnit tests

How does UserController get an instance of DAO?

Page 5: JSUG - Google Guice by Jan Zarnikov

UserController

class UserController { private DAO dao; public UserController() { } public void someMethod { List<User> users = dao.getUsers(); .... } }

Page 6: JSUG - Google Guice by Jan Zarnikov

UserController - with Guice

class UserController { private DAO dao; @Inject public UserController(DAO dao) { this.dao = dao; } public void someMethod { List<User> users = dao.getUsers(); .... } }

Page 7: JSUG - Google Guice by Jan Zarnikov

UserController - with Guice (alternative)

class UserController { @Inject private DAO dao; public UserController() { } public void someMethod { List<User> users = dao.getUsers(); .... } }

Page 8: JSUG - Google Guice by Jan Zarnikov

Configuration - JUnit Tests

public class TestModule extends AbstractModule{

public void configure() { bind(DAO.class).to(TestDAO.class); }

}

Page 9: JSUG - Google Guice by Jan Zarnikov

Configuration - Application

public class AppModule extends AbstractModule{

public void configure() { bind(DAO.class).to(JdbcDAO.class);

}

}

Page 10: JSUG - Google Guice by Jan Zarnikov

Bind to ...

bind(DAO.class).to(TestDAO.class);

bind(DAO.class).to(TestDAO.class) .in(Scopes.SINGLETON);

bind(DAO.class).toInstance(new TestDAO());

bind(DAO.class).toProvider(DAOFactory.class);

Default implementation: @ImplementedBy

Page 11: JSUG - Google Guice by Jan Zarnikov

MyApplication

public static void main(String[] args) { ... Injector injector = Guice.createInjector(new AppModule());

UserController userController = injector. getInstance(UserController.class); }

Page 12: JSUG - Google Guice by Jan Zarnikov

JUnit Test

@Before public void setup() { Injector injector = Guice.createInjector(new TestModule());

UserController userController = injector. getInstance(UserController.class); }

Page 13: JSUG - Google Guice by Jan Zarnikov

Much more...

Annotated with:@Injectpublic SomeConstructor(@Blue DAO dao); ...bind(DAO.class). annotatedWith(Blue.class). to(....);

Annotations with attributes

ServletScopes.SESSION bzw. REQUEST

SringIntegration