Home > Java > Javagetting Started > body text

What are the three ways of spring injection?

王林
Release: 2020-11-13 10:19:02
Original
37025 people have browsed it

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);
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template