Java 如何使用JSR330和Spring作为实现,而不使用任何XML和Spring特定的注释?
我想使用JSR330(@Inject@Named,@Singleton,…)。因为spring是常用的,所以我想使用spring作为底层实现。但我不知道是否可以使用:Java 如何使用JSR330和Spring作为实现,而不使用任何XML和Spring特定的注释?,java,spring,dependency-injection,spring-annotations,jsr330,Java,Spring,Dependency Injection,Spring Annotations,Jsr330,我想使用JSR330(@Inject@Named,@Singleton,…)。因为spring是常用的,所以我想使用spring作为底层实现。但我不知道是否可以使用: 使用JSR330 以Spring作为实现 没有特定于spring的注释只允许JSR330注释。这不是规范的全部内容吗 不带XML 我尝试了以下方法,但没有成功: package org.di.spring; import org.springframework.context.ApplicationContext; import
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和spring支持JSR330,那么spring是否可以只使用JSR330注释而不使用任何SpringXML
如果这是不可能的,你能告诉我,为了让它工作,我需要使用的spring注释的绝对最小集是什么(请不要使用xml)。所有被理解为JSR330
的注释,包括这些命名注释,都只是一个API,即,提供接口的应用程序接口,无需任何实现
这意味着按原样使用这些注释不会触发任何效果,因为没有类、容器或任何东西可以监视这些注释在何处使用。JavaSE和JavaEE都不提供任何IoC(控制反转)容器。您必须包括这些方面的实现框架——Spring就是一个很好的例子
看一看。此实现和其他实现的配置方式(注释、XML、任何不同的内容)是独立的。
iwillseyou
未使用@Named
进行注释,因此不会被检测到。此外,您始终需要一个带有Spring的ApplicationContext
,因为这是Spring的核心。其他一切都可能是JSR330。事实上,我在iWillueYou和UseMe中都添加了@Named,它起了作用。我大体上同意这个答案,并想说hk2是JSR-330在“原始”jvm上的一个很好的轻量级实现。(). 充分披露:我是hk2项目的技术负责人,所以我可能有点偏见lol@jwells131313当前位置HK2的设计迫切需要完成,否则,它很难在开源项目中流行。是的,这是真的。这是一个更好的文档站点: