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.
@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.
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.
@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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With