您的位置:首页 > 编程语言 > Java开发

学习笔记-Spring入门(一)

2017-03-01 22:09 423 查看
一、框架

1、什么是框架

框架就是制定一套规范或者规则(思想),程序员在该规范或者规则(思想)下工作。或者说即使使用别人搭好的舞台来做表演。

2、框架的特点

半成品

封装了特定的处理流程和控制逻辑

成熟的、不断升级改进的软件

3、框架与类库的区别

框架一般封装了逻辑、高内聚的,类库是松散的工具组合。

框架专注于某一领域,类库则是更通用的。

二、接口及面向接口编程

1、接口

用于沟通的中介物的抽象化;

实体把自己提供给外界的一种抽象化说明,用以由内部操作分离出外部沟通的方法,使其能被修改内部而不影响外界其他实体与其交互的方式。

对应的 java接口及声明,声明了哪些方法是对外公开提供的

2、接口、抽象类、普通的类

接口:只能有声明不能有实现,支持多重继承

抽象类:既可以有像接口一样的声明,又可以有实现

普通的类:只有具体实现,不支持多重继承

3、面向接口编程

结构设计中,分清层次机调用关系,每层值向外(上层)提供一组功能接口,各层间仅依赖接口而非实现类。

接口实现的变动不影响各层间的调用。

“面向接口的编程”中的“接口”是用于隐藏具,体实现和实现多态性的组件。

三、IOC

1、什么是IOC

IOC:控制反转(获得依赖对象的过程被反转了),控制权的转移,应用程序本身不负责依赖对象的创建和维护,而由外部容器负责创建和维护。

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

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

IOC容器在初始化的时候会创建一系列的对象,并把这种对象关系通过注入的方式组织起来。

2、IOC与房屋中介进行对比

(1)房屋中介:找中介

中介介绍房子

租房、入住

(2)IOC:找IOC容器

容器返回对象

使用对象

(3)spring_ioc.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-2.5.xsd"> <!--
将如下的三个bean纳入到spring容器管理,
(实际注入的是各个接口的实现类,毕竟只有实现类才真正实现具体方法(虽然在bean中看起来是接口形式声明的属性))
personAction
personServiceImpl   (面向接口编程:所以简写为接口名称)
personDaoImpl       (面向接口编程:所以简写为接口名称)
-->
<bean id="personDao" class="lxk.test.spring.mvc.PersonDaoImpl"/>

<bean id="personService" class="lxk.test.spring.mvc.PersonServiceImpl">
<property name="personDao" ref="personDao"/>
</bean>

<bean id="personAction" class="lxk.test.spring.mvc.PersonAction">
<property name="personService" ref="personService"/>
</bean>


自从有了IOC后不用自己去创建对象了,IOC机制就提供了。

三、单元测试

1、

- 下载junit-*.jar并引入工程;

- 创建UnitTestBase类,完成对Spring配置文件的加载、销毁;

- 所有单元测试类都继承自UnitTestBase,通过它的getBean方法获取想要得到的对象;

- 子类(具体的执行单元测试的类)加注解:

@RunWith(BlockJUnit4ClassRunner.class)

- 单元测试方法加注解:@Test

- 右键选择要执行的单元测试方法执行或者执行一个类的全部单元测试方法。

import org.apache.commons.lang.StringUtils;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import    org.springframework.context.support.ClassPathXmlApplicationContext;
public class UnitTestBase {

private ClassPathXmlApplicationContext context;
private String springXmlpath;

public UnitTestBase(){}

public UnitTestBase(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();
}
}


四、Bean容器的初始化

1、基础:两个包

- org.springframework.beans

- org.springframework.connext

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

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

2、初始化ApplicationConnext的方式

本地文件

FileSystemXmlApplicationContext context=new FileSystemXmlApplicationContext("F:workspace/appcontext.xml");


Classpath

FileSystemXmlApplicationContext context=new FileSystemXmlApplicationContext("classpath:spring-connext.xml");


Web应用中依赖servlet或者Listener

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet<servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>


五、spring注入

1、spring注入是指在启动spring容加载bean配置的时候,完成对变量的赋值行为。

2、常用的两种注入方式:

(1)设值注入

<!-- 下面两个bean是设值注入,第二个bean的意思是在类PersonServiceImpl中有一个名为 personDao的属性,
而这个属性的类型是PersonDaoImpl这个类型。第一个Bean的id要与第二个Bean的ref相同。通过set的方式去注入,
所以PersonServiceImpl中要有一个setPersonDao的方法 -->
<bean id="personDao" class="lxk.test.spring.mvc.PersonDaoImpl"/>

<bean id="personService" class="lxk.test.spring.mvc.PersonServiceImpl">
<property name="personDao" ref="personDao"/>
</bean>


(2)构造注入

<!--   构造器注入 -->
<bean id="personDao1" class="lxk.test.spring.mvc.PersonDaoImpl1"/>

<bean id="personService1" class="lxk.test.spring.mvc.PersonServiceImpl1">
<constructor-arg name="personDao1" ref="personDao1"></constructor-arg>
</bean>


类PersonDaoImpl1中要有一个如下的构造方法:

//构造器注入
public void PersonDaoImpl1(PersonDao1,personDao1){
this.personDao1=personDao1;
}


注:对应名称必须保持一致。

9d43
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息