1、IoC创建对象的方式

  1. 使用无参构造创建对象

  2. 假如要使用有参构造创建:

    1. 下标赋值constructor-arg

      <!--有参-->
      <bean id="User" class="com.reliable.pojo.User" >
      <constructor-arg index="0" value="靠谱杨"></constructor-arg>
      </bean>
      public User(String name){
      System.out.println("User的有参构造!");
      this.name=name;
      }
    2. 通过类型type="java.lang.String"

        <bean id="User" class="com.reliable.pojo.User" >
    <constructor-arg type="java.lang.String" value="靠谱杨"></constructor-arg>
    </bean>
    1. 通过参数名name="name" value="reliable"
        <bean id="User" class="com.reliable.pojo.User" >
    <constructor-arg name="name" value="reliable"></constructor-arg>
    </bean>

    总结:在配置文件加载的时候,Spring容器中管理的对象就已经初始化成功了!

2、Spring的配置

2.1、别名

    <!--别名-->
<alias name="User" alias="new_user"></alias>

2.2、Bean的配置

<?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">
<!--
类型 变量名 = new 类型();
Hello hello = new Hello(); bean就是java对象 , 由Spring创建和管理
bean = 一个对象
其中
id = 变量名
class = new的对象类型
property相当于给对象里的属性设置一个值
-->
<bean id="Hello" class="com.reliable.pojo.Hello">
<property name="name" value="Spring"/>
</bean>
<!-- 无参 -->
<!--<bean id="User" class="com.reliable.pojo.User">-->
<!--<property name="name" value="靠谱"></property>-->
<!--</bean>-->
<!--有参第一种,index-->
<!--<bean id="User" class="com.reliable.pojo.User" >
<constructor-arg index="0" value="靠谱杨"></constructor-arg>
</bean>-->
<!-- 2 类型-->
<!-- <bean id="User" class="com.reliable.pojo.User" >
<constructor-arg type="java.lang.String" value="靠谱杨"></constructor-arg>
</bean>-->
<!-- 3 参数名字 -->
<bean id="User" class="com.reliable.pojo.User" >
<constructor-arg name="name" value="User"></constructor-arg>
</bean>
<bean id="User1" class="com.reliable.pojo.User1">
<constructor-arg name="name" value="User1"></constructor-arg>
</bean>
<!--别名 如果添加的别名 都可以使用-->
<alias name="User" alias="new_user"></alias>
</beans>

2.3、import

一般用于团队开发使用,可以将多个配置文件导入合并为一个

    <!--import -->
<import resource="beans1.xml"></import>

3、依赖注入(DI)

3.1 构造器注入

  • 依赖注入:Set注入

    • 依赖:bean对象的创建依赖于容器
    • 注入:bean对象中的所有属性,由容器来注入!

3.2、Set方式注入【重点】

  • 复杂类型
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
  • 实体对象
import java.util.*;

public class Student {
public String getName() {
return name;
} public Address getAddress() {
return address;
} public String[] getBooks() {
return books;
} public List<String> getHobbies() {
return hobbies;
} public Map<String, String> getCard() {
return card;
} public Set<String> getGames() {
return games;
} public String getWife() {
return wife;
} public Properties getInfo() {
return info;
}
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
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 setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
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;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
//show方法
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爱好:"+ hobbies);
System.out.println("card:"+card);
System.out.println("games:"+games);
System.out.println("wife:"+wife);
System.out.println("info:"+info);
}
}

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="Address" class="com.kuang.pojo.Address">
<property name="address" value="石家庄"></property>
</bean>
<bean id="Student" class="com.kuang.pojo.Student">
<!-- 第一种:普通值注入 -->
<property name="name" value="杨传伟"></property>
<!-- 第二种:ref注入 -->
<property name="address" ref="Address"></property>
<!-- 第三种:数组注入 -->
<property name="books">
<array>
<value>《红楼梦》</value>
<value>《西游记》</value>
<value>《水浒传》</value>
<value>《三国演义》</value>
</array>
</property>
<!-- 第四种:List注入 -->
<property name="hobbies">
<list>
<value>听音乐</value>
<value>看电影</value>
<value>敲代码</value>
<value>摄影</value>
</list>
</property> <!-- 第五种:Map注入 --> <property name="card">
<map>
<entry key="IDcard" value="1234567"></entry>
<entry key="STcard" value="7654321"></entry>
</map>
</property>
<!-- 第六种:Set注入 -->
<property name="games">
<set>
<value>跑跑卡丁车官方竞速版</value>
<value>王者荣耀</value>
</set>
</property> <!-- 第七种:设置空值 -->
<property name="wife">
<null></null>
</property> <!--properties-->
<property name="info">
<props>
<prop key="学号">20194074</prop>
<prop key="性别">男</prop>
<prop key="姓名">杨传伟</prop>
<prop key="username">reliable</prop>
<prop key="userpass">resetpass01</prop>
</props>
</property>
</bean>
</beans>

3.3、拓展方式注入

使用p命名空间和c命名空间

使用:

package com.kuang.pojo;

public class User {
private String name;
private int age; public User(String name,int age) {
this.name = name;
this.age=age;
}
public User(){};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + 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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.kuang.pojo.User" p:name="靠谱" p:age="21"/>
<!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
<bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="18"/>
</beans>

测试:

	public void test2(){
ApplicationContext context=new ClassPathXmlApplicationContext("beans03.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
User user2 = context.getBean("user2", User.class);
System.out.println(user2);
}

注意

要引入c和p命名空间:

xmlns:p="http://www.springframework.org/schema/p"

xmlns:c="http://www.springframework.org/schema/c"

Spring框架之IoC( Inversion of Control )基础知识入门的更多相关文章

  1. Spring框架的IOC之注解方式的快速入门

    1. 步骤一:导入注解开发所有需要的jar包 * 引入IOC容器必须的6个jar包 * 多引入一个:Spring框架的AOP的jar包,spring-aop的jar包 2. 步骤二:创建对应的包结构, ...

  2. Spring (一) IOC ( Inversion Of Control )

    前序 现在小米手机很火就还拿小米手机来举例子,上一篇写的关于SSH框架搭建是从小米手机公司内个整个流程方面来考虑,如何提高效率生产效率,这篇博客主要从公司外部环境说明如何提高生产效率,那么怎么才能提高 ...

  3. Spring框架中IoC(控制反转)的原理(转)

    原文链接:Spring框架中IoC(控制反转)的原理 一.IoC的基础知识以及原理: 1.IoC理论的背景:在采用面向对象方法设计的软件系统中,底层实现都是由N个对象组成的,所有的对象通过彼此的合作, ...

  4. 初识Spring框架实现IOC和DI(依赖注入)

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的, IoC是 ...

  5. 【Java_Spring】控制反转IOC(Inversion of Control)

    1. IOC的概念 控制反转IoC(Inversion of Control)是一种设计思想,而DI(依赖注入)是实现IoC的一种方法.在没有使用IOC的程序中,对象间的依赖关系是靠硬编码的方式实现的 ...

  6. Spring框架的IOC(控制反转)

    1.1.IoC是什么 Ioc-Inversion of Control,即"控制反转",不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制, ...

  7. Spring框架(3)---IOC装配Bean(注解方式)

    IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...

  8. (精简)Spring框架的IoC(替代工厂类实现方法)和AOP(定义规则,约定大于配置)

    Spring的核心框架主要包含两个技术,分别用来处理工厂类,以及事务处理和连接管理的. 两大核心概念 1)  IoC:控制反转,在现在的开发中,如果想建立对象并设置属性,是需要先new对象,再通过se ...

  9. Spring框架之IOC(控制反转)

    [TOC] 第一章Spring框架简介 IOC(控制反转)和AOP(面向方面编程)作为Spring框架的两个核心,很好地实现了解耦合.所以,简单来说,Spring是一个轻量级的控制反转(IoC)和面向 ...

  10. Spring框架的IOC核心功能快速入门

    2. 步骤一:下载Spring框架的开发包 * 官网:http://spring.io/ * 下载地址:http://repo.springsource.org/libs-release-local/ ...

随机推荐

  1. win32 - Shell菜单项的创建

    #include <windows.h> #include <shobjidl_core.h> #include <windowsx.h> #include < ...

  2. 案例分享:Qt便携式致病菌快速检测仪(账号管理、实验过程、二维图表、历史数据、通讯管理、实验报告、中英文等等)

    需求   根据提供的用户原型设计.ui设计.通讯协议研发便携式致病菌快速检测仪器软件.  100%还原ui.   基本主功能(推荐visio:★★★☆☆,前期主流程需求整理)          Dem ...

  3. python中的泛型使用TypeVar

    引入为什么需要TypeVar PEP484的作者希望借助typing模块引入类型提示,不改动语言的其它部分.通过精巧的元编程技术,让类 支持[]运算不成问题.但是方括号内的T变量必须在某处定义,否则要 ...

  4. python selenium list index out of range

    常见错误原因 常见错误原因 其他错误原因 场景 使用selenium循环打开并跳转到新的网页,然后关闭新的窗口,然后回到原来窗口,这时候获取list中的值,报错: list index out of ...

  5. 【Azure 事件中心】使用Kafka的性能测试工具(kafka-producer-perf-test)测试生产者发送消息到Azure Event Hub的性能

    问题描述 Azure Event Hub支持 kafka,所以为了测试消息生产者所在环境与Azure Event  Hub之间发送消息的性能如何,特别使用 kafka 官方测试生产者,消费者的性能工具 ...

  6. Python基础之程序与用户交互

    [一]Python基础之程序与用户交互 [一]程序如何与用户交互 用户通过input命令在窗口内与输入就可以让用户和窗口进行交流 input接受的所有数据类型都是 str 类型 username = ...

  7. 【mac】Alfred 无法调用Terminal

    原因:是从mac app商店安装的Alfred class 解决方案:建议从官网下载安装:https://www.alfredapp.com/ 参考:https://www.alfredforum.c ...

  8. 脑电测量ADS1299芯片调试总结

    问题一:读出来ID不对? 笔者经过查阅官网资料和测试,发现这个一般是上电或者启动次序不对引起的. 特别是上电次序不同会导致这类问题. 问题二:内部时钟和外部时钟的选择是什么? 就拿内部时钟来说吧,首先 ...

  9. idea的使用操作

    怎么让idea中的方法之间显示横线 点击setting 选择Editor 选择Appearance 选择Show method separators 效果: 如果设置idea的标签页可以多行显示 点击 ...

  10. 《TencentNCNN系列》 之param文件(网络结构文件)格式分析

    PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明   本文作为本人csdn blog的主站的备份.(Bl ...