spring之入门ioc(控制反转)di(依赖注入)

spring是一个容器管理框架。
重点:

  • ioc 控制反转,以往对象的创建式通过new方式,现在对应都由spring管理通过反射形式将对象初始化放在spring容器中。
  • di 依赖注入,bean与bean之间根据依赖关系进行引用,之所以引入这个名词,应该是bean之间相互依赖的关系更能体现spring容器管理的特点。例如在下面例子中UserInfoService对象需要初始化User对象。
  • applicationContext.xml 中将对象配置到bean标签下,value指定的是示例参数,ref指定的是引用(例如在某个对象中需要使用另一个对象)。
  • 使用ClassPathXmlApplicationContext加载并初始化bean容器(可以理解成解析xml文件,通过Java反射机制初始化对象放在类似Hashmap中,例 user = Class.forName("com.hiro.entity.User");并设置属性,放入类似Hashmap中),当获取时候通过id获取即可。
  • ClassPathXmlApplicationContext操作是占用内存的,所以保证ApplicationContext是单态的。
  • 通过一个例子直观看一下。
    • 1.maven工程目录
    • 2.pom.xml添加依赖
    • 3.创建几个对象
    • 4.创建applicationContext.xml
    • 5.创建bean容器,测试获取bean
    • 6.修改获取单态ApplicationContext
1 maven 工程目录:

在这里插入图片描述

2 添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hiro</groupId>
    <artifactId>springStart</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
    </dependencies>
</project>
3 创建两个bean
package com.hiro.entity;

/**
 * Project: springStart
 *
 * @author : hirolin
 * @date : 2019/5/18 12:20
 */
public class User {
    private String name;

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}
package com.hiro.service;

import com.hiro.entity.User;

/**
 * Project: springStart
 *
 * @author : hirolin
 * @date : 2019/5/18 12:19
 */
public class UserInfoService {

    private String userFrom;

    private User user;

    public String getUserFrom() {
        return userFrom;
    }

    public void setUserFrom(String userFrom) {
        this.userFrom = userFrom;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void say() {
        System.out.println(userFrom);
        System.out.println(user.toString());
    }
}
4 创建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="user" class="com.hiro.entity.User">
        <property name="name" value="小花"/>
    </bean>

    <bean id="userInfoService" class="com.hiro.service.UserInfoService">
        <property name="userFrom" value="web"></property>
        <property name="user" ref="user"></property>
    </bean>
</beans>
5 创建bean容器,测试获取bean
package com.hiro.test;

import com.hiro.service.UserInfoService;
import com.hiro.utils.ApplicationContextUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Project: springStart
 *
 * @author : hirolin
 * @date : 2019/5/18 12:23
 */
public class Test {

    public static void main(String [] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        UserInfoService userInfoService = (UserInfoService) applicationContext.getBean("userInfoService");
        userInfoService.say();
    }
}
6.修改获取单态ApplicationContext

使用饿汉式单例模式,使用final修改class防止被修改

package com.hiro.utils;

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

/**
 * Project: springStart
 *
 * @author : hirolin
 * @date : 2019/5/18 12:34
 */
//使用final修饰 保证class不被继承修改
final public class ApplicationContextUtils {

    private static ApplicationContext applicationContext = null;

    //创建空的构造器
    public ApplicationContextUtils() {

    }

    //饿汉式单例 类文件加载的时候就会初始化
    static {
        applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    }

    public static ApplicationContext getApplicationContext(){
        return applicationContext;
    }
}

测试:

package com.hiro.test;

import com.hiro.entity.User;
import com.hiro.service.UserInfoService;
import com.hiro.utils.ApplicationContextUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Project: springStart
 *
 * @author : hirolin
 * @date : 2019/5/18 12:23
 */
public class Test {

    public static void main(String [] args) throws ClassNotFoundException {
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//        UserInfoService userInfoService = (UserInfoService) applicationContext.getBean("userInfoService");
//        userInfoService.say();
        UserInfoService userInfoService = (UserInfoService) ApplicationContextUtils.getApplicationContext().getBean("userInfoService");
        userInfoService.say();
    }
}

源码

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值