Search in sources :

Example 1 with Configurator

use of org.b3log.latke.ioc.config.Configurator in project latke by b3log.

the class Lifecycle method startApplication.

/**
 * Starts the application with the specified bean class and bean modules.
 *
 * @param classes the specified bean class, nullable
 * @param beanModule the specified bean modules
 */
public static void startApplication(final Collection<Class<?>> classes, final BeanModule... beanModule) {
    LOGGER.log(Level.DEBUG, "Initializing Latke IoC container");
    beanManager = LatkeBeanManagerImpl.getInstance();
    applicationContext.setActive(true);
    beanManager.addContext(applicationContext);
    final Configurator configurator = beanManager.getConfigurator();
    if (null != classes && !classes.isEmpty()) {
        configurator.createBeans(classes);
    }
    if (null != beanModule && 0 < beanModule.length) {
        for (int i = 0; i < beanModule.length; i++) {
            configurator.addModule(beanModule[i]);
        }
    }
    LOGGER.log(Level.DEBUG, "Initialized Latke IoC container");
}
Also used : Configurator(org.b3log.latke.ioc.config.Configurator)

Example 2 with Configurator

use of org.b3log.latke.ioc.config.Configurator in project latke by b3log.

the class SpeakerUnitTest method beforeTest.

@BeforeTest
@SuppressWarnings("unchecked")
public void beforeTest() throws Exception {
    System.out.println("before SpeakerUnitTest");
    Latkes.initRuntimeEnv();
    beanManager = LatkeBeanManagerImpl.getInstance();
    Lifecycle.startApplication(speakerPackageClasses);
    final Configurator configurator = beanManager.getConfigurator();
    // Create beans by APIs approach
    configurator.createBean(HelloSpeaker.class).qualified(new HelloLiteral());
    configurator.createBean(NightSpeaker.class).qualified(new NightLiteral());
    configurator.createBean(MidnightSpeaker.class).qualified(new MidnightLiteral());
    final Set<Annotation> helloSpeakerQualifiers = new HashSet<Annotation>();
    helloSpeakerQualifiers.add(new HelloLiteral());
    helloSpeakerQualifiers.add(new NamedLiteral("helloSpeaker"));
    final LatkeBean<?> helloSpeakerBean = beanManager.getBean(Speaker.class, helloSpeakerQualifiers);
    helloSpeaker = (HelloSpeaker) beanManager.getReference(helloSpeakerBean);
    assertNotNull(helloSpeaker);
    configurator.validate();
    final Set<Annotation> morningSpeakerQualifiers = new HashSet<Annotation>();
    morningSpeakerQualifiers.add(new MorningLiteral());
    morningSpeakerQualifiers.add(new NamedLiteral("morningSpeaker"));
    final LatkeBean<?> morningSpeakerBean = beanManager.getBean(Speaker.class, morningSpeakerQualifiers);
    morningSpeaker = (MorningSpeaker) beanManager.getReference(morningSpeakerBean);
    assertNotNull(morningSpeaker);
    final Set<Annotation> speakerQualifiers = new HashSet<Annotation>();
    speakerQualifiers.add(new NamedLiteral("speakerService"));
    final LatkeBean<?> speakerProviderBean = beanManager.getBean(SpeakerService.class, speakerQualifiers);
    speakerProvider = (SpeakerService) beanManager.getReference(speakerProviderBean);
    assertNotNull(speakerProvider);
}
Also used : Configurator(org.b3log.latke.ioc.config.Configurator) NamedLiteral(org.b3log.latke.ioc.literal.NamedLiteral) Annotation(java.lang.annotation.Annotation) MorningLiteral(org.b3log.latke.ioc.speaker.annotation.MorningLiteral) HelloLiteral(org.b3log.latke.ioc.speaker.annotation.HelloLiteral) MidnightLiteral(org.b3log.latke.ioc.speaker.annotation.MidnightLiteral) NightLiteral(org.b3log.latke.ioc.speaker.annotation.NightLiteral) HashSet(java.util.HashSet) BeforeTest(org.testng.annotations.BeforeTest)

Example 3 with Configurator

use of org.b3log.latke.ioc.config.Configurator in project latke by b3log.

the class BottleUnitTest method beforeTest.

@BeforeTest
@SuppressWarnings("unchecked")
public void beforeTest() throws Exception {
    System.out.println("before BottleUnitTest");
    Latkes.initRuntimeEnv();
    Lifecycle.startApplication(drinkPackageClasses);
    beanManager = LatkeBeanManagerImpl.getInstance();
    // Creates bean by APIs
    final Configurator configurator = beanManager.getConfigurator();
    configurator.createBean(Mix.class).named("spiritMix").scoped(Singleton.class).qualified(new OddLiteral());
    // Validate after bean configuration
    configurator.validate();
    final LatkeBean<?> wineBottleBean = beanManager.getBean(WineBottle.class);
    assertNotNull(wineBottleBean);
    wineBottle = (WineBottle) beanManager.getReference(wineBottleBean);
    assertNotNull(wineBottle);
    final LatkeBean<?> juiceBottleBean = beanManager.getBean(JuiceBottle.class);
    assertNotNull(juiceBottleBean);
    juiceBottle = (JuiceBottle) beanManager.getReference(juiceBottleBean);
    assertNotNull(juiceBottle);
    final LatkeBean<?> mixBottleBean = beanManager.getBean(MixBottle.class, new HashSet<Annotation>());
    mixBottle = (MixBottle) beanManager.getReference(mixBottleBean);
    assertNotNull(mixBottle);
}
Also used : OddLiteral(org.b3log.latke.ioc.drink.annotation.OddLiteral) Configurator(org.b3log.latke.ioc.config.Configurator) Singleton(org.b3log.latke.ioc.inject.Singleton) Mix(org.b3log.latke.ioc.drink.mix.Mix) Annotation(java.lang.annotation.Annotation) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

Configurator (org.b3log.latke.ioc.config.Configurator)3 Annotation (java.lang.annotation.Annotation)2 BeforeTest (org.testng.annotations.BeforeTest)2 HashSet (java.util.HashSet)1 OddLiteral (org.b3log.latke.ioc.drink.annotation.OddLiteral)1 Mix (org.b3log.latke.ioc.drink.mix.Mix)1 Singleton (org.b3log.latke.ioc.inject.Singleton)1 NamedLiteral (org.b3log.latke.ioc.literal.NamedLiteral)1 HelloLiteral (org.b3log.latke.ioc.speaker.annotation.HelloLiteral)1 MidnightLiteral (org.b3log.latke.ioc.speaker.annotation.MidnightLiteral)1 MorningLiteral (org.b3log.latke.ioc.speaker.annotation.MorningLiteral)1 NightLiteral (org.b3log.latke.ioc.speaker.annotation.NightLiteral)1