Spring5学习深入理解IOC和AOP(二)
「这是我参与2022首次更文挑战的第6天,活动详情查看:2022首次更文挑战」。
关于作者
- 作者介绍
🍓 博客主页:作者主页
🍓 简介:JAVA领域优质创作者🥇、一名在校大三学生🎓、在校期间参加各种省赛、国赛,斩获一系列荣誉🏆、阿里云专家博主、51CTO专家博主
🍓 关注我:关注我学习资料、文档下载统统都有,每日定时更新文章,励志做一名JAVA资深程序猿👨💻
6、IOC创建对象方式
6.1 通过无参构造方法来创建
User.java
package com.spring.pojo;
/**
* @ProjectName: Spring5study
* @Package: com.spring.pojo
* @ClassName: User
* @Author: 张晟睿
* @Date: 2022/1/23 22:00
* @Version: 1.0
*/
public class User {
private String name;
public User() {
System.out.println("user实体类调用无参构造方法");
}
public void setName(String name) {
this.name = name;
}
public void show() {
System.out.println("name=" + name);
}
}
bean.xml
<?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>
测试类
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: 张晟睿
* @Date: 2022/1/23 22:01
* @Version: 1.0
*/
public class MyTest {
@Test
public void test(){ ApplicationContext context = new
ClassPathXmlApplicationContext("bean.xml");
//在执行getBean的时候, user已经创建好了 , 通过无参构造
User user = (User) context.getBean("user");
//调用对象的方法 .
user.show();
}
}
运行结果
结果可以发现,在调用show方法之前,User对象已经通过无参构造初始化了!
6.2 通过有参构造方法来创建
UserT.java
package com.spring.pojo;
/**
* @ProjectName: Spring5study
* @Package: com.spring.pojo
* @ClassName: UserT
* @Author: 张晟睿
* @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 );
}
}
bean.xml
第一种根据index参数下标设置
<?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">
<!-- 第一种根据index参数下标设置 -->
<bean id="userT" class="com.spring.pojo.UserT">
<!-- index指构造方法 , 下标从0开始 -->
<constructor-arg index="0" value="changzhang"/>
</bean>
</beans>
第二种根据参数名字设置
<?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">
<!-- name指参数名 -->
<constructor-arg name="name" value="changzhang"/>
</bean>
</beans>
第三种根据参数类型设置
<?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-arg type="java.lang.String" value="changzhang"/>
</bean>
</beans>
测试类
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: 张晟睿
* @Date: 2022/1/23 22:01
* @Version: 1.0
*/
public class MyTest {
@Test
public void testT() {
ApplicationContext context = new
ClassPathXmlApplicationContext("bean.xml");
UserT user = (UserT) context.getBean("userT");
user.show();
}
}
运行结果
结论:在配置文件加载的时候。其中管理的对象都已经初始化了!
源码链接:spring5demo: Spring打卡学习 (github.com)
后语
厂长写博客目的初衷很简单,希望大家在学习的过程中少走弯路,多学一些东西,对自己有帮助的留下你的赞赞👍或者关注➕都是对我最大的支持,你的关注和点赞给厂长每天更文的动力。
对文章其中一部分不理解,都可以评论区回复我,我们来一起讨论,共同学习,一起进步!
微信(z613500)或者 qq(1016942589) 详细交流。