spring 第二课

Spring IOC

准备pojo Category,用来演示IOC和DI

注入属性

package pojo;


public class Category {

    private int id;
    private String name;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Category{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

配置applicationContext.xml文件
applicationContext.xml是Spring的核心配置文件,通过关键字c即可获取Category对象,该对象获取的时候,即被注入1 到id

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <bean name="category" class="pojo.Category">
        <property name="id" value="1" />
    </bean>
</beans>

测试代码,演示通过spring获取Category对象,以及该对象被注入的id属性。

public class test {
    public static void main(String[] args) {
        //初始化spring容器applianceContext,加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
        Category category=(Category)  context.getBean("category");
        System.out.println(category.getId());
    }
}

结果
在这里插入图片描述传统的方式:
通过new 关键字主动创建一个对象
IOC方式:
对象的生命周期由Spring来管理,直接从Spring那里去获取一个对象。 IOC是反转控制 (Inversion Of Control)的缩写,就像控制权从本来在自己手里,交给了Spring。

注入对象

创建Product类

public class Product {
    private int id=;
    private String name;
    private Category category;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", category=" + category +
                '}';
    }
    
}

配置applicationContext.xml
用ref注入对象

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <bean name="category" class="pojo.Category">
        <property name="id" value="1" />
    </bean>
    <bean name="product" class="pojo.Product">
        <property name="id" value="2" />
        <property name="category" ref="category" />
    </bean>
</beans>

通过Spring拿到的Product对象已经被注入了Category对象了

public class test {
    public static void main(String[] args) {
        //初始化spring容器applianceContext,加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
        Product product=(Product) context.getBean("product");
        System.out.println(product.getId());
        System.out.println(product.getCategory().getId());
    }
}

在这里插入图片描述

使用注解的方式完成对象注入

修改applicationContext.xml
添加context:annotation-config/

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/>
    <bean name="category" class="pojo.Category">
        <property name="id" value="1" />
    </bean>
    <bean name="product" class="pojo.Product">
        <property name="id" value="2" />
        <!--<property name="category" ref="category" />-->
    </bean>
</beans>

在Product.java的category属性前加上@Autowired注解
在这里插入图片描述
运行结果
在这里插入图片描述
结果相同

使用@Resource注解也是可以的,但是要加上name,必须和bean配置的name一致

在这里插入图片描述

对bean进行注解

修改applicationContext.xml
添加<context:component-scan base-package=pojo"/>
作用是告诉Spring,bean都pojo这个包下

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="pojo"/>
</beans>

为Product、Category加上@Component注解,即表明此类是bean
属性初始化放在属性声明上进

在这里插入图片描述
在这里插入图片描述
运行结果一致
在这里插入图片描述

总结

本次课程运用实例解释了IOC和DI,学习了注的两种方式,构造方法注入,基于注解的注入。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值