Spring框架入门

xiaoxiao2021-02-27  360

一,概述

1)专业术语了解

组件/框架设计

a.侵入式设计:引入了框架,对现有的类的结构有影响,即需要实现或者继承某些特定类.

eg:struts框架,继承ActionSupport

b.非侵入式设计:引入框架,对现有类的结构没有影响.

eg:Hibernate框架,Spring框架

控制反转

Inversion on Control,控制反转IOC,对象的创建交给外部容器完成,这个就叫做控制反转.

依赖注入

Dependency Injection,处理对象的依赖关系.

控制反转与依赖注入的区别?

控制反转:解决对象的创建问题[对象创建交给别人].

依赖注入:在创建完对象后,对象关系的处理就是依赖注入[通过set方法依赖注入].

AOP

面向切面编程,切面可以简单理解为一个类,由许多重复代码形成的类.切面举例:事务 日志 权限

二,Spring框架

1)概述

Spring框架可以解决对象创建以及对象之间依赖关系的一种框架.同时,Spring框架可以与其他框架一起使用,如Spring与Struts Spring与Hibernate.可以这么说,Spring是一个起整合作用的框架.

Spring提供了一站式解决方案:

a)Spring Core:Spring的核心功能,IOC容器解决对象创建及依赖关系.

b)Spring Web:Spring对web模块的支持.

-->可以与Struts整合,让Struts的Action创建交给Spring.

-->Spring MVC模式.

c)Spring DAO:Spring对Jdbc操作的支持.[JdbcTemplate]模板工具类.

d)Spring ORM:Spring对ORM的支持.

-->Spring既可以与Hibernate整合,也可以使用Spring对Hibernate操作的封装.

e)Spring AOP:切面编程.

f_SpringEE:Spring对JavaEE其他模块的支持.

2)开发步骤

在Spring3.0以下的版本,源码有Spring相关的所有包[Spring功能+依赖包],如2.5版本.

在Spring3.0以上的版本,源码中 只有Spring功能包[没有依赖包],如果要使用依赖包,需要单独下载.

a) 源码, jar文件:spring-framework-3.2.5.RELEASE commons-logging-1.1.3.jar           日志(自己单独下载导入) spring-beans-3.2.5.RELEASE.jar        bean节点 spring-context-3.2.5.RELEASE.jar       spring上下文节点 spring-core-3.2.5.RELEASE.jar         spring核心功能 spring-expression-3.2.5.RELEASE.jar    spring表达式相关表 以上是必须引入的5个jar文件,在项目中可以用户库管理!

b)核心配置文件:applicationContext.xml

Spring配置文件:applicationContext.xml/bean.xml

约束参考:约束参考:spring-framework-3.2.5.RELEASE\docs\spring-framework-reference\htmlsingle\index.html

<?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:p="http://www.springframework.org/schema/p" 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/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>

3)我的第一个Spring程序

a)实体类

package com.bighuan.a_hello; public class User { public User(){ System.out.println("创建user对象"); } private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void init_user(){ System.out.println("在创建user对象后初始化"); } public void destroy_user(){ System.out.println("IOC容器销毁,user对象回收"); } } b)配置bean

<?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:p="http://www.springframework.org/schema/p" 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/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- IOC容器的配置:要创建的所有对象都在这里配置 --> <bean id="user" class="com.bighuan.a_hello.User" init-method="init_user" destroy-method="destroy_user" scope="singleton" lazy-init="false" ></bean> <!--<bean id="user" class="com.bighuan.a_hello.User" scope="prototype"></bean>--> </beans>

c)代码测试:获取IOC容器对象,从容器中获取bean

package com.bighuan.a_hello; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class App1_get_ioc { /** * 通过工厂类得到IOC容器创建的对象 * * @throws Exception */ @Test public void testIOC() throws Exception { // 创建对象 // User user=new User(); // 现在,把对象的创建交给spring的IOC容器 Resource resource = new ClassPathResource( "com/bighuan/a_hello/applicationContext.xml"); // 创建容器对象(bean的工厂),IOC容器=工厂类+applicationContext.xml BeanFactory fac = new XmlBeanFactory(resource); // 得到容器创建的对象 User user = (User) fac.getBean("user"); System.out.println(user); } /** * 直接得到IOC容器对象 * * @throws Exception */ @Test public void testAc() throws Exception { // 直接得到IOC容器对象 ApplicationContext ac = new ClassPathXmlApplicationContext( "com/bighuan/a_hello/applicationContext.xml"); // 得到容器创建的对象 User user = (User) ac.getBean("user"); System.out.println(user.getId()+","+user.getName()); } } 运行测试代码,只要环境配置正确,文件配置正确,就可以输出对象地址了.

三,bean对象创建的细节

1) 对象创建: 单例/多例          scope="singleton", 默认值, 即 默认是单例    [service/dao/工具类]          scope="prototype", 多例  [Action对象]   2) 什么时候创建?          scope="prototype" ,在用到对象的时候,才创建对象.          scope="singleton" ,在启动(容器初始化之前), 就已经创建了bean,且整个应用只有一个. 3)是否延迟创建          lazy-init="false" ,默认为false, 不延迟创建,即在启动时候就创建对象.          lazy-init="true" , 延迟初始化,在用到对象的时候才创建对象.(只对单例有效) 4) 创建对象之后,初始化/销毁          init-method="init_user"            [对应对象的init_user方法,在对象创建爱之后执行]          destroy-method="destroy_user"  [在调用容器对象的destriy方法时候执行,(容器用实现类)]

四,总结

Spring入门就到这吧!下一篇,继续Spring!

转载请注明原文地址: https://www.6miu.com/read-5535.html

最新回复(0)