Hi,
The problem is you are attempting to inject a per-request thing onto
the field of a singleton. When injecting the scope of what you are
injecting on to needs to the the same as or a narrow scope of the
thing that is being injected.
The reason why it is possible to inject HttpServletRequest is there is
a singleton injectable provider that injects a thread local proxy
where instances of HttpServletRequest are stored.
I think what we need to support is the following on your
TransactionProvider:
@Context Injectable<Connection> iConnection.
and then you can call:
Connection c = iConnection.getValue(); in your
TransactionProvider.getValue();
If you would like to me develop that solution please log an issue.
The alternative is to utilize Guice, which provides much better/
cleaner support for DI, where as for Jersey I just did what was
necessary to get something working for its requirements as it could
not leverage a DI framework for implementation at the time.
Hope this helps,
Paul.
On Jun 16, 2009, at 4:25 PM, tatsuya uesugi wrote:
> Hi! i have a trouble with inject one object, i want to know how to
> inject one object and this object depend on another object which
> also injected. maybe you don't understand the problem,i will give
> the example.
> there is one class named ConnectionProvider. that extends the
> PerRequestTypeInjectableProvider
> public class ConnectionProvider extends
> PerRequestTypeInjectableProvider<Context, Connection> {
>
> @Context HttpServletRequest httpServletRequest;
> public ConnectionProvider() {
> super(Connection.class);
> }
> @Override
> public Injectable<Connection> getInjectable(ComponentContext cc,
> Context context) {
> .....
> and there is another class named TransactionProvider which also
> extends the PerRequestTypeInjectableProvider class:
> @Provider
> public class TransactionProvider extends
> PerRequestTypeInjectableProvider<Context, TransactionManager> {
> /** HTTP Servlet Request. */
> @Context
> HttpServletRequest httpServletRequest;
> // @Context
> // Connection connection;
> /** Count of send event when get transaction failure. */
> private static int gettransactionFailCount = 0;
> /** Count of get session failure. */
> public static boolean firstSendGetSessionFailEvent = true;
>
> /** Count of send event when get connection failure. */
> private static int getConnFailCount = 0;
> /** Count of get connection pool failure. */
> public static boolean firstSendGetConnPoolFailEvent = true;
>
> public TransactionProvider() {
> super(TransactionManager.class);
> }
> @Override
> public Injectable<TransactionManager>
> getInjectable(ComponentContext cc, Context arg1) {
> return new Injectable<TransactionManager>() {
> public TransactionManager getValue() {
> if(conn == null){
> Debugger.error("[beaf]
> TransactionProvider.getInjectable() *** can't get connection.");
> throw new WebApplicationException(500);
> }
> .......
> this TransactionProvider use the connection which is another
> provider,and i can not get the connection by the inject,
> i want to know how to get the connection by inject in this
> TransactionProvider class.
> thank you !!
>