Spring-ioc

一. 什么是spring,它能够做什么?
Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

  1. 中间层框架、万能胶
    struts2
    spring
    hibernate
    2 容器框架
    JavaBean 项目中的一个个类
    IOC和AOP

spring包含的模块在这里插入图片描述

二 、Spring-IoC入门

  1. 什么是IoC控制反转(或依赖注入)

控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)” ,即由容器动态的将某种依赖关系注入到组件之中
案例:实现Spring的IoC

IOC/DI
将以前由程序员实例化对象/赋值的工作交给了spring处理

下面举一个简单的demo供大家理解,代码如下:

IUserDao.java

public interface IFileDao {
 
	void upload();
}

UserDaoImpl.java

public class FileDaoImpl implements IFileDao{
 
	@Override
	public void upload() {
            System.out.println("文件上传!");
	}
}

假如在项目实际开发中有个上传文件的接口FileService,在这个接口中要实现文件上传,需要通过调用FileDaoImpl中的upload()方法来实现。
我们的正常做法是在FileService中先new FileDaoImpl(),然后再调用FileDaoImpl中相关方法。

正常做法的代码如下:

public class FileService{
 
    private IFileDao ifd=new FileDaoImpl();
 
    public void upload(){
        ifd.upload();
    }
}

而IoC的思想即是,在你需要依赖对象的时候,IoC容器即可提供一个已经实例化好的对象给你,这样你就不需要自己去新建相应的依赖类。
看下面这段代码,来感受下IoC的过程:

FileService中声明的ifd无需再实例化,但是要提供set方法

public class FileService {
 
	private IFileDao ifd;
        //set方法注入
	public void setIfd(IFileDao ifd) {
		this.ifd = ifd;
	}
 
	public void upload() {
		System.out.println(ifd.upload());
	}
}

2.在spring的配置文件spring-context.xml中配置FileDaoImpl 与FileService(spring-context.xml配置文件是自己创建的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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
        
	<bean class="com.zking.biz.impl.FileDaoImpl" id="ifd"></bean>
	<bean id="fileService" class="com.zking.action.FileService">
            <!-- 
                set方法注入
                name 为FileService中声明的接口的名字
                ref 指依赖谁,这里的值为上面配置的FileDaoImpl的id
              -->
            <property name="ifd" ref="ifd"></property>
        </bean>
</beans>

测试方法调用FileService的upload方法:

public class FileServiceTest {
 
	@Test
	public void testUpload() {
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-context.xml");
		FileService fileService = (FileService)applicationContext.getBean("fileService");
		fileService.upload();
	}
}

2.依赖注入的3种方式
· set方法注入

上面的实例用的就是set方法注入

· 构造方法注入

FileService.java中将set方法改为构造函数

public class FileService {
 
	private IFileDao ifd;
	public FileService(IFileDao ifd) {
		this.ifd = ifd;
	}
	public void upload() {
		System.out.println(iuda.upload());
	}
}

spring-context.xml中将property标签改为constructor-arg标签

<?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:aop="http://www.springframework.org/schema/aop"
	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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
        
	<bean class="com.zking.biz.impl.FileDaoImpl" id="ifd"></bean>
	<bean id="fileService" class="com.zking.action.FileService">
            <!-- 
                构造方法注入 
                name 为FileService中声明的接口的名字
                ref 指依赖谁,这里的值为上面配置的FileDaoImpl的id
              -->
            <constructor-arg name="ifd" ref="ifd"></constructor-arg>
        </bean>
</beans>

· 自动装配

FileService.java中提供set方法

public class FileService {
 
	private IFileDao ifd;
        //自动装配
	public void setIfd(IFileDao ifd) {
		this.ifd = ifd;
	}
 
	public void upload() {
		System.out.println(ifd.upload());
	}
}

spring-context.xml中的beans标签中设置 default-autowire 属性,默认值为default,注释中对default-autowire的值有解释:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        default-autowire="byName"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
        
        <!-- 
		byType:根据javabean的接口属性,在spring的上下文中自动寻找实现类去注入,当找到两个或两个以上时会报错,与spring的上下文的id无关
		
		byName:根据管理的javabean中的接口名,在spring上下文中去寻找同名的id进行注入。
	 -->
	<bean class="com.zking.biz.impl.FileDaoImpl" id="ifd"></bean>
	<bean id="fileService" class="com.zking.action.FileService"></bean>
 
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值