Search in sources :

Example 1 with NamedLiteral

use of org.b3log.latke.ioc.literal.NamedLiteral in project latke by b3log.

the class BeanImpl method addQualifier.

/**
 * Adds a qualifier with the specified qualifier.
 *
 * @param qualifier the specified qualifier
 */
private void addQualifier(final Annotation qualifier) {
    if (qualifier.getClass().equals(NamedLiteral.class)) {
        final NamedLiteral namedQualifier = (NamedLiteral) getNamedQualifier();
        final NamedLiteral newNamedQualifier = (NamedLiteral) qualifier;
        if (!namedQualifier.value().equals(newNamedQualifier.value())) {
            setNamedQualifier(newNamedQualifier);
        }
    } else {
        qualifiers.add(qualifier);
    }
    configurator.addClassQualifierBinding(beanClass, qualifier);
}
Also used : NamedLiteral(org.b3log.latke.ioc.literal.NamedLiteral)

Example 2 with NamedLiteral

use of org.b3log.latke.ioc.literal.NamedLiteral 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 NamedLiteral

use of org.b3log.latke.ioc.literal.NamedLiteral in project latke by b3log.

the class BeanImpl method named.

@Override
public LatkeBean<T> named(final String name) {
    final Named namedQualifier = new NamedLiteral(name);
    addQualifier(namedQualifier);
    return this;
}
Also used : Named(org.b3log.latke.ioc.inject.Named) NamedLiteral(org.b3log.latke.ioc.literal.NamedLiteral)

Example 4 with NamedLiteral

use of org.b3log.latke.ioc.literal.NamedLiteral in project latke by b3log.

the class Beans method getQualifiers.

/**
 * Gets qualifiers of the specified class. If no qualifiers,
 *
 * @param clazz the specified class
 * @param beanName the specified bean name
 * @return qualifier annotations
 */
public static Set<Annotation> getQualifiers(final Class<?> clazz, final String beanName) {
    final Annotation[] annotations = clazz.getAnnotations();
    final Set<Annotation> qualifierAnnotations = CollectionUtils.arrayToSet(annotations);
    Set<Annotation> ret = selectQualifiers(qualifierAnnotations);
    if (ret == null) {
        ret = new HashSet<Annotation>();
    }
    if (!hasNamedQualifier(clazz)) {
        ret.add(new NamedLiteral(beanName));
    }
    return ret;
}
Also used : NamedLiteral(org.b3log.latke.ioc.literal.NamedLiteral) Annotation(java.lang.annotation.Annotation)

Example 5 with NamedLiteral

use of org.b3log.latke.ioc.literal.NamedLiteral in project latke by b3log.

the class MoonUnitTest method beforeTest.

@BeforeTest
@SuppressWarnings("unchecked")
public void beforeTest() throws Exception {
    System.out.println("before MoonUnitTest");
    Latkes.initRuntimeEnv();
    beanManager = LatkeBeanManagerImpl.getInstance();
    Lifecycle.startApplication(moonPackageClasses);
    final Set<Annotation> moonQualifiers = new HashSet<Annotation>();
    moonQualifiers.add(new NamedLiteral("moon"));
    final LatkeBean<?> moonBean = beanManager.getBean(Moon.class, moonQualifiers);
    moon = (Moon) beanManager.getReference(moonBean);
    assertNotNull(moon);
    final Set<Annotation> artificalMoonQualifiers = new HashSet<Annotation>();
    artificalMoonQualifiers.add(new ArtificalLiteral());
    final LatkeBean<?> artificalMoonBean = beanManager.getBean(ArtificalMoon.class, artificalMoonQualifiers);
    artificalMoon = (ArtificalMoon) beanManager.getReference(artificalMoonBean);
    assertNotNull(artificalMoon);
    final Set<Annotation> waterMoonQualifiers = new HashSet<Annotation>();
    waterMoonQualifiers.add(new ArtificalLiteral());
    waterMoonQualifiers.add(new WaterLiteral());
    final LatkeBean<?> waterMoonBean = beanManager.getBean(WaterMoon.class, waterMoonQualifiers);
    waterMoon = (WaterMoon) beanManager.getReference(waterMoonBean);
    assertNotNull(waterMoon);
}
Also used : WaterLiteral(org.b3log.latke.ioc.moon.annotation.WaterLiteral) ArtificalLiteral(org.b3log.latke.ioc.moon.annotation.ArtificalLiteral) NamedLiteral(org.b3log.latke.ioc.literal.NamedLiteral) Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

NamedLiteral (org.b3log.latke.ioc.literal.NamedLiteral)5 Annotation (java.lang.annotation.Annotation)3 HashSet (java.util.HashSet)2 BeforeTest (org.testng.annotations.BeforeTest)2 Configurator (org.b3log.latke.ioc.config.Configurator)1 Named (org.b3log.latke.ioc.inject.Named)1 ArtificalLiteral (org.b3log.latke.ioc.moon.annotation.ArtificalLiteral)1 WaterLiteral (org.b3log.latke.ioc.moon.annotation.WaterLiteral)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