Weird. Has that always been true? For some reason I've had it in my head
that services weren't available for field injection.... I'm also confused
why @Inject would work with Logger in a Component, but not in a service.


On Tue, Dec 28, 2010 at 1:31 PM, Igor Drobiazko <igor.drobia...@gmail.com>wrote:

> Injection into services is not limited to constructors. It is absolutely ok
> to annotate fields with @Inject. The issue here is that Logger is not a
> typical service. For Logger one should use @InjectResource annotation. Same
> applies for configurations.
>
> So following should work:
>
> public class UserServiceImpl implements UserService {
>
>    @InjectResource
>    private Logger logger;
>
>   @Inject
>   private RandomNumberGenerator randomNumber;
> }
>
> On Tue, Dec 28, 2010 at 10:19 PM, Josh Canfield <joshcanfi...@gmail.com
> >wrote:
>
> > Services do constructor injection, not @Inject. So create a constructor
> > that
> > takes your your services as parameters and you're golden.
> >
> >
> > public class UserServiceImpl implements UserService {
> >
> >    private Logger logger;
> >
> >   private RandomNumberGenerator randomNumber;
> >
> >   public UserServiceImpl(RandomNumberGenerator rn, Logger logger) {
> >      this.logger = logger;
> >      randomNumber = rn;
> >   }
> > }
> >
> >
> > On Tue, Dec 28, 2010 at 1:01 PM, Robert Hannebauer <
> rob...@hannebauer.org
> > >wrote:
> >
> > > I'm having problems with Tapestry 5.2.4 and injecting a Logger into a
> > > service and a component.
> > >
> > > My AppModule:
> > >    public static void bind(ServiceBinder binder) {
> > >        binder.bind(RandomNumberGenerator.class,
> > > SecureRandomNumberGenerator.class);
> > >        binder.bind(UserService.class, UserServiceImpl.class);
> > >    }
> > >
> > > My component class
> > > public class Layout
> > > {
> > >    @Inject
> > >    private Logger logger;
> > > }
> > >
> > > works as expected, but my service class throws an exception.
> > >
> > > public class UserServiceImpl implements UserService {
> > >
> > >    @Inject
> > >    private Logger logger;
> > >
> > >    @Inject
> > >    private RandomNumberGenerator randomNumber;
> > > }
> > >
> > > The injection of the RandomNumberGenerator also works as expected, but
> > not
> > > the logger:
> > >
> > > [DEBUG] AppModule.UserService Loading class
> > > test.services.impl.UserServiceImpl.
> > > [DEBUG] AppModule.UserService Marking class
> > > test.services.impl.UserServiceImpl to be (re-)loaded
> > > [DEBUG] AppModule.UserService BEGIN Analyzing
> > > test.services.impl.UserServiceImpl
> > > [DEBUG] AppModule.UserService   END Analyzing
> > > test.services.impl.UserServiceImpl
> > > [DEBUG] AppModule.UserService Invoking constructor public
> > > test.services.impl.UserServiceImpl().
> > > [ERROR] ioc.Registry No service implements the interface
> > org.slf4j.Logger.
> > > [ERROR] ioc.Registry Operations trace:
> > > [ERROR] ioc.Registry [ 1] Realizing service RegistryStartup
> > > [ERROR] ioc.Registry [ 2] Invoking
> > > org.apache.tapestry5.ioc.internal.services.RegistryStartup(Logger,
> List)
> > (at
> > > RegistryStartup.java:36) via
> > > org.apache.tapestry5.ioc.services.TapestryIOCModule.bind(ServiceBinder)
> > (at
> > > TapestryIOCModule.java:55)
> > > [ERROR] ioc.Registry [ 3] Determining injection value for parameter #2
> > > (java.util.List)
> > > [ERROR] ioc.Registry [ 4] Collecting ordered configuration for service
> > > RegistryStartup
> > > [ERROR] ioc.Registry [ 5] Invoking method
> > > test.services.AppModule.initApplication(UserService, JdbcTemplate,
> > Logger)
> > > (at AppModule.java:174).
> > > [ERROR] ioc.Registry [ 6] Reloading class
> > > test.services.impl.UserServiceImpl.
> > > [ERROR] ioc.Registry [ 7] Calculating injection value for field
> 'logger'
> > > (org.slf4j.Logger)
> > > [ERROR] ioc.Registry [ 8] Resolving object of type org.slf4j.Logger
> using
> > > MasterObjectProvider
> > >
> > > Caused by: org.apache.tapestry5.ioc.internal.OperationException: No
> > service
> > > implements the interface org.slf4j.Logger.
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.OperationTrackerImpl.logAndRethrow(OperationTrackerImpl.java:102)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:69)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.PerThreadOperationTracker.invoke(PerThreadOperationTracker.java:68)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.RegistryImpl.invoke(RegistryImpl.java:1063)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.services.MasterObjectProviderImpl.provide(MasterObjectProviderImpl.java:41)
> > >    at
> > >
> >
> $MasterObjectProvider_12d2eb17ffe.provide($MasterObjectProvider_12d2eb17ffe.java)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.RegistryImpl.getObject(RegistryImpl.java:811)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.ObjectLocatorImpl.getObject(ObjectLocatorImpl.java:50)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.util.InternalUtils$4.run(InternalUtils.java:368)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.OperationTrackerImpl$1.invoke(OperationTrackerImpl.java:50)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.OperationTrackerImpl$1.invoke(OperationTrackerImpl.java:47)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:65)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.OperationTrackerImpl.run(OperationTrackerImpl.java:46)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.PerThreadOperationTracker.run(PerThreadOperationTracker.java:56)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.RegistryImpl.run(RegistryImpl.java:1058)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.util.InternalUtils.injectIntoFields(InternalUtils.java:353)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.ConstructorServiceCreator.createObject(ConstructorServiceCreator.java:64)
> > >    ... 136 more
> > > Caused by: java.lang.RuntimeException: No service implements the
> > interface
> > > org.slf4j.Logger.
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.RegistryImpl.getService(RegistryImpl.java:670)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.ObjectLocatorImpl.getService(ObjectLocatorImpl.java:45)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.services.MasterObjectProviderImpl$1.invoke(MasterObjectProviderImpl.java:56)
> > >    at
> > >
> >
> org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:65)
> > >    ... 151 more
> > >
> > > If i delete the injection of the logger in my UserService everything
> > works
> > > fine.
> > >
> > > Is it not possible to inject a Logger into a service implementation?
> Why?
> > > It looks strange to me to mix Logger instantiation in service
> > implementation
> > > (LoggerFactory.getLogger()) and component (@Inject) classes.
> > >
> > > Regards
> > > Robert
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
> > > For additional commands, e-mail: users-h...@tapestry.apache.org
> > >
> > >
> >
>
>
>
> --
> Best regards,
>
> Igor Drobiazko
> http://tapestry5.de
>

Reply via email to