What are the minimum dependencies required to just use Spring's dependency injection (core framework only)? I'm using Spring for a standalone application, and I'd like to minimize the number of dependencies that I have to ship with the application.
I suppose I could systematically remove a Jar and see if the application breaks, but it would be much better if someone had a definitive answer.
Oh, and I'm using Spring 2.5.
Check out the readme.txt that comes with the download of Spring 2.5.
Assuming you don't have AOP, JDBC, Transactions, or ORM, then your best bet is probably:
spring-core
spring-beans
spring-context
If your are using maven then only spring-context is needed in your pom.xml, it depends on aop, beans, core, expression and asm
Related
I have several projects that depend on a common framework, built on top of Spring Boot. This framework autoconfigures lots of the things that applications use via BeanPostProcessor and similar Spring mechanisms.
It also adds support for optional libraries that the applications can use, using #Conditional beans and optional Maven dependencies.
The problem I'm facing is that one of those optional libraries is Resilience4J and I'd like to add a dependency on resilience4j-micrometer if Resilience4j is added, so I can expose out-of-the-box all Resilience4J metrics via Micrometer.
Is there any mechanism in Maven that allows to add a conditional dependency given some condition, so I ensure that every application using Resilience4J exposes its metrics?
PS: I tried to just create a module to wrap the dependencies, but there are different Resilience4J dependencies the applications use (some apps are web and others Webflux, some use the Spring Cloud abstraction and others don't...).
You can try to manage dependencies via maven profile but it not really the best way. Your framework should integrate all libraries and your app manage dependencies with exclusions in order that Spring Boot can autoconfigure only included dependencies.
I'm not sure I'm following your question, I assume that you have a library "common".
So you have an application A (spring boot driven), and in its pom.xml you add a dependency on "common". You would like to create Beans in the application context of A if A has a dependency on Resilience4J. These beans are supposed to add expose some micrometer related stuff.
In this case you could probably use ConditionalOnClass:
In "common"'s code you could have something like this:
#Configuration
#ConditionalOnClass(Resilience4j.class) // or whatever class that can indicate that resilience4j is in the classpath, I took this for the sake of example, I don't know whether this class really exists
public class CommonConfiguration {
#Bean
public YourMicrometerIntegrationBean yourMicrometerIntegrationBean() {
return new YourMicrometerIntegrationBean();
}
.. other beans ..
}
This solution assumes that you have a dependency (optional ?) at the level of maven in "commons" to resilience4j. If you don't want this for some reason, you could create your custom condition (I believe you know how to do that based on the details that you've supplied in the question you have a pretty advanced setup, let me know if you need more details on that), and this custom condition will evaluate to "true" if Class.forName(<FULLY_QUALIFIED_NAME_OF_RESILIENCE_4_J>) won't throw an exception so that you'll see that its available in runtime.
I downloaded the latest version of Struts 2 to upgrade our current set of jars, and looking at the huge list of jars that come with Struts 2, I want to only include the ones that are truly required.
I had a look at the Struts 2 dependency tree and the Struts 2 compile dependencies, but I'm having trouble understanding what they mean for my application. I don't think we use anything in org.springframework (doing a workspace-wide search), but is that enough to know whether we can exclude those jars?
Is it as simple as looking at the "optional" column of the compile dependencies table from the 2nd link above? Of the optional ones, how do you know when you need them and when you don't? There are so many....
The Struts2 project has a compile dependency on Spring framework. But this doesn't mean that you need a spring framework jars to your project. As you mentioned these dependencies are optional. It means they are required if you use some features of Struts2 that depend directly on Spring. You can use Maven to resolve dependencies for your project's pom.xml. If you want to view/analize a dependency tree you can use eclipse m2eclipse plugin. Analogous visualization tools available in other IDEs. The same as invoking manually mvn dependency:tree.
It doesn't depend on spring framework.
Check here
Step 3 - Add Struts 2 Jar Files To Class Path
to see the minimum required libs for a strtus2 project.
Struts2 is using the spring framework only for the tests and compiling during build.
Beside this only the Struts2 Spring plugin has a dependency to the spring framework.
If you are not using this plugin you should not have any spring dependencies in your project.
struts2 has a dependency on spring framework because of some decent core features.
Dependency injection and AOP are the widely used features of spring framework with struts2.
while struts2 framework is rich in UI and MVC view but these are the components that makes struts2 dependent on spring.
Spring framework's security feature,JDBC,transaction management are the other module that can be used with struts2.
thanks,
Amit Kumar
Struts2 does not have any dependency on Spring. However, when you take a look at struts 2 library, it contains Spring related jar. This is done to add additional support to integrate Struts and Spring and to use nice to have features/modules of Spring like DI, AOP, transaction etc. It is completely optional to exclude those jars and to go ahead with conventional Struts framework. Also as there is no incremental development for struts like bringing struts 3 in market. Way to go forward is to integrate struts with Spring.
I am pretty new in Spring. If I want to implement a Spring MVC project have I to explicitly put the spring-mvc.jar file into my classpath? From what I know the Spring MVC project is not part of the Spring Core.
Is it right?
You can go many ways but I'll suggest you to learn a build automation tool like maven or gradle. It will take care of everything related to dependency management. Here's a good resource to get started https://spring.io/guides/gs/gradle/
I'm fairly new to Java/Spring and am trying to put together a simple app which will include some basic CRUD operations and I'd like to use Hibernate for data access.
I'm using Maven as my build tool. My question is: how can I find out which dependencies are required to use Hibernate? For example, I'm using Spring 3.0.6, but how would I know what version of Hibernate to use with that version of Spring? More over, if there are multiple possible Hibernate dependencies, how would I know which ones to include for the functionality I need? So far this seems to be partially reading documentation and partially trial and error.
Is there a definitive way of knowing which Maven dependencies to use with certain version of other dependencies? Any which dependencies to use for particular bits of functionality?
Thanks,
James.
I follow these steps when starting to use a new framework:
Go to framework's web site. In your case hibernate web site and try to find latest (or a specific) version. For hibernate it is 3.6.8-Final at the time of writing.
Search for a maven dependency definition on the framework web site. If you can not find any dependency definition, them simply google for "frameworkname _version_ maven dependency" and you'll most probably find necessary definition, as well as the necessary repository information. For example you can find the dependency definition for hibernate on mvnrepository.com and necessary artifact repository information on Hibernate 3.6.8 release page:
The artifacts have all been published to the JBoss Nexus repository under the org.hibernate groupId at http://repository.jboss.org/nexus/content/groups/public-jboss/
The question of which dependencies are necessary and which are optional depends entirely on the framework to be used. So for example in order to use hibernate, as stated on Hibernate Quick Start Guide:
hibernate-core: The main artifact, which contains all the Hibernate classes, in packageorg.hibernate. You need these to build applications using the native Hibernate APIs. It includes capabilities for using native Hibernate mapping in hbm.xml files, as well as annotations.
About compatibility issues (which version of hibernate is compatible with spring 3.0.6), all I can say is you have to read about integration manuals for those frameworks. Since Spring and Hibernate are two exclusively distinct frameworks, I don't think you can find a constant location to look for version compatibility matrix or something like that.
The purpose of Maven is to avoid handling dependencies by hand. Just choose which version of Hibernate to use and include it in your pom; Spring supports many different versions.
If you know what parts of Spring you want to use, just include those parts in your pom; they'll include their own requirements.
Is there a specific module and/or version combination you're having an issue with?
The only way to know for sure that you've got all dependencies is by running your app.
Maven resolves for you transitive dependencies so you can quickly detect missing ones by compiling the java code.
However, in a web app there are many dependencies that are required in runtime only, so they are not detected at compilation time.
you can find out the dependencies by running mvn dependency:tree and analyze if they are required or not by running mvn dependency:analyze.
Taking the newest ones usally works as long as they are stable.
Start with hibernate and spring core, context, tx.
After you added some could you will probably recognize that something else is missing.
Try and error doesn't sound good, but its working pretty well for spring dependencies.
I have noticed from several web pages that apparently Spring 3.0 supports #Inject from JSR-330. As we would really like to use JSR-299 syntax for dependency injection in our libraries for both web apps and stand-alone applications, and have alternatives to Weld, it would be nice if Spring could do this.
Being a novice to Spring, I tried downloading the Spring Framework distribution and put all jars on the Eclipse build path. No Inject annotation so my existing test project using Weld did not compile.
Can this be done with Spring? What do I need to do to get it running?
(I am aware that Guice eventually will support this too. It is only in SVN for now, and if there is an official Spring release which can, that would be better.)
It can be done. The JSR-330 jar must be downloaded seperately, and cglib to parse the manually written #Configuration classes, plus a commons logging implementation.
The largest difference from Weld seems to be that the wiring needs to be manually written instead of magically found (a bit more cumbersome, but may make more robust applications), plus the startup time is much less. I am still new to Spring - is there a way to have #Configuration classes autodiscovered?
From the Spring 3.0.x Reference documentation:
JSR 330's #Inject annotation can be used in place of Spring's #Autowired in the examples below. #Inject does not have a required property unlike Spring's #Autowire annotation which has a required property to indicate if the value being injected is optional. This behavior is enabled automatically if you have the JSR 330 JAR on the classpath.
So you can make your code agnostic of the DI framework by using #Inject, but you still need to include a jar with the javax.inject classes in your project because Spring does not ship them itself. You can find the relevant jar in the downloads section at JSR-330's Google Code site.
The javax.inject package is not included as part of Spring 3, but it does support it if it's present.
If you look at the source for AutowiredAnnotationBeanPostProcessor, you'll see the constructor uses reflection to locate javax.inject.Inject, and logs a message if it finds it. There's no compile-time dependency on it.
You'll need to locate the JSR-330 JARs from some other source (e.g. the JavaEE 6 SDK).