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);
}
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);
}
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;
}
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;
}
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);
}
Aggregations