/** * Raise a BeanNotOfRequiredTypeException for an unresolvable dependency, if applicable, * i.e. if the target type of the bean would match but an exposed proxy doesn't. */ private void checkBeanNotOfRequiredType(Class<?> type, DependencyDescriptor descriptor) { for (String beanName : this.beanDefinitionNames) { RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); Class<?> targetType = mbd.getTargetType(); if (targetType != null && type.isAssignableFrom(targetType) && isAutowireCandidate(beanName, mbd, descriptor, getAutowireCandidateResolver())) { // Probably a proxy interfering with target type match -> throw meaningful exception. Object beanInstance = getSingleton(beanName, false); Class<?> beanType = (beanInstance != null && beanInstance.getClass() != NullBean.class ? beanInstance.getClass() : predictBeanType(beanName, mbd)); if (beanType != null && !type.isAssignableFrom(beanType)) { throw new BeanNotOfRequiredTypeException(beanName, type, beanType); } } } BeanFactory parent = getParentBeanFactory(); if (parent instanceof DefaultListableBeanFactory) { ((DefaultListableBeanFactory) parent).checkBeanNotOfRequiredType(type, descriptor); } }
Object singletonInstance = getSingleton(beanName); if (singletonInstance instanceof SmartInitializingSingleton) { final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
/** * Raise a BeanNotOfRequiredTypeException for an unresolvable dependency, if applicable, * i.e. if the target type of the bean would match but an exposed proxy doesn't. */ private void checkBeanNotOfRequiredType(Class<?> type, DependencyDescriptor descriptor) { for (String beanName : this.beanDefinitionNames) { RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); Class<?> targetType = mbd.getTargetType(); if (targetType != null && type.isAssignableFrom(targetType) && isAutowireCandidate(beanName, mbd, descriptor, getAutowireCandidateResolver())) { // Probably a proxy interfering with target type match -> throw meaningful exception. Object beanInstance = getSingleton(beanName, false); Class<?> beanType = (beanInstance != null && beanInstance.getClass() != NullBean.class ? beanInstance.getClass() : predictBeanType(beanName, mbd)); if (beanType != null && !type.isAssignableFrom(beanType)) { throw new BeanNotOfRequiredTypeException(beanName, type, beanType); } } } BeanFactory parent = getParentBeanFactory(); if (parent instanceof DefaultListableBeanFactory) { ((DefaultListableBeanFactory) parent).checkBeanNotOfRequiredType(type, descriptor); } }
Object singletonInstance = getSingleton(beanName); if (singletonInstance instanceof SmartInitializingSingleton) { final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
@Override protected Object getSingleton(String beanName, boolean allowEarlyReference) { if (super.containsSingleton(beanName)) { return super.getSingleton(beanName, allowEarlyReference); } else { return getBean(beanName); } }
@Override public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) { if (super.containsSingleton(beanName)) { return super.getSingleton(beanName, singletonFactory); } else { return getBean(beanName); } }
@Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) registry; ConfigurableEnvironment environment = (ConfigurableEnvironment) beanFactory .getSingleton("environment"); int total = environment.getProperty("bench.beans", Integer.class, 0); for (int i = beans; i < beans + total; i++) { registry.registerBeanDefinition("service" + i, BeanDefinitionBuilder .rootBeanDefinition(Service.class).getBeanDefinition()); } beans += total; total = environment.getProperty("bench.aspects", Integer.class, 0); Class<?> interceptorType = ServiceInterceptor.class; if (environment.getProperty("bench.annotation", Boolean.class, false)) { interceptorType = AnnotationInterceptor.class; } for (int i = aspects; i < aspects + total; i++) { registry.registerBeanDefinition("aspect" + i, BeanDefinitionBuilder .rootBeanDefinition(interceptorType).getBeanDefinition()); } aspects += total; }
public List<AuditAppender> auditAppenders(final String domain) throws BeansException { return implementationLookup.getAuditAppenderClasses().stream().map(clazz -> { AuditAppender auditAppender; if (ApplicationContextProvider.getBeanFactory().containsSingleton(clazz.getName())) { auditAppender = (AuditAppender) ApplicationContextProvider.getBeanFactory(). getSingleton(clazz.getName()); } else { auditAppender = (AuditAppender) ApplicationContextProvider.getBeanFactory(). createBean(clazz, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, true); auditAppender.setDomainName(domain); auditAppender.init(); } return auditAppender; }).collect(Collectors.toList()); }
public static Optional<PasswordRule> buildPasswordRule(final Implementation impl) throws InstantiationException, IllegalAccessException { switch (impl.getEngine()) { case GROOVY: return Optional.of(ImplementationManager.<PasswordRule>buildGroovy(impl)); case JAVA: default: PasswordRule rule = null; PasswordRuleConf ruleConf = POJOHelper.deserialize(impl.getBody(), PasswordRuleConf.class); Class<? extends PasswordRule> ruleClass = ApplicationContextProvider.getApplicationContext(). getBean(ImplementationLookup.class).getPasswordRuleClass(ruleConf.getClass()); if (ruleClass == null) { LOG.warn("Could not find matching password rule for {}", impl.getClass()); } else { // fetch (or create) rule if (ApplicationContextProvider.getBeanFactory().containsSingleton(ruleClass.getName())) { rule = (PasswordRule) ApplicationContextProvider.getBeanFactory(). getSingleton(ruleClass.getName()); } else { rule = (PasswordRule) ApplicationContextProvider.getBeanFactory(). createBean(ruleClass, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false); ApplicationContextProvider.getBeanFactory(). registerSingleton(ruleClass.getName(), rule); } rule.setConf(ruleConf); } return Optional.ofNullable(rule); } }
public static Optional<Reportlet> buildReportlet(final Implementation impl) throws InstantiationException, IllegalAccessException { switch (impl.getEngine()) { case GROOVY: return Optional.of(ImplementationManager.<Reportlet>buildGroovy(impl)); case JAVA: default: Reportlet reportlet = null; ReportletConf reportletConf = POJOHelper.deserialize(impl.getBody(), ReportletConf.class); Class<? extends Reportlet> reportletClass = ApplicationContextProvider.getApplicationContext(). getBean(ImplementationLookup.class).getReportletClass(reportletConf.getClass()); if (reportletClass == null) { LOG.warn("Could not find matching reportlet for {}", reportletConf.getClass()); } else { // fetch (or create) reportlet if (ApplicationContextProvider.getBeanFactory().containsSingleton(reportletClass.getName())) { reportlet = (Reportlet) ApplicationContextProvider.getBeanFactory(). getSingleton(reportletClass.getName()); } else { reportlet = (Reportlet) ApplicationContextProvider.getBeanFactory(). createBean(reportletClass, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false); ApplicationContextProvider.getBeanFactory(). registerSingleton(reportletClass.getName(), reportlet); } reportlet.setConf(reportletConf); } return Optional.ofNullable(reportlet); } }
public static Optional<PushCorrelationRule> buildPushCorrelationRule(final Implementation impl) throws InstantiationException, IllegalAccessException { switch (impl.getEngine()) { case GROOVY: return Optional.of(ImplementationManager.<PushCorrelationRule>buildGroovy(impl)); case JAVA: default: PushCorrelationRule rule = null; PushCorrelationRuleConf ruleConf = POJOHelper.deserialize(impl.getBody(), PushCorrelationRuleConf.class); Class<? extends PushCorrelationRule> ruleClass = ApplicationContextProvider.getApplicationContext(). getBean(ImplementationLookup.class).getPushCorrelationRuleClass(ruleConf.getClass()); if (ruleClass == null) { LOG.warn("Could not find matching push correlation rule for {}", impl.getClass()); } else { // fetch (or create) rule if (ApplicationContextProvider.getBeanFactory().containsSingleton(ruleClass.getName())) { rule = (PushCorrelationRule) ApplicationContextProvider.getBeanFactory(). getSingleton(ruleClass.getName()); } else { rule = (PushCorrelationRule) ApplicationContextProvider.getBeanFactory(). createBean(ruleClass, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false); ApplicationContextProvider.getBeanFactory(). registerSingleton(ruleClass.getName(), rule); } rule.setConf(ruleConf); } return Optional.ofNullable(rule); } }
public static Optional<Reportlet> buildReportlet(final Implementation impl) throws InstantiationException, IllegalAccessException { switch (impl.getEngine()) { case GROOVY: return Optional.of(ImplementationManager.<Reportlet>buildGroovy(impl)); case JAVA: default: Reportlet reportlet = null; ReportletConf reportletConf = POJOHelper.deserialize(impl.getBody(), ReportletConf.class); Class<? extends Reportlet> reportletClass = ApplicationContextProvider.getApplicationContext(). getBean(ImplementationLookup.class).getReportletClass(reportletConf.getClass()); if (reportletClass == null) { LOG.warn("Could not find matching reportlet for {}", reportletConf.getClass()); } else { // fetch (or create) reportlet if (ApplicationContextProvider.getBeanFactory().containsSingleton(reportletClass.getName())) { reportlet = (Reportlet) ApplicationContextProvider.getBeanFactory(). getSingleton(reportletClass.getName()); } else { reportlet = (Reportlet) ApplicationContextProvider.getBeanFactory(). createBean(reportletClass, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false); ApplicationContextProvider.getBeanFactory(). registerSingleton(reportletClass.getName(), reportlet); } reportlet.setConf(reportletConf); } return Optional.ofNullable(reportlet); } }
public static Optional<AccountRule> buildAccountRule(final Implementation impl) throws InstantiationException, IllegalAccessException { switch (impl.getEngine()) { case GROOVY: return Optional.of(ImplementationManager.<AccountRule>buildGroovy(impl)); case JAVA: default: AccountRule rule = null; AccountRuleConf ruleConf = POJOHelper.deserialize(impl.getBody(), AccountRuleConf.class); Class<? extends AccountRule> ruleClass = ApplicationContextProvider.getApplicationContext(). getBean(ImplementationLookup.class).getAccountRuleClass(ruleConf.getClass()); if (ruleClass == null) { LOG.warn("Could not find matching account rule for {}", impl.getClass()); } else { // fetch (or create) rule if (ApplicationContextProvider.getBeanFactory().containsSingleton(ruleClass.getName())) { rule = (AccountRule) ApplicationContextProvider.getBeanFactory(). getSingleton(ruleClass.getName()); } else { rule = (AccountRule) ApplicationContextProvider.getBeanFactory(). createBean(ruleClass, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false); ApplicationContextProvider.getBeanFactory(). registerSingleton(ruleClass.getName(), rule); } rule.setConf(ruleConf); } return Optional.ofNullable(rule); } }
public static Optional<PullCorrelationRule> buildPullCorrelationRule(final Implementation impl) throws InstantiationException, IllegalAccessException { switch (impl.getEngine()) { case GROOVY: return Optional.of(ImplementationManager.<PullCorrelationRule>buildGroovy(impl)); case JAVA: default: PullCorrelationRule rule = null; PullCorrelationRuleConf ruleConf = POJOHelper.deserialize(impl.getBody(), PullCorrelationRuleConf.class); Class<? extends PullCorrelationRule> ruleClass = ApplicationContextProvider.getApplicationContext(). getBean(ImplementationLookup.class).getPullCorrelationRuleClass(ruleConf.getClass()); if (ruleClass == null) { LOG.warn("Could not find matching pull correlation rule for {}", impl.getClass()); } else { // fetch (or create) rule if (ApplicationContextProvider.getBeanFactory().containsSingleton(ruleClass.getName())) { rule = (PullCorrelationRule) ApplicationContextProvider.getBeanFactory(). getSingleton(ruleClass.getName()); } else { rule = (PullCorrelationRule) ApplicationContextProvider.getBeanFactory(). createBean(ruleClass, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false); ApplicationContextProvider.getBeanFactory(). registerSingleton(ruleClass.getName(), rule); } rule.setConf(ruleConf); } return Optional.ofNullable(rule); } }
public static Optional<PushCorrelationRule> buildPushCorrelationRule(final Implementation impl) throws InstantiationException, IllegalAccessException { switch (impl.getEngine()) { case GROOVY: return Optional.of(ImplementationManager.<PushCorrelationRule>buildGroovy(impl)); case JAVA: default: PushCorrelationRule rule = null; PushCorrelationRuleConf ruleConf = POJOHelper.deserialize(impl.getBody(), PushCorrelationRuleConf.class); Class<? extends PushCorrelationRule> ruleClass = ApplicationContextProvider.getApplicationContext(). getBean(ImplementationLookup.class).getPushCorrelationRuleClass(ruleConf.getClass()); if (ruleClass == null) { LOG.warn("Could not find matching push correlation rule for {}", impl.getClass()); } else { // fetch (or create) rule if (ApplicationContextProvider.getBeanFactory().containsSingleton(ruleClass.getName())) { rule = (PushCorrelationRule) ApplicationContextProvider.getBeanFactory(). getSingleton(ruleClass.getName()); } else { rule = (PushCorrelationRule) ApplicationContextProvider.getBeanFactory(). createBean(ruleClass, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false); ApplicationContextProvider.getBeanFactory(). registerSingleton(ruleClass.getName(), rule); } rule.setConf(ruleConf); } return Optional.ofNullable(rule); } }
public static Optional<AccountRule> buildAccountRule(final Implementation impl) throws InstantiationException, IllegalAccessException { switch (impl.getEngine()) { case GROOVY: return Optional.of(ImplementationManager.<AccountRule>buildGroovy(impl)); case JAVA: default: AccountRule rule = null; AccountRuleConf ruleConf = POJOHelper.deserialize(impl.getBody(), AccountRuleConf.class); Class<? extends AccountRule> ruleClass = ApplicationContextProvider.getApplicationContext(). getBean(ImplementationLookup.class).getAccountRuleClass(ruleConf.getClass()); if (ruleClass == null) { LOG.warn("Could not find matching account rule for {}", impl.getClass()); } else { // fetch (or create) rule if (ApplicationContextProvider.getBeanFactory().containsSingleton(ruleClass.getName())) { rule = (AccountRule) ApplicationContextProvider.getBeanFactory(). getSingleton(ruleClass.getName()); } else { rule = (AccountRule) ApplicationContextProvider.getBeanFactory(). createBean(ruleClass, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false); ApplicationContextProvider.getBeanFactory(). registerSingleton(ruleClass.getName(), rule); } rule.setConf(ruleConf); } return Optional.ofNullable(rule); } }
public static Optional<PullCorrelationRule> buildPullCorrelationRule(final Implementation impl) throws InstantiationException, IllegalAccessException { switch (impl.getEngine()) { case GROOVY: return Optional.of(ImplementationManager.<PullCorrelationRule>buildGroovy(impl)); case JAVA: default: PullCorrelationRule rule = null; PullCorrelationRuleConf ruleConf = POJOHelper.deserialize(impl.getBody(), PullCorrelationRuleConf.class); Class<? extends PullCorrelationRule> ruleClass = ApplicationContextProvider.getApplicationContext(). getBean(ImplementationLookup.class).getPullCorrelationRuleClass(ruleConf.getClass()); if (ruleClass == null) { LOG.warn("Could not find matching pull correlation rule for {}", impl.getClass()); } else { // fetch (or create) rule if (ApplicationContextProvider.getBeanFactory().containsSingleton(ruleClass.getName())) { rule = (PullCorrelationRule) ApplicationContextProvider.getBeanFactory(). getSingleton(ruleClass.getName()); } else { rule = (PullCorrelationRule) ApplicationContextProvider.getBeanFactory(). createBean(ruleClass, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false); ApplicationContextProvider.getBeanFactory(). registerSingleton(ruleClass.getName(), rule); } rule.setConf(ruleConf); } return Optional.ofNullable(rule); } }
public static Optional<PasswordRule> buildPasswordRule(final Implementation impl) throws InstantiationException, IllegalAccessException { switch (impl.getEngine()) { case GROOVY: return Optional.of(ImplementationManager.<PasswordRule>buildGroovy(impl)); case JAVA: default: PasswordRule rule = null; PasswordRuleConf ruleConf = POJOHelper.deserialize(impl.getBody(), PasswordRuleConf.class); Class<? extends PasswordRule> ruleClass = ApplicationContextProvider.getApplicationContext(). getBean(ImplementationLookup.class).getPasswordRuleClass(ruleConf.getClass()); if (ruleClass == null) { LOG.warn("Could not find matching password rule for {}", impl.getClass()); } else { // fetch (or create) rule if (ApplicationContextProvider.getBeanFactory().containsSingleton(ruleClass.getName())) { rule = (PasswordRule) ApplicationContextProvider.getBeanFactory(). getSingleton(ruleClass.getName()); } else { rule = (PasswordRule) ApplicationContextProvider.getBeanFactory(). createBean(ruleClass, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false); ApplicationContextProvider.getBeanFactory(). registerSingleton(ruleClass.getName(), rule); } rule.setConf(ruleConf); } return Optional.ofNullable(rule); } }
Object singletonInstance = getSingleton(beanName); if (singletonInstance instanceof SmartInitializingSingleton) { final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
final Object singleton = super.getSingleton(name, true); if (singleton == null) { throw new NoSuchBeanDefinitionException(name);