欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring(二)IOC

程序员文章站 2022-07-12 13:10:11
...

传统获取对象

public class A{
    public void createObject(){
    //通过new来创建对象,是主动的创建
        C c = new C();
    }
}

Spring容器创建对象

  • 创建xml文件,把想要的对象的”注册信息”写好,然后让Spring容器生产
<?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:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">
      <!-- class包名+类名-->
      <bean id="c" class="com.spring.bean.C"></bean>
</bean>

然后我们需要实例化Spring容器,将生产好的对象取出来

    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
         C c = ac.getBean("c",C.class);  

原理:解析spring的配置文件,利用Java的反射机制创建对象。

Spring创建对象的方式有三种

  • 通过构造方法
    创建电脑类

    public class Computer{
        private String cpu;
        public void show(){
            System.out.println("我是I7处理器");
        }
    }

    创建applicationContext.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="c" class="com.spring.bean.Computer"></bean>
    </beans>         

    编写测试类

    public class TestBean {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
         Computer mac = ac.getBean("c",Computer.class);
         mac.show();
    }
    }
  • 静态工厂
    增加一个电脑工厂类
public class ComputerStaticFactory{
    public static Computer getInstance(){
        return new Computer();
    }
}

修改applicationContext.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="c" class="com.spring.bean.ComputerStaticFactory" factory-method="getInstance"></bean></beans>
  • 实例工厂
    创建非静态工厂类
public class ComputerFactory{
    public Computer getInstance(){
        return new Computer();
    }
}

修改applicationContext.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="ComputerFactory" class="com.spring.bean.ComputerFactory"/>
    <bean id="c" factory-bean="ComputerFactory" factory-method="getInstance"></bean>
</beans>

bean的作用域

默认是单例模式,测试bean是否是单例

public class TestBean {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
         Computer mac = ac.getBean("c",Computer.class);
         Computer windows = ac.getBean("c",Computer.class);
         System.out.println(windows == mac);
    }
}
//输出结果为true

在applicationContext.xml bean属性中增加scope=”prototype”,执行测试类得到false。

bean的生命周期

  1. Spring容器在创建bean时,可以帮忙管理bean的生命周期,即管理bean的初始化及销毁的方法。
  2. 默认情况下Spring容器创建时,会创建单例的对象,也可以让Spring容器延迟实例化这些单例的对象

维护对象关系(DI)

传统的方法给属性赋值

package com.spring.bean;

public class Computer {
    private String cpu;//cpu
    private String hdd;//硬盘
    private String ram;//内存
    public String getCpu() {
        return cpu;
    }
    public void setCpu(String cpu) {
        this.cpu = cpu;
    }
    public String getHdd() {
        return hdd;
    }
    public void setHdd(String hdd) {
        this.hdd = hdd;
    }
    public String getRam() {
        return ram;
    }
    public void setRam(String ram) {
        this.ram = ram;
    }
    public void show() {
        System.out.println("cpu型号是"+cpu);
        System.out.println("hdd型号是"+hdd);
        System.out.println("ram型号是"+ram);
    }
}
package com.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.bean.Computer;
import com.spring.bean.ExampleBean;

public class TestBean {
    public static void main(String[] args) {
    Computer c = new Computer();
    c.setCpu = "i7";
    c.setRam = "金士顿";
    c.setHdd = "三星";
    }
}

Spring容器给属性注入值

setter注入

创建电脑类Computer,代码如下:

package com.spring.bean;

public class Computer {
    private String cpu;//cpu
    private String hdd;//硬盘
    private String ram;//内存
    public String getCpu() {
        return cpu;
    }
    public void setCpu(String cpu) {
        this.cpu = cpu;
    }
    public String getHdd() {
        return hdd;
    }
    public void setHdd(String hdd) {
        this.hdd = hdd;
    }
    public String getRam() {
        return ram;
    }
    public void setRam(String ram) {
        this.ram = ram;
    }
    public void show() {
        System.out.println("cpu型号是"+cpu);
        System.out.println("hdd型号是"+hdd);
        System.out.println("ram型号是"+ram);
    }
}

在applicationContext.xml中声明这个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">
<bean id="computer" class="com.spring.bean.Computer">
    <property name="cpu" value="i7"></property>
    <property name="hdd" value="三星"></property>
    <property name="ram" value="金士顿"></property>
</bean>
</beans>       

测试

package com.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.bean.Computer;
import com.spring.bean.ExampleBean;

public class TestBean {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Computer c = ac.getBean("computer",Computer.class);
        c.show();
    }
}

运行结果Spring(二)IOC

构造器注入

创建手机类

package com.spring.bean;

public class Phone {
    private String color;
    private int price;
    public Phone(String color,int price) {
        this.color = color;
        this.price = price;
    }
    public void show() {
        System.out.println("手机颜色是:"+color);
        System.out.println("手机价格是:"+price);
    }
}

applicationContext.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="phone" class="com.spring.bean.Phone">
<constructor-arg index="0" value="红色"></constructor-arg>
<constructor-arg index="1" value="2300"></constructor-arg>
</bean>
</beans>    

测试

package com.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.bean.Computer;
import com.spring.bean.ExampleBean;
import com.spring.bean.Phone;

public class TestBean {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Phone p = ac.getBean("phone",Phone.class);
        p.show();
    }
}

运行结果:Spring(二)IOC

DI依赖注入的值有很多类型,包括对象注入、List、Set、表达式注入等。
例如集合注入

<?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="phone" class="com.spring.bean.Phone">
<!-- 构造器注入 -->
<constructor-arg index="0" value="红色"></constructor-arg>
<!-- list注入 -->
<property name="style">
    <list>
        <value>iphone</value>
        <value>华为</value>
    </list>
</property>
<!-- 对象注入 -->
<property name="style" ref="computer"></property>
</bean>
</beans>    

spring注解

上面我们是通过xml配置来实现IOC的,接下来我们使用注解来是现实。

  • 组件自动扫描
    可以按照指定的包路径,将包下所有组件扫描,如果发现组件类定义前有以下标记,会将组件扫描到Spring容器。
    @Component //其他组件
    @Controller //控制器组件
    @Service //业务层组件
    @Repository //数据访问层组件
    @Named(需要引入第三方标准包)
    示例:
    创建一个ExampleBean,且添加注解
package com.spring.bean;

import org.springframework.stereotype.Component;
//扫描ExampleBean组件,也可以指定id  @Component("exampleBean")
@Component
@Scope("prototype")
public class ExampleBean {
    public void execute() {
        System.out.println("执行execute方法");
    }
}

创建applicationContext.xml,开启注解

 <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 开启组件扫描 -->
<context:component-scan base-package="com.spring"></context:component-scan>
</beans>   

编写测试类

package com.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.bean.Computer;
import com.spring.bean.ExampleBean;
import com.spring.bean.Phone;

public class TestBean {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        ExampleBean eb = ac.getBean("exampleBean",ExampleBean.class);
        eb.execute();
    }
}

运行结果:Spring(二)IOC

  • 对象注入
    @Resource(name=”指定名称”)
    @Autowired
    直接在属性上面编写即可,不再赘述。
    Spring(二)IOC
相关标签: spring