Learn more about IOC and AOP
This is the sixth day of my participation in the First Challenge 2022. For details: First Challenge 2022.
About the author
- The authors introduce
🍓 Blog home Page: author home page 🍓 Introduction: High-quality creator in the JAVA field 🥇, a junior student 🎓, participated in various provincial and national competitions during school, and won a series of honors 🍓, Ali Cloud expert blogger, 51CTO expert blogger, follow me: Pay attention to my learning materials, document download all have, regularly update the article every day, inspirational to do a JAVA senior program ape 👨💻
6. IOC object creation method
6.1 Create by using the no-argument constructor
User.java
package com.spring.pojo; /** * @projectName: Spring5study * @package: com.spring. Pojo * @classname: User * @author: Zhang Shengrui * @date: 2022/1/23 22:00 * @version: 1.0 */ public class User {private String name; Public User() {system.out.println (" User entity class calls no-argument constructor "); } public void setName(String name) { this.name = name; } public void show() { System.out.println("name=" + name); }}Copy the code
bean.xml
<? The 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 id="user" class="com.spring.pojo.User"> <property name="name" value="changzhang"/> </bean> </beans>Copy the code
The test class
import com.spring.pojo.User; import org.junit.jupiter.api.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @projectName: Spring5study * @package: PACKAGE_NAME * @classname: MyTest * @author: Zhang Shenggrui * @date: 2022/1/23 22:01 * @Version: Public class MyTest {@test public void Test (){ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); User = (user) context.getBean("user"); user = (user) context.getbean ("user"); // Call the object's method.user.show (); }}Copy the code
The results
As it turns out, the User object was already initialized with no-argument construction before the show method was called!
6.2 Create by the parameter constructor
UserT.java
package com.spring.pojo; /** * @projectName: Spring5study * @package: com.spring. Pojo * @classname: UserT * @author: Zhang Shengrui * @date: 2022/1/23 22:06 * @version: 1.0 */ public class UserT {private String name; public UserT(String name) { this.name = name; } public void setName(String name) { this.name = name; } public void show(){ System.out.println("name="+ name ); }}Copy the code
bean.xml
The first is based on the index parameter
<? The 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 id="userT" class="com.spring.pojo. userT" > <! Constructor ="0" value="changzhang"/> </bean> </beans>Copy the code
The second is based on the parameter name
<? The 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 id="userT" class="com.kuang.pojo. userT" > <! <constructor-arg name="name" value="changzhang"Copy the code
The third is based on the parameter type
<? The 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"> <! Class ="com.spring.pojo. userT" > <constructor-arg type=" java.lang.string" value="changzhang"/> </bean> </beans>Copy the code
The test class
import com.spring.pojo.User; import org.junit.jupiter.api.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @projectName: Spring5study * @package: PACKAGE_NAME * @classname: MyTest * @author: Zhang Shenggrui * @date: 2022/1/23 22:01 * @Version: Public class MyTest {@test public void testT() {ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); UserT user = (UserT) context.getBean("userT"); user.show(); }}Copy the code
The results
Conclusion: When the configuration file is loaded. All managed objects are already initialized!
Spring5demo: Spring Clocking learning (github.com)
After the language
The original intention of the director to write blog is very simple, I hope everyone in the process of learning less detours, learn more things, to their own help to leave your praise 👍 or pay attention to ➕ are the biggest support for me, your attention and praise to the director every day more power.
If you don’t understand one part of the article, you can reply to me in the comment section. Let’s discuss, learn and progress together!
Wechat (Z613500) or QQ (1016942589) for detailed communication.