首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用带有Spring的JSR 330作为实现,而不需要任何XML和特定于spring的注释?

如何使用带有Spring的JSR 330作为实现,而不需要任何XML和特定于spring的注释?
EN

Stack Overflow用户
提问于 2018-07-26 17:26:38
回答 1查看 1.2K关注 0票数 2

我想使用JSR330 (@Inject,@Singleton,.)。因此,由于spring是常用的,所以我希望使用spring作为底层实现。但我不明白是否可以用:

  1. 与JSR330
  2. 以Spring为实现的
  3. 没有特定的春季注释,只允许使用JSR330注释。这不是规范的全部内容吗。
  4. 没有XML的

我尝试了以下方法,但没有成功:

代码语言:javascript
复制
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());

    }
}

输出失败:

代码语言:javascript
复制
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呢?

如果这是不可能的,那么您能否告诉我,我需要使用哪一组最小的spring注释(请不要使用xml )才能正常工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-26 17:37:32

来自javax.inject的所有注释(包括这些命名注释)都被理解为JSR330,它只是一个API,即一个提供接口的应用程序接口,而没有任何实现。

这意味着使用这些注释,因为它不会触发任何效果,因为没有类、容器,也没有监视这些注释在哪里使用的任何内容。Java和Java都没有提供任何IoC (反转控制)容器。您必须包括这些实现框架- Spring就是一个很好的例子。

看看前男友。JBoss焊。配置此实现和其他实现的方式(注释、XML,任何不同的东西)都是单独的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51544449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档