spring框架详解(一)--IOC与注入

本文介绍了Spring框架的基础概念,包括其轻量级特性、依赖注入(DI)、面向切面编程(AOP),并详细讲解了IOC容器及Bean管理,以及如何通过设置注入和构造注入等方式进行依赖管理。

一.spring框架简介

    Spring 是一个 IOC(DI) 和 AOP 容器框架.
    具体描述:
       轻量级:Spring 是非侵入性的 - 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API
       依赖注入(DI --- dependency injection、IOC)
       面向切面编程(AOP --- aspect oriented programming)
       容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期
       框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 Java 注解组合这些对象
      一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC)

二.框架的核心原理

spring框架的核心是IOC 与AOP,当你了解这两个核心部分,那么你对整个spring的原理就有基本的了解了.

IOC:(全称:Inverse Of Control )控制反转,容器主动将资源推送给它所管理的组件,组件所做的是选择一种合理的方式接受资源。
简单的理解:把创建对象和维护之间的关系的权利由程序中转移到Spring容器的配置文件中。
DI:(全称:Dependency Injection)依赖注入,IOC的另一种表现方式.所谓的依赖注入,就是IOC容器在运行期间,动态的将某种依赖关系注入到对象之中.
---------------------------------------------------------------------------------------------------------------------------------------------------------
AOP面向切面编程
a.静态代理
根据每个具体类分别编写代理类
根据一个接口编写一个代理类
b.动态代理
针对一个方面编写一个InvocationHandler,然后借用JDK反射包中的Proxy类为各种接口动态生成相应的代理类
那么我们下面分别来梳理下这几个核心部分的东西:

三.IOC与bean容器

先建立个简单的获取bean案例:
步骤一:导入该导入的包,这里只需要导入(我这里用的是maven工程)
   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.10.RELEASE</version>
   </dependency>
   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.3.10.RELEASE</version>
   </dependency>
   <!-- 单元测试包 -->
   <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
步骤二:写一个接口与它的实现类
public interface TestInter {
public void show(String str);
}

public class TestImp implements TestInter {
	public void show(String str) {
		System.out.println(this.hashCode());
	}
}

为什么在spring框架里要定义接口呢?

  框架的目的本质就是为了实现解耦合,灵活性,便于维护和后期修改的。所以定义接口就是为了不同实现,工作中特别是大型项目就是为了后期的容易修改,不定义接口你想象下后期如果有需要修改项目是有多麻烦.

新建一个spring-bean.xml,(这个名字按自己习惯取)
<bean id="TestImp" class="com.eduask.chp.test.TestImp"></bean>

步骤三:单元测试方法
public class Junit {
	static ApplicationContext app;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		app=new ClassPathXmlApplicationContext("spring-bean.xml");//创建了一个IOC容器
	}
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		((AbstractApplicationContext) app).destroy();
	}
	@Test
	public void test() {
		TestInter t=(TestInter) app.getBean("TestImp");//获取容器中的对象
		t.show("你好");//调用方法
	}

}

补充一点:ioc容器应用了单例模式,一个容器中只允许出现一份,可以做过测试,你在new 一个容器中的对象,用==判断下你会发现是true.

四.spring注入

 spring注入是在spring容器在加载bean配置的时候,完成对变量的赋值行为
常用的两种注入方式:
设置注入
web项目开发至少分三层,业务层,dao层,控制层
这里写个业务层与dao层的注入关系
dao接口:
public interface TestDao {
public void Save(String str);
}
dao实现类:
public class TestDaoImp implements TestDao{
	public void Save(String str) {
	System.out.println("这是dao层要保存的数据");
	}
}
service接口 :
public interface TestService {
public void Save(String str);
}
service实现类:
public class TestServiceImp implements TestService{
private TestDao testDao;
//设置注入一定要set方法,可以不用get方法
public void setTestDao(TestDao testDao) {
	this.testDao = testDao;
}
	public void Save(String str) {
		 System.out.println(str);
		 testDao.Save(str);
	}
}
bean配置
<bean id="testService" class="com.eduask.chp.test.TestServiceImp">
<property name="testDao" ref="testDao"></property>
</bean>
<bean id="testDao" class="com.eduask.chp.test.TestDaoImp"/>
测试一下:
public void test() {
		TestService ts=(TestService) app.getBean("testService");
	        ts.Save("这是service接受的数据");
	}


构造注入
将上面的service的set改为构造方法,如下所示
public class TestServiceImp implements TestService{
private TestDao testDao;
//构造器注入
public TestServiceImp(TestDao testDao) {
	this.testDao = testDao;
}
	public void Save(String str) {
		 System.out.println(str);
		 testDao.Save(str);
	}

}

xml文件改为
<bean id="testService" class="com.eduask.chp.test.TestServiceImp">
<constructor-arg name="testDao" ref="testDao"></constructor-arg>
</bean>
<bean id="testDao" class="com.eduask.chp.test.TestDaoImp"/>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值