Actframework依赖注入 II - 注入对象类型

本篇讲述Actframework依赖注入的对象类型

1. 框架内置绑定

在ActFramework中有大量的服务和组件都可以直接使用依赖注入,其中包括

  • ActionContext - Encapsulate all data/info relevant to an HTTP request context
  • H.Session - HTTP request session. Also available via actionContext.session()
  • H.Flash - HTTP request flash. Also available via actionContext.flash()
  • H.Request - HTTP request. Also available via actionContext.req()
  • H.Response - HTTP response. Also available via actionContext.resp()
  • CliContext - Encapsulate all data/facilities relevant to a CLI session
  • CliSession (Since act-0.6.0) - CLI session
  • MailerContext - Mailer method context
  • ActContext - A generic ActContext depends on the current computation environment, could be either ActionContext, CliContext or MailerContext or null
  • Logger - The Act.LOGGER instance
  • UserAgent - The user agent if in a request handling context
  • AppConfig - The application configuration object
  • AppCrypto - The application crypto object
  • CacheService - The App.cache() cache service
  • EventBus - The application’s event bus
  • Locale - Could be ActContext.locale() or AppConfig.locale() if there is no context

1.1 应用服务组件

  • DbServiceManager
  • MailerService
  • Router
  • CliDispatcher
  • AppJobManager

2. Dao

目前支持EbeanDaoMorphiaDao两种分别用于访问SQL和MongoDB数据库

  1. // Demonstrate inject to field
  2. @Controller("user")
  3. public class UserService {
  4. @javax.inject.Inject
  5. private MorphiaDao<User> userDao;
  6. @PostAction
  7. public void create(User user) {
  8. userDao.save(user);
  9. }
  10. }
  1. // Demonstrate inject to parameter
  2. @Controller("user")
  3. public class UserService {
  4. @PostAction
  5. public void create(User user, MorphiaDao<User> userDao) {
  6. userDao.save(user);
  7. }
  8. }

如果应用有自定义的Dao,可以直接注入:

  1. // The Model
  2. @Entity("user")
  3. public class User extends MorphiaModel<User> {
  4. public String email;
  5. ...
  6. public static class Dao extends MorphiaDao<User> {
  7. public User findByEmail(String email) {
  8. return findOneBy("email", email);
  9. }
  10. }
  11. }
  1. // The controller
  2. @Controller("user")
  3. public class UserService {
  4. @javax.inject.Inject
  5. private User.Dao userDao;
  6. @GetAction("{email}")
  7. public User findByEmail(String email) {
  8. return userDao.findByEmail(email);
  9. }
  10. }

3. 可构造对象

任何拥有public缺省构造函数或者带有@Inject构造函数的类均可被注入, 例如:

  1. // A class with public default constructor
  2. public class Foo {
  3. public Foo() {...}
  4. }
  1. // A class with Inject constructor
  2. public class Bar {
  3. @javax.inject.Inject
  4. public Bar(Foo foo) {...}
  5. }

上面的FooBar都可以用于依赖注入:

  1. public class XxxController {
  2. @Inject Foo foo;
  3. @Inject Bar bar;
  4. ...
  5. }

注意 可构造对象不能直接用于参数注入

  1. public class XxxController {
  2. // foo and bar won't be able to injected throght DI
  3. // instead they will be deserialized from form parameters
  4. @PostAction("/xxx")
  5. public void xxxAction(Foo foo, Bar bar) {
  6. }
  7. }

但是可以通过@Provided注解来指定使用依赖注入

  1. public class YyyController {
  2. // this time foo and bar will be injected through DI
  3. @PostAction("/yyy")
  4. public void xxxAction(@Provided Foo foo, @Provided Bar bar) {
  5. }
  6. }

4. 应用自定义的绑定

假设应用自己定义了接口或抽象类, 并且定义了绑定, 可以直接使用依赖注入

  1. // The interface
  2. public interface MyService {
  3. void service();
  4. }
  1. // The implemention one
  2. public class OneService implements MyService {
  3. public void service() {Act.LOGGER.info("ONE is servicing");}
  4. }
  1. // The implemention two
  2. public class TwoService implements MyService {
  3. public void service() {Act.LOGGER.info("TWO is servicing");}
  4. }
  1. // Define bindings
  2. public class MyModule extends org.osgl.inject.Module {
  3. protected void configure() {
  4. bind(MyService.class).to(OneService.class);
  5. bind(MyService.class).named("two").to(TwoService.class);
  6. }
  7. }
  1. // Inject the service
  2. public class Serviced {
  3. @Inject
  4. private MyService one;
  5. @Inject
  6. @Named("two")
  7. private MyService two;
  8. }

链接