I have javax.inject in my pom.xml, will spring use it automatically?

12,996

Solution 1

The dependency in your pom for javax.inject

   <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

has nothing to do with Spring . It just brings in a javax.inject dependency into the project . A jar with the name javax.inject-1.jar . This jar is needed if you use the @Inject annotation which is supported by Spring as well .

You could use @Autowired/@Resource/@Inject as per your needs . See here for their difference and also a discussion at What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition? .

As for slf4j , it is just an abstraction over frameworks like log4j allowing run time plugging in of logging framework . Spring-OSGI , Hibernate all use this internally. So this is why you find the dependency in your pom . Hope this clears things out.

Solution 2

inject enables JSR 330 support to spring. Using the inject annotations makes the application not tied to spring - it could be switched to Java EE 6 or guice or other providers which support the specification.

You could either use inject or spring injection (or perhaps both).

Solution 3

as @Aravind told javax.inject dependency has nothing to do with spring. and it's used to get annotation @Inject, it's is part of the Java CDI standard, introduced in Java EE 6 (JSR-299)

Spring using @Inject synonymously with their own @Autowired annotation.
@Autowired and @Inject, two annotations works the same way as Spring has decided to support some JSR-299 annotations in addition to their own.

comming to Simple Logging Facade for Java (SLF4J):

The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks, e.g. java.util.logging, log4j and logback, allowing the end user to plug in the desired logging framework at deployment time.

Share:
12,996
Blankman
Author by

Blankman

... .. . blank

Updated on July 10, 2022

Comments

  • Blankman
    Blankman almost 2 years

    I copied a pom.xml while I was going through a spring mvc tutorial online, and it had:

            <!-- @Inject -->
            <dependency>
                <groupId>javax.inject</groupId>
                <artifactId>javax.inject</artifactId>
                <version>1</version>
            </dependency>
    

    From what I understand spring has its own dependancy injection built-in, will this override the default and use javax.inject?

    I have also seen slf4j in pom's, with no further setup in code or xml.

    How does this work under the covers, spring examines the lib's folder and if any lib is found that is overridable it does it?