IOC创建对象方式与依赖注入

本文详细介绍了Java中IOC(Inverse of Control)如何通过无参构造方法和构造方法注入来创建对象。重点讨论了依赖注入的概念,强调Bean的创建与管理依赖于容器,并且容器负责设置和装配Bean所依赖的资源。特别地,文章提到了Set注入的实现,要求被注入的属性必须有set方法,以及p命名和c命名注入的方式,这些都需要有参构造。此外,还提及了如何整合多个Spring配置文件。
摘要由CSDN通过智能技术生成

无参构造方法

通过默认的无参构造方法来创建

构造方法注入

有三种方式编写

<!-- 第一种根据index参数下标设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
<!-- index指构造方法 , 下标从0开始 -->
<constructor-arg index="0" value="kuangshen2"/>
</bean>
<!-- 第二种根据参数名字设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
<!-- name指参数名 -->
<constructor-arg name="name" value="kuangshen2"/>
</bean>
<!-- 第三种根据参数类型设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
<constructor-arg type="java.lang.String" value="kuangshen2"/>
</bean>

结论:在配置文件加载的时候。其中管理的对象都已经初始化了!

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

依赖注入:(Dependency Injection,DI)

  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .

  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .

我采用构造器注入

Set 注入 (重点)

要求被注入的属性 , 必须有set方法

public class Hello {
    private String str;
    public void setStr(String str) {
        this.str = str;
    }
}

Student.java

 package com.kuang.pojo;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 
 public class Student {
 
     private String name;
     private Address address;
     private String[] books;
     private List<String> hobbys;
     private Map<String,String> card;
     private Set<String> games;
     private String wife;
     private Properties info;
 
     public void setName(String name) {
         this.name = name;
     }
 
     public void setAddress(Address address) {
         this.address = address;
     }
 
     public void setBooks(String[] books) {
         this.books = books;
     }
 
     public void setHobbys(List<String> hobbys) {
         this.hobbys = hobbys;
     }
 
     public void setCard(Map<String, String> card) {
         this.card = card;
     }
 
     public void setGames(Set<String> games) {
         this.games = games;
     }
 
     public void setWife(String wife) {
         this.wife = wife;
     }
 
     public void setInfo(Properties info) {
         this.info = info;
     }
 
     public void show(){
         System.out.println("name="+ name
                 + ",address="+ address.getAddress()
                 + ",books="
         );
         for (String book:books){
             System.out.print("<<"+book+">>\t");
         }
         System.out.println("\n爱好:"+hobbys);
 
         System.out.println("card:"+card);
 
         System.out.println("games:"+games);
 
         System.out.println("wife:"+wife);
 
         System.out.println("info:"+info);
 
     }
 }
//1、常量注入
 <bean id="student" class="com.kuang.pojo.Student">
     <property name="name" value="小明"/>
 </bean>
 
 //Bean注入
 <bean id="addr" class="com.kuang.pojo.Address">
     <property name="address" value="重庆"/>
 </bean>
 
 <bean id="student" class="com.kuang.pojo.Student">
     <property name="name" value="小明"/>
     <property name="address" ref="addr"/>
 </bean>
 
 //数组注入
  <bean id="student" class="com.kuang.pojo.Student">
     <property name="name" value="小明"/>
     <property name="address" ref="addr"/>
     <property name="books">
         <array>
             <value>西游记</value>
             <value>红楼梦</value>
             <value>水浒传</value>
         </array>
     </property>
 </bean>

//List注入
 <property name="hobbys">
     <list>
         <value>听歌</value>
         <value>看电影</value>
         <value>爬山</value>
     </list>
 </property>

//Map注入
 <property name="card">
     <map>
         <entry key="中国邮政" value="456456456465456"/>
         <entry key="建设" value="1456682255511"/>
     </map>
 </property>

//null注入
 <property name="wife"><null/></property>

//Properties注入
 <property name="info">
     <props>
         <prop key="学号">20190604</prop>
         <prop key="性别"></prop>
         <prop key="姓名">小明</prop>
     </props>
 </property>

p命名和c命名注入

(需要有参构造)

 导入约束 : xmlns:p="http://www.springframework.org/schema/p"
 
 <!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
 <bean id="user" class="com.kuang.pojo.User" p:name="狂神" p:age="18"/>
导入约束 : xmlns:c="http://www.springframework.org/schema/c"
<!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.kuang.pojo.User" c:name="狂神" c:age="18"/>

整合多个spring配置文件

//团队的合作通过import来实现 .
<import resource="{path}/beans.xml"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值