我想使用 JSR330(@Inject @Named,@Singleton,...)。所以由于spring是常用的,所以我想用spring作为底层实现。但我不知道它是否可以使用:
- 带 JSR330
- 以 Spring 作为实现
- 如果没有 spring 特定的注释,则只允许使用 JSR330 注释。这不就是规范的全部内容。
- 没有 XML
我尝试了以下但没有成功:
package org.di.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.tomerbd.snippets.nlp.OpenNLP;
import javax.inject.Inject;
// A failed attempt to achieve: DI + JSR330 + Spring + NoSpring Specific Annotations + No XML
// I didn't want to have any spring annotations but it looks like I have to, wanted to be pure JSR330.
@Configuration
@ComponentScan
public class DIJSR330NoXML {
public static class UseMe {
public String getMe() { return "here I am"; }
}
public static class IWillUseYou {
@Inject private UseMe useMe;
public String letsUseHim() {
return useMe.getMe();
}
}
public static void main(String[] args) throws Exception {
ApplicationContext ctx = new AnnotationConfigApplicationContext("org.di.spring");
((AnnotationConfigApplicationContext) ctx).refresh();
IWillUseYou user = ctx.getBean(IWillUseYou.class); // any other way to get class? I don't want to use spring annotations only jsr330 please with spring as impl!
System.out.println(user.letsUseHim());
}
}
输出失败:
20:25:38.509 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'DIJSR330NoXML' to allow for resolving potential circular references
20:25:38.564 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'DIJSR330NoXML'
20:25:38.566 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
20:25:38.794 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@51c693d]
20:25:38.805 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
20:25:38.813 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.di.spring.DIJSR330NoXML$IWillUseYou' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:347)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1107)
at org.di.spring.DIJSR330NoXML.main(DIJSR330NoXML.java:31)
Process finished with exit code 1
因此,如果 JSR330 提供 DI 并且 spring 支持 JSR330,是否可以仅使用带有JSR 330 注释且没有任何 spring XML 的 Spring?
如果这是不可能的,你能告诉我为了让它工作我需要使用的绝对最小的弹簧注释集(请不要使用 xml)是什么。