Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The principle and usage of spring dependency injection

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the principle and usage of spring dependency injection. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the principle and usage of spring dependency injection.

A little bit of eye contact

Control inversion and dependency injection are equivalent concepts in the Spring environment, and control inversion is implemented through dependency injection. The so-called dependency injection means that the container is responsible for creating objects and maintaining dependencies between objects, rather than creating and solving their own dependencies through the objects themselves.

The main purpose of dependency injection is to decouple and embody a concept of combination. What if you want your class to have a function and inherit it from a parent class with a secondary function? Is it better to combine another class with this function? The answer is self-evident: inheriting a parent class, the subclass and the parent class are coupled, and combining another class greatly reduces the coupling.

The Spring IoC container (ApplicationContext) is responsible for creating the Bean and injecting the functional Bean into the Bean you need through the container.

Spring provides Bean creation and injection using xml, annotations, Java configuration, and groovy configuration.

Whether it is xml configuration, annotations, or Java configuration, it is called configuration metadata, which is the data that describes the data. Metadata itself does not have any executable ability, only through external code to parse the metadata and do some meaningful operations. The Spring container parses this metadata for Bean initialization, configuration, and management dependencies.

II. Notes on declaring Bean

@ Component component: there is no explicit role. Service: used in the business logic layer (service layer). Respository: used in the data access layer (dao layer). Controller: used in the presentation layer (MVC- > Spring MVC).

Notes on the third injection of Bean

The annotations provided by @ Autowired:Spring @ Inject:JSR-330 @ Resource:JSR-250 provide annotations @ Autowired, @ Inject, @ Resource can be annotated on set methods or properties. Generally, it is customary to annotate on attributes, which has the advantage of less code and clearer hierarchy.

Four actual combat

1 write the Bean of the function class

Package com.wisely.highlight_spring4.ch2.di;import org.springframework.stereotype.Service;// uses the @ Service annotation to declare that the current FunctionService class is a Bean managed by Spring. / / using @ Component, @ Service, @ Respository, @ Controller is equivalent, but / / choose @ Servicepublic class FunctionService {public String sayHello (String word) {return "Hello" + word + "!";}} as needed.

2 use the functional class Bean

Package com.wisely.highlight_spring4.ch2.di;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Service / / use @ Service annotation to declare that the current UseFunctionService class is a Beanpublic class UseFunctionService managed by Spring {/ / use @ Autowired to inject FunctionService entity Bean into UseFunctionService / / let UseFunctionService have the function of FunctionService, here use JSR-330 's Inject / / annotation or JSR-250 's @ Resource annotation is equivalent to @ Autowired FunctionService functionService Public String SayHello (String word) {return functionService.sayHello (word);}}

3 configuration class

Package com.wisely.highlight_spring4.ch2.di;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration / / @ Configuration declares that the current class is a configuration class. / / use @ ComponentScan to automatically scan all @ Component, @ Service, @ Respository, @ Controller// classes under the package and register as Bean@ComponentScan ("com.wisely.highlight_spring4.ch2.di") public class DiConfig {}

4 running class

Package com.wisely.highlight_spring4.ch2.di;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main {public static void main (String [] args) {/ / uses AnnotationConfigApplicationContext as the Spring container, receives / / a configuration class as the parameter AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (DiConfig.class); / / gets the Bean UseFunctionService useFunctionService = context.getBean (UseFunctionService.class) that declares the configuration UseFunctionService; System.out.println (useFunctionService.SayHello ("world")); context.close ();}}

Five tests

Hello world!

Thank you for your reading, the above is the content of "the principle and usage of spring dependency injection". After the study of this article, I believe you have a deeper understanding of the principle and usage of spring dependency injection, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report