Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

258
Vistas
How autowire will work in manual object instantiation?

ServiceInvokerImpl.java

Object lService = null;
lService = Class.forName("com.test.AssetServiceImpl").newInstance();

AssetServiceImpl.java

public class AssetServiceImpl implements LogisticService {

    @Autowired
    private EntityLifeCycleManager entityLifeCycleManager;


    @Override
    @Transactional
    public FetchResults findAsset(String cls, QueryDetail query, OperationProperties props) {

        return entityLifeCycleManager.find("com.test.model.Asset", query, props);
}

Question Description

When I instantiate AssetServiceImpl in ServiceInvokerImpl.java, it shows Autowired property entityLifeCycleManager of AssetServiceImpl.java as null. So, how autowire will work for manual instantiation for above scenario ?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

@Autowired only works for managed instances, i.e. object instances which are created by a Dependency Injection container (for @Autowired, this is Spring).

So if you just invoke Class#getInstance() (which is basically the same as instantiate with new operator), @Autowired is just ignored, and entityLifeCycleManager will be null.

If you still need to instantiate it manually (and not with Spring), you could use constructor injection and manually supply the dependency, for example:

public class AssetServiceImpl implements LogisticService {

    private final EntityLifeCycleManager entityLifeCycleManager;

    @Autowired
    public AssetServiceImpl(EntityLifeCycleManager entityLifeCycleManager) {
        this.entityLifeCycleManager = entityLifeCycleManager;
    }

    ...

and then instantiate it either with new operator:

EntityLifeCycleManager entityLifeCycleManager = //.. somehow obtain an EntityLifeCycleManager instance
LogisticService logisticService = new AssetServiceImpl(entityLifeCycleManager);

or via Class:

EntityLifeCycleManager entityLifeCycleManager = //.. somehow obtain an EntityLifeCycleManager instance
LogisticService logisticService = Class.forName("com.test.AssetServiceImpl").getConstructor(new Class[]{EntityLifeCycleManager.class}).newInstance(new Object[]{entityLifeCycleManager});

Please note that @Autowired annotation is moved to the constructor to still allow this service be created and autowired by Spring if you like so.

Also it is worth noting that field injection (which is used in our initial example) is not recommended. The reason is that same usecase: it is difficult to instantiate (and properly inject with collaborators) a class that uses field injection. Constructor injection is better in this regard.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda