1.Spring简介:

a)Spring春天

b)官网:https://spring.io

c)设计理念:轮子理念,不要重复创造轮子;

d)Spring可以被理解为一个容器,用于管理其他的框架;

e)Spring个版本下载地址:https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring

2.Spring FrameworkRuntime

a) spring-aop-4.1.6.RELEASE.jar

面向切面编程, 必须导入的jar包

b) spring-aspects-4.1.6.RELEASE.jar

面向切面编程时依赖的jar包.

c) spring-beans-4.1.6.RELEASE.jar

是Spring的核心之一, 用于管理对象.

d) spring-context-4.1.6.RELEASE.jar

Spring的核心之一. 上下文, Spring上下文.

e) spring-context-support-4.1.6.RELEASE.jar

上下文依赖的jar包.

f) spring-core-4.1.6.RELEASE.jar

Spring的核心, 是Spring最基本的实现(支持)

g) spring-expression-4.1.6.RELEASE.jar

Spring的核心之一. 对el表达式的一个封装.

h) spring-jdbc-4.1.6.RELEASE.jar

Spring对jdbc封装的一个框架

i) spring-jms-4.1.6.RELEASE.jar

java message service. java消息服务, 消息队列.

j) spring-orm-4.1.6.RELEASE.jar

对orm框架的封装, 例如提供了对hibernate框架的支持.

k) spring-oxm-4.1.6.RELEASE.jar

object xml model. 小型的框架.

l) spring-test-4.1.6.RELEASE.jar

用于进行测试, 类似于JUnit的一个框架.

m) spring-tx-4.1.6.RELEASE.jar

用于使用配置文件实现对实务的管理(声明式事务)

n) spring-web-4.1.6.RELEASE.jar

Spring对javax.servlet包及子包中的接口或类的实现或子类.

  • o) spring-webmvc-4.1.6.RELEASE.jar

Spring提供的mvc控制器层框架. 必须依赖spring-web.jar.

支持注解开发, 使用非常简单.

p) spring-webmvc-portlet-4.1.6.RELEASE.jar

也是一个mvc框架, 使用global session取代了Application的概念.

q) spring-websocket-4.1.6.RELEASE.jar

可以支持通过socket技术实现web访问. 提供了一种可以自定义中间件的技术

3.Spring环境搭建

3.1导包

至少需要导入5个jar包

commons-logging-1.1.3.jar

spring-beans-4.1.6.RELEASE.jar

spring-context-4.1.6.RELEASE.jar

spring-core-4.1.6.RELEASE.jar

spring-expression-4.1.6.RELEASE.jar

3.2编写配置文件

a)Spring 可以看成一个容器,容器对象叫ApplicationContext,用于存放所有Spring管理的对象;

b)ApplicationContext是一个接口,通常我们使用的实现类叫ClassPathXmlApplicationContext,用于加载并解析配置文件。

c)xml配置文件命名其实是无所谓的,一般情况下,该配置文件的名为:applicationContext.xml,放在src目录下;

<?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: 唯一标识, 用于获取对象

class: 标识类的全限定路径

-->

<bean id="person" class="com.bjsxt.pojo.Person"></bean>

</ beans>

d)配置文件中通过schema约束xml的格式,根元素为<beans>,<bean>标签用于创建对象,一个<

bean>对应一个对象

3.3测试

Spring容器被创建时就创建对象,通过getBean方法可以获取该对象,通过getBeanDefinationNames可以获取所有Spring管理的对象的名字;

public static void main(String[] args) {

// 获取Spring容器对象

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

// 从容器中获取对象

Person person = context.getBean("person", Person.class);

person.show();

String[] names = context.getBeanDefinitionNames();

System.out.println(Arrays.toString(names));

context.close();

}

4。Ioc(inversion of control)

Ioc ,控制反转,控制指的是创建对象的操作,反转指的是将创建对象的操作交给spring来管理,由Spring来创建和管理对象的过程被称之为Ioc;

4.1 使用无参构造器

使用无参构造器(默认), 要求实体类必须提供无参构造器.

4.2 使用有参构造器

使用有参有参构造器, 需要通过constructor-arg标签配置

a) name: 参数名

b) index: 参数索引

c) type: 参数类型

d) value: 参数值, 用于基本数据类型和String

e) ref: 引用, 用于对象类型

<bean id="person2" class="com.bjsxt.pojo.Person">

<constructor-arg name="name" index="0" type="java.lang.String" value="张三" />

<constructor-arg name="age" index="1" type="java.lang.Integer" value="18" />

</bean>

4.3 使用静态工厂

获取对象的方法是静态方法的工厂称之为静态工厂

public class PersonFactory {

public static Person getBean() {

System.out.println("PersonFactory.getBean()");

return new Person();

}

}

在配置文件中, 通过class属性和factory-method属性指定使用静态工厂的对应方法创建对象

<bean id="person3" class="com.bjsxt.factory.PersonFactory" factory-method="getBean"></bean>

4.4 实例工厂(动态工厂)

获取对象的方法是实例方法的工厂称之为实例工厂(动态工厂)

public class Person2Factory {

public Person getBean() {

System.out.println("Person2Factory.getBean()");

return new Person();

}

}

在配置文件中先配置工厂对象, 再通过factory-bean指定工厂, 通过factory-method指定方法创建对象

<bean id="factory" class="com.bjsxt.factory.Person2Factory"></bean>

<bean id="person4" factory-bean="factory" factory-method="getBean"></bean>

1. DI(Dependency Injection)

依赖注入. 注入指的是给对象的属性赋值的过程; 依赖指的是依赖由Spring管理的bean对象. 是IoC的一部分.

5.1 构造器注入

在创建对象时, 通过构造器为对象的属性进行赋值. 参照IoC中的有参构造器的方式.

5.2 设值注入

通过调用setter方法为对象的属性赋值的过程.

<?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="person" class="com.bjsxt.pojo.Person">

<property name="name" value="李四"></property>

<property name="age">

<value>20</value>

</property>

<property name="list">

<list>

<value>aaa</value>

<value>bbb</value>

<value>ccc</value>

<value>ddd</value>

</list>

</property>

<property name="teacher" ref="teacher" />

</bean>

<bean id="teacher" class="com.bjsxt.pojo.Teacher">

<property name="name" value="张老师"/>

<property name="age" value="50"/>

</bean>

</beans>

2. Spring整合MyBatis

6.1 优点

a) 不需要再定义MyBatis的配置文件了

b) 很多对象不需要程序员去创建, 只需要给出配置即可

6.2 缺点

需要导入额外的jar包

asm-3.3.1.jar

cglib-2.2.2.jar

commons-logging-1.1.1.jar

javassist-3.17.1-GA.jar

jstl-1.2.jar

log4j-1.2.17.jar

log4j-api-2.0-rc1.jar

log4j-core-2.0-rc1.jar

mybatis-3.2.7.jar

mybatis-spring-1.2.3.jar

mysql-connector-java-5.1.30.jar

slf4j-api-1.7.5.jar

slf4j-log4j12-1.7.5.jar

spring-aop-4.1.6.RELEASE.jar

spring-beans-4.1.6.RELEASE.jar

spring-context-4.1.6.RELEASE.jar

spring-core-4.1.6.RELEASE.jar

spring-expression-4.1.6.RELEASE.jar

spring-jdbc-4.1.6.RELEASE.jar

spring-tx-4.1.6.RELEASE.jar

spring-web-4.1.6.RELEASE.jar

standard-1.1.2.jar

6.3 编写配置文件

Spring整合MyBatis的配置文件

<?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">

<!-- 配置数据源, 在spring-jdbc.jar中 -->

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver" />

<property name="url" value="jdbc:mysql://localhost:3306/java505" />

<property name="username" value="root" />

<property name="password" value="root" />

</bean>

<!-- 配置SqlSessionFactory对象, 在mybatis-spring.jar中 -->

<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">

<!-- 注入数据源 -->

<property name="dataSource" ref="dataSource" />

<!-- 给类定义别名 -->

<property name="typeAliasesPackage" value="com.bjsxt.pojo" />

</bean>

<!-- Mapper扫描器, 在mybatis-spring.jar中 -->

<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<!-- 指定到哪个包进行扫描 -->

<property name="basePackage" value="com.bjsxt.mapper" />

<!-- 指定SqlSessionFactory -->

<property name="sqlSessionFactory" ref="factory" />

</bean>

<!-- 配置service对象 -->

<bean id="studentService" class="com.bjsxt.service.impl.StudentServiceImpl">

<!-- 注入mapper对象 -->

<property name="studentMapper" ref="studentMapper" />

</bean>

</beans>

6.4 注意事项

a) service实现类中需要提供mapper对象, 并提供setter方法;

b) servlet中不能自己创建service对象, 应该从Spring容器中取对象, 否则会报空指针.

c) 可以配置监听器实现配置文件的加载, 优化Servlet的代码

<!-- 全局配置参数, 指定Spring配置文件的位置 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

<!-- 配置Spring的监听器 -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

@WebServlet("/student")

public class StudentServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

private StudentService studentService;

@Override

public void init() throws ServletException {

ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

studentService = context.getBean("studentService", StudentServiceImpl.class);

}

@Override

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

List<Student> list = studentService.selAll();

req.setAttribute("list", list);

req.getRequestDispatcher("/student.jsp").forward(req, resp);

}

}

Spring容器的更多相关文章

  1. 一则spring容器启动死锁问题(DefaultListableBeanFactory/DefaultSingletonBeanRegistry)

    线上发现一个问题,应用在启动时会卡死,log上并没有什么异常输出,初判应该是死锁问题. 抓现场的thread dump文件, 确实是有两个线程有deadlock问题. 线程一 "HSFBiz ...

  2. Spring容器深入(li)

    spring中最常用的控制反转和面向切面编程. 一.IOC IoC(Inversion of Control,控制倒转).对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的 ...

  3. 通过单元测试理解spring容器以及dubbo+zookeeper单元测试异常处理

    一.先说一个结论:单元测试与主项目的spring容器是隔离的,也就是说,单元测试无法访问主项目spring容器,需要自己加载spring容器. 接下来是代码实例,WEB主项目出于运行状态,单元测试中可 ...

  4. 同时使用Junit4的@Parameterized参数化测试和Spring容器

    转载:http://www.jianshu.com/p/d191fe54915f 整合Spring容器 @SpringApplicationConfiguration(classes = Applic ...

  5. Spring 容器

    Spring提供了两个核心接口:BeanFactory和ApplicationContext,其中applicationContext是BeanFactory的子接口. 他们都可代表Spring容器, ...

  6. Spring-Context之四:Spring容器及bean的定义

    Spring框架的核心功能之一就是控制反转(Inversion of Control, IoC),也叫做依赖注入(dependency injection, DI).关于依赖注入的具体内容可以参见Ma ...

  7. spring容器对bean生命周期的管理三中方式

    spring容器对bean的生命周期管理主要在两个时间点:bean的初始化完成(包括属性值被完全注入),bean的销毁(程序结束,或者引用结束)方式一:使用springXML配置中的init-meth ...

  8. spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)转

    关键字:spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件) 应用场景:很多时候我们想要在某个类加载完毕时干某件事情,但是使用了spring管理对象,我们这个类引用 ...

  9. 整合Servlet到Spring容器

    有时在Spring(3.2.5)项目中,如果使用到Servlet,可能希望Servlet实例作为bean受Spring容器管理,这样也能自动注入其他需要的bean,查了下,发现只针对过滤器提供了代理类 ...

  10. spring容器初始化执行某个方法

    在做web项目开发中,尤其是企业级应用开发的时候,往往会在工程启动的时候做许多的前置检查. 比如检查是否使用了我们组禁止使用的Mysql的group_concat函数,如果使用了项目就不能启动,并指出 ...

随机推荐

  1. Unable to preventDefault inside passive event listener due to target being treated as passive

    Unable to preventDefault inside passive event listener due to target being treated as passive 今天在做项目 ...

  2. Python档案袋( 面向对象 )

    类即是一个模型,根据模型建立起不同的对象,对象间拥有共同的一些属性 简单的类: class P: #类变量,所有实例共享变量,推荐使用方法是:类名.类变量名 pvarx="ppvar1&qu ...

  3. 【spring】ApplicationListener传递参数到页面(解决静态+动态资源路径+静态文件的缓存控制)

    一.相对路径还是绝对路径的问题 前端页面加载资源或者请求的时候到底是使用相对路径还是绝对路径,想必大家都很清楚,用的是当然是相对路径,因为这样增加了项目的灵活性,不需要经常的改动.那既然是相对路径就需 ...

  4. Spring设计模式_工厂模式

    先说下工厂模式的特性 1.对于调用者来说,影藏了复杂的逻辑处理过程,调用者只关心执行结果. 2.工厂要对结果负责,保证生产出符合规范的产品. Git代码地址  https://github.com/w ...

  5. Unity GC 优化要点

    参考:http://blog.csdn.net/znybn1/article/details/76464896 为啥要点?因为讲的重点. 游戏运行时来存储数据,当这些数据不再被使用时,存储这些数据的内 ...

  6. JS 中 原生方法 (一) --- 字符串

    目录 Javascript 中 str. arr.date.obj 等常见的原生方法总结 Javascript 中 str. arr.date.obj 等常见的原生方法总结 本文也说主要阐释了 Jav ...

  7. IDEA中MAVEN项目打JAR包的简单方法

      Idea中为一般的非Web项目打Jar包是有自己的方法的,网上一搜就能查到很多. 但是如果是为Maven项目打Jar包,其实是很简单的,因为maven本身就有打Jar包的命令.   最简单的方法 ...

  8. TCP/IP 四次断开

    网络连接状态 网络连接状态(11种)非常重要这里既包含三次握手中的也包括四次断开中的,所以要熟悉. LISTEN 被动打开,首先服务器需要打开一个socket进行监听,监听来自远方TCP端口的连接请求 ...

  9. ThreadLocal使用和原理简析

    1. 解决共享资源冲突 对于并发工作,需要某种方式来防止两个任务同时访问相同的资源,至少在关键阶段不能出现这种冲突情况. 方法之一就是当资源被一个任务使用时,在其上加锁.第一个访问某项资源的任务必须锁 ...

  10. RabbitMQ消息队列(七)-通过fanout模式将消息推送到多个Queue中(.Net Core版)

    前面第六章我们使用的是direct直连模式来进行消息投递和分发.本章将介绍如何使用fanout模式将消息推送到多个队列. 有时我们会遇到这样的情况,多个功能模块都希望得到完整的消息数据.例如一个log ...