Spring context as runtime dependency

I am puzzled by this spring documentation section .

For example, to create an application context and use dependency injection to customize the application, your Maven dependencies would look like this:

<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.5.RELEASE</version> <scope>runtime</scope> </dependency> </dependencies> 

Note that a scope can be declared as a runtime if you do not need to compile spring APIs, which is usually the case for the main use cases of dependency injection.

I know about JSR 330 (DI annotations). But how do you separate from the ApplicationContext class? And if you separate from this, why is it still dependent on spring?

How, for example, can I rewrite a quick start with spring-context as a dependency on runtime? Or what would be the “base use case for dependency injection”?

+8
java spring
source share
5 answers

I think the “basic use case” refers to the contexts of XML-based applications. This documentation says that you are not directly using Spring libraries in your code, then you will not have to include these libraries in your path to compilation classes. This is the case with XML configurations, since all Spring is linked in XML and therefore not compiled.

In the quick start that you linked, the author uses the annotation-based application context configuration, which requires the Spring libraries to be included both at compile time and at run time.

XML configuration example: http://www.springbyexample.org/examples/intro-to-ioc-creating-a-spring-application.html

there should be only a few key points when the application code should directly access the IoC container [...]. If you are developing a web application, you may not need direct access to the IoC container at all, since it automatically handles the instantiation of your controller and any beans that it requires.

I'm not quite familiar with this, but it looks like you can use JSR330, as you suggested with the XML configuration, to auto-negotiate beans using annotations. See here . This will allow you to use annotations, but without the need to include Spring in compile time configurations.

+4
source share

one

First of all, let's talk about DI.

Note from Spring Doc ,

Dependency management and dependency injection are two different things.

  • Dependency Management - "compile all the necessary libraries (jar files) and get them on the way to the classes at runtime and, possibly, at compile time."
  • Dependency Injection, suppose you need a Service object in your class, instead of creating it using service = new Service(); , you let the spring structure handle the life cycle of this bean.

Dependency Management Example:

 <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.5.RELEASE</version> </dependency> </dependencies> 

So you have all these jar files in your project.

 spring-context-4.2.5.RELEASE.jar spring-aop-4.2.5.RELEASE.jar spring-beans-4.2.5.RELEASE.jar spring-core-4.2.5.RELEASE.jar 

An example of dependency injection:

In the quick-start example, you enter a MessageService in MessagePrinter by using the constructor insert. You have not created MessageService anywhere. The spring container creates it for you.

 @Component public class MessagePrinter { final private MessageService service; @Autowired public MessagePrinter(MessageService service) { this.service = service; } public void printMessage() { System.out.println(this.service.getMessage()); } } @Configuration @ComponentScan public class Application { @Bean MessageService mockMessageService() { return new MessageService() { public String getMessage() { return "Hello World!"; } }; } ... } 

2

Now let's talk about transitive dependency and runtime dependency .

Transitive dependency

This means that you will find libraries that require your own dependencies, and turn them on automatically.
For example, if you specified the dependencies A and B in pom.xml . And it depends on C, D. B depends on E. You do not need to include C, D, E in your configuration. Due to the transitive dependency, C, D, E will be turned on automatically.

Runtime dependency

This is a type of dependency region for limiting the transitive dependency.

"This area shows that the dependency is not required for compilation, but for execution. It is at runtime and the classpaths test, but not the path to compiling the class."

3

Now the question is: is there any case that for DI "you don’t need to compile using the spring API", can you set the scope as runtime instead? A similar question is here .

Yes, one example I can think of is a web application. Suppose I use Strtuts with a spring plugin. (below is an example from "Struts 2 in Action" by Manning)

I want to tell spring to create an instance of the Login class to use as its action object for the request.

add spring web context listener to web.xml

 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 

define a bean Login named as springManagedLoginAction in applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="portfolioService" class="manning.chapterNine.utils.PortfolioServiceJPAImpl"/> <bean id="springManagedLoginAction" class="manning.chapterNine.Login" scope="prototype"> <property name="portfolioService" ref="portfolioService"/> </bean> </beans> 

use this bean in action class in struts-config-login.xml

 <action name="Login" class="springManagedLoginAction"> <result type="redirectAction"> <param name="actionName">AdminPortfolio</param> <param name="namespace">/chapterEight/secure</param> </result> <result name="input">/chapterEight/Login.jsp</result> </action> 
+3
source share

I'm not sure I understood your question correctly, but it seems to me why you are asking why you need the spring -context dependency if you use JSR-330 annotations.

Well, the answer from my point of view is that you really do not need a dependency on Spring if you use only JSR-330 annotations, but for their work you need a library that understands them and correctly builds the dependency graph for you, and spring -context is one such library.

The reason this runtime dependency is because you can switch this provider at runtime, at least in theory.

0
source share

You can implement your beans with javax.inject (@Named, @Inject) dependency in a separate package. They will be used from the spring project or any other container that has its own DI implementation.

Below is an example of the redone spring components on javax.inject (without splitting the project into different packages) http://www.mkyong.com/spring3/spring-3-and-jsr-330-inject-and-named-example/

-one
source share

If I understand correctly, you basically ask how the dependency injector is initialized and how you can introduce dependencies in your classes. In the quick start example, you indicated that the application context is manually created in the main class.

 ApplicationContext context = new AnnotationConfigApplicationContext(Application.class); 

According to the documentation

An autonomous application context that accepts annotated classes as input — specifically, @ Configured annotated classes, but also simple @Component Types and JSR-330 compatible classes using javax.inject annotations. Allows registering classes one by one using register (Class ...), and also for scanning classes using scanning (String ...)

An alternative way to initialize spring is in your web.xml, where you use ContextLoaderListner , which will load the spring context of the application for you when you start the program.

The question of how to run spring in web.xml has already been answered here .

-one
source share

All Articles