简体   繁体   English

注入不适用于嵌套对象[Jersey 2.22.1]

[英]Inject not working for nested objects[Jersey 2.22.1]

I have a Jersey resource with a facade object injected. 我有一个注入了Facade对象的Jersey资源。 This is configured in my ResourceConfig and the facade gets injected fine. 这是在我的ResourceConfig配置的,并且外观得到了很好的注入。 The facade contains a DAO class which also should be injected and is configured in the same ResourceConfig . 外观包含一个DAO类,该类也应注入并在同一ResourceConfig配置。 Now to my problem; 现在到我的问题; the DAO class is null. DAO类为null。 Thus, not injected. 因此,不注射。

@ApplicationPath("/service")
public class SystemSetup extends ResourceConfig {

public SystemSetup() {
    packages(false, "com.foo.bar");
    packages("org.glassfish.jersey.jackson");
    register(JacksonFeature.class);

    final LockManager manager = getLockManager();
    final SessionFactory sessionFactory = getSessionFactory();
    register(new AbstractBinder() {
        @Override
        protected void configure() {
            bindFactory(InjectFactory.getDaoFactory(sessionFactory)).to(Dao.class).in(Singleton.class);
            bindFactory(InjectFactory.getFacadeFactory(manager)).to(Facade.class).in(Singleton.class);
        }
    });
}

 @Path("/")
 @Produces("text/json")
 public class ViewResource {

    @Inject
    private Facade logic;

public class Facade {

    @Inject
    private Dao dao; //Not injected

The factory instances are rather simple. 工厂实例非常简单。 They simply call the constructor and pass the argument to it. 他们只是调用构造函数并将参数传递给它。

The strange thing is that this worked absolut fine when I used bind(Class object) rather than bindFactory. 奇怪的是,当我使用bind(Class object)而不是bindFactory时,这绝对可以正常工作。

EDIT 编辑

Factories 工厂工厂

class InjectFactory {

    static Factory<Dao> getDaoFactory() {
        return new Factory<Dao>() {
            @Override
            public Dao provide() {
                return new Dao(new Object());
            }

            @Override
            public void dispose(Dao dao) {}
        };
    }

    static Factory<Facade> getFacadeFactory() {
        return new Factory<Facade>() {

            @Override
            public Facade provide() {
                return new Facade();
            }

            @Override
            public void dispose(Facade facade) {}
        };
    }
}

As is the case with most Di frameworks, when you start instantiating things yourself, it's often the case that you are kicking the framework out of the equation. 与大多数Di框架一样,当您自己开始实例化事物时,通常是将框架排除在等式之外。 This holds true for the Factory instances, as well as the objects the factory creates. 这适用于Factory实例以及工厂创建的对象。 So the Facade instance never gets touch by the framework, except to inject it into the resource class. 因此,除了将Facade实例注入资源类之外,它再也不会与框架接触。

You can can a hold of the ServiceLocator , and explicitly inject objects yourself if you want to create them yourself. 您可以持有ServiceLocator ,如果要自己创建对象,则可以自己显式注入对象。 Here are a couple options. 这里有几个选择。

1) Inject the ServiceLocator into the Factory instance, then inject the Facade instance. 1)将ServiceLocator注入Factory实例,然后注入Facade实例。

static Factory<Facade> getFacadeFactory() {
    return new Factory<Facade>() {

        @Context
        ServiceLocator locator;

        @Override
        public Facade provide() {
            Facade facade = new Facade();
            locator.inject(facade);
            return facade;
        }

        @Override
        public void dispose(Facade facade) {}
    };
}

@Inject
public SystemSetup(ServiceLocator locator) {
    packages("foo.bar.rest");
    packages("org.glassfish.jersey.jackson");
    register(JacksonFeature.class);

    register(new AbstractBinder() {
        @Override
        protected void configure() {
            bindFactory(InjectFactory.getDaoFactory()).to(Dao.class);

            Factory<Facade> factory = InjectFactory.getFacadeFactory();
            locator.inject(factory);
            bindFactory(factory).to(Facade.class);
        }
    });
}

2) Or bind a Factory class , and let the framework inject the ServiceLocator 2)或绑定Factory ,然后让框架注入ServiceLocator

public static class FacadeFactory implements Factory<Facade> {

    @Context
    ServiceLocator locator;

    @Override
    public Facade provide() {
        Facade facade = new Facade();
        locator.inject(facade);
        return facade;
    }

    @Override
    public void dispose(Facade facade) {}
}

register(new AbstractBinder() {
    @Override
    protected void configure() {
        bindFactory(InjectFactory.getDaoFactory()).to(Dao.class);
        bindFactory(InjectFactory.FacadeFactory.class).to(Facade.class);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 迁移泽西2.22.1到泽西2.22.2 - Migration Jersey 2.22.1 to Jersey 2.22.2 Jersey 1 @Inject 迁移到 Jersey 2 停止工作 - Jersey 1 @Inject migrated to Jersey 2 stopped working @Inject 在 Struts 2 中的 Jersey REST 网络服务中不起作用 - @Inject not working in Jersey REST webservices in Struts 2 泽西岛jarhell-v2.22.1,添加了所有依赖项 - Jersey jarhell - v2.22.1, all dependencies added 微服务:C:\\ ... m2 \\ repository \\ org \\ glassfish \\ jersey \\ core \\ jersey-client \\ 2.22.1 \\ jersey-client-2.22.1.jar; LOC标头无效(签名错误) - microservices: C:\…m2\repository\org\glassfish\jersey\core\jersey-client\2.22.1\jersey-client-2.22.1.jar; invalid LOC header (bad signature) 泽西岛多个构造函数@inject - Jersey multiple constructor @inject 泽西岛:在 ResourceConfig 中使用 @Inject - Jersey : use @Inject in ResourceConfig Jersey将实例注入资源 - Jersey inject instance into Resource 泽西岛:如何使用嵌套对象作为JSON打印通用对象? - Jersey: How to print a generic Object with nested Objects as JSON? 获取ArtifactDescriptorException:无法读取org.glassfish.jersey.media:jersey-media-moxy:jar:2.22.1的工件描述符 - Getting ArtifactDescriptorException: Failed to read artifact descriptor for org.glassfish.jersey.media:jersey-media-moxy:jar:2.22.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM