search
HomeJavaJavagetting StartedWhat are the three ways of spring injection?

What are the three ways of spring injection?

Nov 13, 2020 am 10:19 AM
springinjection

The three methods of spring injection are: 1. Constructor injection; 2. Setter injection; 3. Interface injection. Constructor injection depends on the implementation of the constructor method, and setter injection is injected through the setter method.

What are the three ways of spring injection?

Spring’s dependency injection is divided into three methods, as follows:

1. Constructor injection

2.Setter Injection

3. Interface injection

Constructor injection and setter injection are the two main methods of dependency injection. Interface injection refers to the method of injecting from other places. (Injection is achieved by describing it in xml)

(Learning video recommendation: java video tutorial)

1. Constructor injection

Constructor Injection depends on the implementation of the constructor. ----------The constructor can be parameterized or parameterless.

Before spring, in most cases, we created class objects through constructors. Spring can use reflection to complete injection through the construction method. This is the principle of the construction method.

1. Introduce spring support

2. By describing specific classes, construction methods and parameters, spring can create objects through reflection through corresponding information.

3. How to load the spring configuration file in the test file

String configLocation ="applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);
Role role = context.getBean("role", Role.class);

All resources in spring are beans.

2. The most mainstream injection method in spring: setter injection

Setter injection is determined by the JAVA Bean specification

Note:

Constructor injection is through Constructor injection,

setter injection is injected through the setter method

First set the constructor to a parameterless constructor, and then use setter injection to set a new value for it, which is also done through java Reflection technology is achieved.

Note: Constructor injection and setter injection are both achieved through Java's reflection technology.

1     <!-- 下面是setter注入,需要一个无参的构造方法 -->
2     <bean id="role1" class="com.pojo.Role" >
3         <property name="id"    value="124"></property>
4         <property name="name" value="张三"></property>
5         <property name="age" value="2324"></property>
6     </bean>

3. The third way of spring dependency injection: interface injection

Sometimes resources do not come from your own system, but from the outside world. For example, database connection resources can be completely Configure it under Tomcat, and then obtain it through JNDI. Such database connection resources are resources outside the development project.

Related recommendations:Getting started with java

The above is the detailed content of What are the three ways of spring injection?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment