DEPENDENCY INJECTION EXAMPLE USING SPRING

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DEPENDENCY INJECTION EXAMPLE USING SPRING相关的知识,希望对你有一定的参考价值。

Dependency Injection

The Spring Framework is literally built around the concept of Dependency Injection. In this post, we’ll take a look at a simple example of Dependency Injection using the Spring Framework.

If you want a deeper dive on Dependency Injection and how it works in conjunction with Inversion of Control in the Spring Framework, sign up for my free Introduction to Spring tutorial at the bottom of this post.

Dependency Injection Example

In this blog post, I will take a realistic example of having a web controller and a service. In practice, the controller would be responsible for managing requests from the web, and the service would interact with the persistence layer.

Domain

Our service will return a domain class. In this example, our controller will return a simple list of products.

技术分享
 1 package guru.springframework.domain;
 2 
 3 /**
 4  * Created by jt on 3/27/15.
 5  */
 6 public class Product {
 7     private String description;
 8 
 9     public Product(String description) {
10         this.description = description;
11     }
12 
13     public String getDescription() {
14         return description;
15     }
16 
17     public void setDescription(String description) {
18         this.description = description;
19     }
20 }
Product.class

Service Layer

Ideally, when you are coding for Dependency Injection, you will want to code to an interface. This will allow you easily utilize polymorphism and implement different concrete implementations. When coding for the use of dependency injection, coding to an interface also complies with the Interface Segregation Principle of the SOLID principles of Object Oriented Programming.

A common example would be to have the implementation you will use in your production code, and then a mock implementation for unit testing your code. This is the power of dependency injection. It allows you to change the behavior of your application through configuration changes over code changes. For example with persistence, you might be injecting a mock for unit testing, a H2 database for local development and CI builds, and then a Oracle implementation when your code is running in production. When developing enterprise class applications, dependency injection gives you a tremendous amount of versatility. 

技术分享
 1 package guru.springframework.services;
 2 
 3 import guru.springframework.domain.Product;
 4 
 5 import java.util.List;
 6 
 7 /**
 8  * Created by jt on 3/27/15.
 9  */
10 public interface ProductService {
11 
12     List<Product> listProducts();
13 }
ProductService.interface

Here is the implementation of the service. This is just a simple implementation which returns a list of Product domain POJOs, which is sufficient for this example. Naturally, in a real example, this implementation would be interacting with the database or possibly a web service.

I’ve annotated the class with @Service , this tells Spring this class is a Spring Bean to be managed by the Spring Framework. This step is critical, Spring will not detect this class as a Spring Bean without this annotation. Alternatively, you could explicitly define the bean in a Spring configuration file.

技术分享
 1 package guru.springframework.services;
 2 
 3 import guru.springframework.domain.Product;
 4 import org.springframework.stereotype.Service;
 5 
 6 import java.util.ArrayList;
 7 import java.util.List;
 8 
 9 /**
10  * Created by jt on 3/27/15.
11  */
12 @Service
13 public class ProductServiceImpl implements ProductService {
14 
15     @Override
16     public List<Product> listProducts() {
17         ArrayList<Product> products = new ArrayList<Product>(2);
18         products.add(new Product("Product 1 description"));
19         products.add(new Product("Product 2 description"));
20         return products;
21     }
22 }
ProductServiceImpl.class

Controller

We have a simple controller to return a list of Products to our view layer. In this example, I’m using setter based Dependency Injection.   First, I’ve defined a property in our example controller using the Interface type, not the concrete class. This allows any class to be injected which implements the specified interface.  On the setter, you see the @Autowired  annotation. This directs Spring to inject a Spring managed bean into this class.  Our controller class is also annotated with the @Controller  annotation. This marks the class as a Spring Managed bean. Without this annotation, Spring will not bring this class into the context, and will not inject an instance of the service into the class.  

技术分享
 1 package guru.springframework.controllers;
 2 
 3 import guru.springframework.domain.Product;
 4 import guru.springframework.services.ProductService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 
 8 import java.util.List;
 9 
10 /**
11  * Created by jt on 3/27/15.
12  */
13 @Controller
14 public class MyController {
15 
16     private ProductService productService;
17 
18     @Autowired
19     public void setProductService(ProductService productService) {
20         this.productService = productService;
21     }
22 
23     public List<Product> getProducts(){
24         return productService.listProducts();
25     }
26 
27 }
MyController.class

Running the Example

We’ll use Spring Boot to run our example. Spring Boot will help bring up the Spring context for running our example. Spring Boot does automate a lot of common tasks for us. For example, it will automatically do a component scan in the package the class is running in.

Example Execution Code

For our example, we need to tell Spring where our components are located. We use the @ComponentScan  annotation. By using this annotation Spring will scan the specified package for Spring components (aka Spring managed beans).

In our main method, we get the Spring Context, then request from the context an instance of our controller bean. Spring will give us an instance of the controller. Spring will perform the Dependency Injection for us, and inject the dependent components into the object returned to us.

It is important to remember, the Spring Context is returning to us Spring Managed beans. This means Spring will be managing the dependency injection for us. If for some reason, Spring cannot fulfill a dependency, it will fail to startup. You will see in the stack trace information about the missing dependencies.

技术分享
 1 package diexample;
 2 
 3 import guru.springframework.controllers.MyController;
 4 import guru.springframework.domain.Product;
 5 import org.springframework.boot.SpringApplication;
 6 import org.springframework.boot.autoconfigure.SpringBootApplication;
 7 import org.springframework.context.ApplicationContext;
 8 import org.springframework.context.annotation.ComponentScan;
 9 
10 import java.util.List;
11 
12 @SpringBootApplication
13 @ComponentScan("guru.springframework")
14 public class DiExampleApplication {
15 
16     public static void main(String[] args) {
17         ApplicationContext ctx = SpringApplication.run(DiExampleApplication.class, args);
18         MyController controller = (MyController) ctx.getBean("myController");
19         List<Product> products = controller.getProducts();
20 
21         for(Product product : products){
22             System.out.println(product.getDescription());
23         }
24     }
25 }
DiExampleApplication.class

Console Output

When you run the above example, you will see the following output in the console.  Note in the console output, you see our two product descriptions, which proves that Spring did in fact wire our controller correctly. If Spring did not, our code would have failed on a null pointer error.  

技术分享
 1   .   ____          _            __ _ _
 2  /\\ / ___‘_ __ _ _(_)_ __  __ _ \ \ \  3 ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \  4  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
 5   ‘  |____| .__|_| |_|_| |_\__, | / / / /
 6  =========|_|==============|___/=/_/_/_/
 7  :: Spring Boot ::        (v1.2.2.RELEASE)
 8 
 9 2015-03-27 10:28:21.016  INFO 64108 --- [           main] diexample.DiExampleApplication           : Starting DiExampleApplication on Johns-MacBook-Pro.local with PID 64108 (/Users/jt/src/springframework.guru/blog/di-example/target/classes started by jt in /Users/jt/src/springframework.guru/blog/di-example)
10 2015-03-27 10:28:21.115  INFO 64108 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]3e57cd70: startup date [Fri Mar 27 10:28:21 EDT 2015]; root of context hierarchy
11 2015-03-27 10:28:22.107  INFO 64108 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
12 2015-03-27 10:28:22.121  INFO 64108 --- [           main] diexample.DiExampleApplication           : Started DiExampleApplication in 1.606 seconds (JVM running for 2.134)
13 Product 1 description
14 Product 2 description
15 2015-03-27 10:28:22.122  INFO 64108 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]3e57cd70: startup date [Fri Mar 27 10:28:21 EDT 2015]; root of context hierarchy
16 2015-03-27 10:28:22.123  INFO 64108 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
Output

 

以上是关于DEPENDENCY INJECTION EXAMPLE USING SPRING的主要内容,如果未能解决你的问题,请参考以下文章

Dependency Injection学习笔记

Using MEF to Set Up Dependency Injection

Dependency Injection in ASP.NET Web API 2

Dependency Injection: Constructor vs Property

learning scala dependency injection

Why using constructor based dependency injection | Spring