How to use JSR 330 with Spring as implementation without any XML and without spring specific annotations?

1.3k views Asked by At

I wanted to use JSR330 (@Inject @Named, @Singleton, ...). So as spring is commonly used I wanted to use spring as the underlying implementation. But I don't get if its possible to use:

  1. With JSR330
  2. With Spring as implementation
  3. Without spring specific annotations only JSR330 annotations allowed. Isn't that what the specification is all about.
  4. Without XML

I tried the below with no success:

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());

    }
}

got failed output:

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

So if JSR330 presented DI and spring supports JSR330 is it possible to use Spring with Only JSR 330 annotations and without any spring XML?

If this is not possible just impossible could you tell me what is the absolute minimal set of spring annotations (with no xml please) that I need to use in order to have it working.

1

There are 1 answers

3
Nikolas Charalambidis On BEST ANSWER

All the annotation from javax.inject which is understood as JSR330 including these named annotations is just an API, i.e. an application interface providing the interface without any implementation.

It means using these annotations as is would trigger no effect since there is no class, container, nothing that would monitor where are these annotations used. Neither Java SE nor Java EE provides any IoC (Inversion of Control) container. You have to include the implementation framework for these - Spring is a good example.

Take a look at ex. JBoss Weld. The way this and others implementations are configured (annotations, XML, anything different) is individual.