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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

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
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment