What are the three ways of spring ioc injection?
The three ways of spring ioc injection are: 1. Setter method injection. After the container instantiates the bean by calling the parameterless constructor or parameterless static factory method, it calls the setter method of the bean. 2. Constructor method injection. 3. P namespace injection.
#The operating environment of this tutorial: windows7 system, java8 version, Dell G3 computer.
Spring IOC (three methods of dependency injection):
1. Setter method injection
Setter method injection is when the container instantiates a bean by calling a parameterless constructor or a parameterless static factory method, and then calls the setter method of the bean, which implements setter-based dependency injection.
package com.jpeony.spring.setter; import com.jpeony.spring.common.HelloServiceImpl;public class HelloWord { private HelloService helloService; // setter方式注入Bean public void setHelloService(HelloService helloService) { this.helloService = helloService; } @Override public void selfIntroduction() { // 向大家打招呼 helloService.sayHello("大家好!"); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Bean声明: 该bean类似于javaConfig中的@Bean注解; 用于创建bean的类通过class属性来指定,并且需要使用全限定的类名。 通过id指定bean的ID。如果不显示指定,默认使用class的全限定名进行命名。 eg: com.jpeony.spring.common.HelloServiceImpl#0,其#0是一个计数器的形式, 用来区分相同类型的其他bean。 使用自动化命名很方便,但是没有多少实际用处,还是建议自己给bean显示设定ID。 --> <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/> <!-- setter注入bean --> <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord"> <property name="helloService" ref="helloService"/> </bean> </beans>
2. Constructor injection
Constructor dependency injection is implemented by triggering the constructor of a class through the container. The class has a series of Parameters, each parameter represents a dependency on other classes.
package com.jpeony.spring.setter; import com.jpeony.spring.common.HelloServiceImpl; public class HelloWord { private HelloService helloService; // 构造方法注入 public HelloWord (HelloService helloService) { this.helloService = helloService; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Bean声明: 该bean类似于javaConfig中的@Bean注解; 用于创建bean的类通过class属性来指定,并且需要使用全限定的类名。 通过id指定bean的ID。如果不显示指定,默认使用class的全限定名进行命名。 eg: com.jpeony.spring.common.HelloServiceImpl#0,其#0是一个计数器的形式, 用来区分相同类型的其他bean。 使用自动化命名很方便,但是没有多少实际用处,还是建议自己给bean显示设定ID。 --> <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/> <!-- 构造方法注入bean --> <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord"> <constructor-arg><ref bean="helloService"/></constructor-arg> </bean> </beans>
3. P namespace injection
package com.jpeony.spring.setter; import com.jpeony.spring.common.HelloServiceImpl; public class HelloWord { //名字 private String name; //年龄 private String age; //方法类 private HelloService helloService; public void setName (String name) { this.name = name; } public void setAge (String age) { this.age = age; } public void setHelloService(HelloService helloService) { this.helloService = helloService; } @Override public void selfIntroduction() { // 向大家打招呼 helloService.sayHello("我叫"+ name + ",今年" + age + "岁,大家好!"); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <!-- 引入p命名标签 --> xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/> <!-- p标签注入bean --> <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord" p:name="明明" p:age="24" p:helloService-ref="helloService"></bean> </beans>
P tag injection collection bean
package com.jpeony.spring.setter; import com.jpeony.spring.common.HelloServiceImpl; import java.util.List; public class HelloWord { //名字 private String name; //年龄 private String age; //方法类 private List<HelloService> helloServices; public void setName (String name) { this.name = name; } public void setAge (String age) { this.age = age; } public void setHelloServices(List<HelloService> helloServices) { this.helloServices = helloServices; } @Override public void selfIntroduction() { // 向大家打招呼 helloServices[0].sayHello("我叫"+ name + ",今年" + age + "岁,大家好!"); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <!-- 引入p命名标签 --> xmlns:p="http://www.springframework.org/schema/p" <!-- 引入util命名标签 --> xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/> <bean id="helloService2" class="com.jpeony.spring.common.HelloServiceImpl"> ........... </bean> <util:list id="helloServices"> <ref bean="helloService"/> <ref bean="helloService2"/> </util:list> <!-- p标签注入bean --> <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord" p:name="明明" p:age="24" p:helloServices-ref="helloServices"></bean> </beans>
For more knowledge about computer programming, please visit: Introduction to Programming! !
The above is the detailed content of What are the three ways of spring ioc 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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo
