Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-wiring annotations in classes from dependent jars

I'm relatively new to Java, so I hope this isn't a dumb question.

I have a Web project in Eclipse that I'm trying to deploy to Tomcat. I have two dependent projects in Eclipse which are being compiled into .jar files and deployed to the /WEB-INF/lib directory.

This is fine, but unfortunately Spring doesn't scan for annotations in dependencies unless the class files from the .jars are extracted into the /WEB-INF/classes directory.

Is there an easy way to do this extraction at build time? I'm using Maven. I've written a batch file for the time being to do this (I'm developing on Windows, deploying on Ubuntu)

I do have questions about how to automate this for server deployment, though, am I doing something wrong? Surely I'm not the only one to wrestle with this problem.

like image 257
Jason Kolb Avatar asked Feb 08 '11 17:02

Jason Kolb


People also ask

What is use of @autowired annotation?

@Autowired annotation is optional for constructor based injection. Here, the person object from the container is passed to the constructor while creating the Customer object. The setter method will be called with the Person object at runtime by the container.

What is difference between Autowiring and dependency injection?

You can annotate fields and constructor using @Autowired to tell Spring framework to find dependencies for you. The @Inject annotation also serves the same purpose, but the main difference between them is that @Inject is a standard annotation for dependency injection and @Autowired is spring specific.

Which annotation can be used for injecting dependencies in Spring?

@Autowired annotation is used to let Spring know that autowiring is required. This can be applied to field, constructor and methods. This annotation allows us to implement constructor-based, field-based or method-based dependency injection in our components.

What is the difference between @autowired and @inject annotation in Spring?

The behaviour of the @Autowired annotation is similar to the @Inject annotation. The only difference is that the @Autowired annotation is part of the Spring framework. This annotation has the same execution paths as the @Inject annotation, listed in order of precedence: Match by Type.


2 Answers

According to this Spring issue, if you're creating JAR files in Eclipse using the Export... > Java > JAR file wizard, then you have to check the Add directory entries checkbox (unchecked by default) for Spring's component scan to find components in the JAR files.

like image 127
Chin Huang Avatar answered Nov 15 '22 20:11

Chin Huang


I think spring scans the whole classpath, you just have to provide:

<context:component-scan base-package="org.example"/>

There is a note in the docs:

The scanning of classpath packages requires the presence of corresponding directory entries in the classpath. When you build JARs with Ant, make sure that you do not activate the files-only switch of the JAR task.

like image 25
Bozho Avatar answered Nov 15 '22 19:11

Bozho