2

Does Jodd framework provide mechanism to inject petitebeans references for the objects created by other frameworks.

Below are scenarios
- Domain/Service objects are created by Spring Framework
- Domain objects created are by ORM Frameworks
- These objects need to be injected with Repository/DAO object (Singleton objects registered as PetiteBean via AutomagicPetiteConfigurator)


Below is sample code, after petite container is shutdown, initMethod() is invoked when pc.getBean(Greetings.class).message(null) is invoked and destroyMethod() is not invoked, can you please point me what I am doing wrong?

@PetiteBean("greetings")
public class EnglishGreetings implements Greetings {

    @Override
    public String message(String message) {

        if (message == null) {
            return "defaultMessage";
        }
        return message;
    }

    @PetiteInitMethod
    public void initMethod() {
        System.out.println("Entered initMethod");
    }

    @PetiteDestroyMethod
    public void destroyMethod() {
        System.out.println("Entered destroyMethod");
    }
}

public class GreetingRunner {
    final static Logger logger = LoggerFactory.getLogger(GreetingRunner.class);

    @PetiteInject
    public Greetings greetings;

    public static void main(String s[]) {

        jodd.log.LoggerFactory.setLoggerFactory(new Slf4jLoggerFactory());

        PetiteContainer pc = new PetiteContainer();
        AutomagicPetiteConfigurator configurator = new AutomagicPetiteConfigurator();
        configurator.setIncludedEntries("com.rans.*");
        configurator.configure(pc);
        pc.shutdown();
        System.out.println(pc.getBean(Greetings.class).message(null));
    }
}
NRV
  • 21
  • 3
  • Why do you call `pc.shutdown();` _before_ using `getBean`? – igr Apr 27 '16 at 22:07
  • @игор, Intent to call pc.shutdown() was to check the following - Whether destroyMethod() will be invoked marked with PetiteDestroyMethod annotation and Beans cannot be accessed after Petite Container is shutdown – NRV Apr 29 '16 at 22:19
  • please mark the correct answer. – igr Sep 26 '17 at 10:58

1 Answers1

0

Destroy method has not been invoked because of lazy aspect of Petite - if bean has not been used, no destroy method will be called. The same applies to init methods. If bean is not used, Petite simple ignores it.

Now back to the question:

Does Jodd framework provide mechanism to inject petitebeans references for the objects created by other frameworks.

Technically, yes - if you overwrite it :) See PetiteProxettaContainer. You may override getBean and use 3rd party container to fetch the bean. Actually, you may override createBeanDefinitionForRegistration method to register the bean in the different container. To be honest, we might make this more obvious :)

(Sorry for late response)

igr
  • 10,199
  • 13
  • 65
  • 111
  • After PetiteContainer shutdown() method is called, can PetiteContainer be configured to throw exception when trying to access beans via PetiteContainer getBean APIs – NRV May 03 '16 at 20:13
  • Hey @NRV, in 3.7.1 we will delete all the bean data. Would that work you - instead of throwing exception? – igr May 22 '16 at 21:59
  • And feel free @NRV to mark the question answered, for other ppl – igr Jun 03 '16 at 05:06