Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 620
Blankman Avatar asked Jan 05 '12 04:01

Blankman


2 Answers

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.

like image 122
Aravind A Avatar answered Sep 28 '22 09:09

Aravind A


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).

like image 36
Raghuram Avatar answered Sep 28 '22 09:09

Raghuram