Spring 之 IOC

本文介绍了Spring的IOC(控制反转)概念,指出DI是其实现方式,目的是创建和组装对象关系。还阐述了Bean容器初始化,涉及两个基础包及初始化ApplicationContext的三种方式。此外,讲解了Spring注入概念和常用的设值、构造两种注入方式,并给出示例及注意事项。

1. 什么是IOC?

IOC : 控制反转, 控制权的转移, 应用程序本身不负责依赖对象的创建和维护, 而是有外部容器负责创建和维护

DI(依赖注入)是其一种实现方式

目的:创建对象并且组装对象之间的关系

 

事例:

IOC房屋中介

2. Bean容器初始化

基础:两个包

-- org.springframework.beans

-- org.springframework.context

-- BeanFactory提供配置结构和基本功能,加载并初始化Bean

-- ApplicationContext保存了Bean对象并在Spring中被广泛使用

初始化ApplicationContext的三种方式:

-- 加载本地文件

-- 通过Classpath(相对路径)加载文件

-- Web应用中依赖servlet或Listener

 

3. Spring注入

概念: Spring注入是指在启动Spring容器加载Bean配置的时候,完成对变量的赋值行为

常用的两种注入方式:

--  设值注入

--  构造注入

示例:

所需jar包

 

package dao;

public interface InjectDao {
	public void save(String data);
}
package dao;

public class InjectDaoImpl implements InjectDao{

	@Override
	public void save(String data) {
		// TODO 自动生成的方法存根
		System.out.println("注入方式 --->" + data);
	}
}
package services;

public interface InjectService {
	public void save(String data);
}

 

 

package base;

import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;

public class Base {
	private ClassPathXmlApplicationContext context;
	
	private String springXmlpath;
	
	public Base() {}
	
	public Base(String springXmlpath) {
		this.springXmlpath = springXmlpath;
	}
	
	@Before
	public void before() {
		if (StringUtils.isEmpty(springXmlpath)) {
			springXmlpath = "classpath*:spring-*.xml";
		}
		try {
			context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
			context.start();
		} catch (BeansException e) {
			e.printStackTrace();
		}
	}
	
	@After
	public void after() {
		context.destroy();
	}
	
	@SuppressWarnings("unchecked")
	protected <T extends Object> T getBean(String beanId) {
		try {
			return (T)context.getBean(beanId);
		} catch (BeansException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	protected <T extends Object> T getBean(Class<T> clazz) {
		try {
			return context.getBean(clazz);
		} catch (BeansException e) {
			e.printStackTrace();
			return null;
		}
	}
}

 

 

 

 

 

 

package services;

import dao.InjectDao;

public class InjectServiceImpl implements InjectService{

	private InjectDao dao;
	
	/*//设值注入
	public void setDao(InjectDao dao) {
		this.dao = dao;
	}*/
	
	//构造注入
	public InjectServiceImpl(InjectDao dao){
		this.dao = dao;
	}
	
	@Override
	public void save(String data) {
		// TODO 自动生成的方法存根
		//模拟业务操作
		System.out.println("参数接收   ---> " + data);
		data = data + " --" + this.hashCode();
		dao.save(data);
	}
}


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="injectService" class="services.InjectServiceImpl">
      <!-- 设值注入 -->
       <!-- <property name="dao" ref="dao"></property> -->
       <!-- 依赖注入 -->
       <constructor-arg name="dao" ref="dao"></constructor-arg>
      </bean>
      <bean id="dao" class="dao.InjectDaoImpl"></bean>
</beans>

 

 

测试类

 

package test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import services.InjectService;
import services.InjectServiceImpl;
import base.Base;

@RunWith(BlockJUnit4ClassRunner.class)
public class InjectTest  extends Base{
	//加载配置文件
	public InjectTest(){
		super("classpath:spring-injection.xml");
	}
	
	//设值注入
	@Test
	public void test(){
		InjectService is = super.getBean("injectService");
		//is.save("设值注入");
		is.save("构造注入");
	}
	
}

注: 我这里使用的是单元测试,相关jar包和代码已显示,需要注意的是:bean容器中的bean多对应的id必须和实例化的变量名一致,否则会报错,配置文件必须放在src目录下

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值