【SSH】Spring学习(一)

来源:互联网 发布:好听的淘宝童装店名 编辑:程序博客网 时间:2024/05/17 23:47

一、Spring介绍

Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EEfull-stack(一站式) 轻量级开源框架。

Spring框架性质是属于容器性质的,容器中装什么对象就有什么功能,所以可以一站式。

不仅不排斥其它框架,还能帮其它框架管理对象。

AOP支持 IOC思想 Spring jdbcAOP事务Junit测试支持

二、Spring下载及搭建

1、下载

官网:https://spring.io/

下载:http://repo.spring.io/release/org/springframework/spring/


2、Spring搭建

(1)新建工程、导包

1>找到spring-framework-4.3.9.RELEASE-dist\spring-framework-4.3.9.RELEASE\libs下面4个基本jar包


2>spring-framework-3.0.2.RELEASE-dependencies\org.apache.commons\com.springsource.org.apache.commons.logging\1.1.1

spring-framework-3.0.2.RELEASE-dependencies.zip下载地址:http://download.csdn.net/download/a911711054/10031348

将选的jar包导入到工程lib下


(2)新建一个对象

public class User {private String name;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}}

(3)书写配置注册对象到容器

位置任意(建议放在src下)配置文件名任意(建议以applicationContext.xml命名)
1>导入约束



最终效果:
appliationContext.xml
<?xml version="1.0" encoding="UTF-8"?><bean xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans/spring-beans-4.3.xsd" xsi:schemaLocation="http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/beans/spring-beans-4.3.xsd "></bean>

注册Usre对象到容器:


(4)测试代码

public class Demo {@Testpublic void fun1() {//1.创建容器对象ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//2.向容器要user对象User user = (User) ac.getBean("user");//打印userSystem.out.println(user);}}

三、Spring基本概念

1、IOC:控制反转,将对象的创建权交给了Spring

控制反转(Inversion of Control,英文缩写为IoC)是框架的重要特征,并非面向对象编程的专用术语。它与依赖注入(Dependency Injection,简称DI)和依赖查找(Dependency Lookup)并没有关系。

2、DI:依赖注入

依赖注入(Dependency Injection)是Spring框架的核心之一。依赖注入需要有IOC环境,Spring创建这个类的过程中,Spring将类的依赖的属性设置进去。

3、applicationCobtext&BeanFactory


四、Spring配置详解

1、Bean元素


2、Bean元素进阶

(1)Scope属性:

singleton(默认值):单例对象,被标识为单例的对象在spring容器中只会存在一个实例
prototype:多例原型,被标识为多例的对象,每次再获得才会创建,每次创建都是新的对象,整合struts2时候,ActionBean必须配置为多例的
request:web环境下,对象与request生命周期一致
session:web环境下,对象与session生命周期一致


(2)生命周期属性

配置一个方法作为生命周期初始化方法,Spring会在对象那个创建之后立即调用————init-method
配置一个方法作为生命周期的销毁方法,Spring容器会在关闭并销毁所有容器中的对象之前调用————destroy-method

3、Spring创建对象的方式

(1)空参构造
(2)静态工厂(了解)
(3)实例工厂(了解)
Demo.java
public class Demo {@Test//创建方式1:空参构造public void fun1() {//1.创建容器对象ApplicationContext ac = new ClassPathXmlApplicationContext("com/sh/b_create/applicationContext.xml");//2.向容器要user对象User user = (User) ac.getBean("user");//打印userSystem.out.println(user);}@Test//创建方式2:静态工厂构造public void fun2() {//1.创建容器对象ApplicationContext ac = new ClassPathXmlApplicationContext("com/sh/b_create/applicationContext.xml");//2.向容器要user对象User user = (User) ac.getBean("user2");//打印userSystem.out.println(user);}@Test//创建方式3:实例工厂构造public void fun3() {//1.创建容器对象ApplicationContext ac = new ClassPathXmlApplicationContext("com/sh/b_create/applicationContext.xml");//2.向容器要user对象User user = (User) ac.getBean("user3");//打印userSystem.out.println(user);}}
UserFactory.java
public class UserFactory {public static User createUser() {System.out.println("静态工厂创建User");return new User();}public User createUser2() {System.out.println("实例工厂创建User");return new User();}}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "><!-- 创建方式1:空参构造 --><bean name="user" class="com.sh.bean.User"></bean><!-- 创建方式2:静态工厂创建  调用UserFactory的createUser方法创建user2的对象,放入容器--><bean name="user2" class="com.sh.b_create.UserFactory"factory-method="createUser"></bean><!-- 创建方式3:实例工厂  调用UserFactory的createUser2方法创建user3的对象,放入容器--><bean name="user3" factory-bean="userFactory" factory-method="createUser2"></bean><bean name="userFactory" class="com.sh.b_create.UserFactory"></bean></beans>

4、Spring的分模块配置

主配置文件导入其他模块配置
<!-- 导入其他spring配置文件 --><import resource="com/sh/b_create/applicationContext.xml" /> 

五、Spring属性注入

1、set方法注入(最重要)


2、构造函数注入(重要)


3、p名称空间注入(了解)



applicationContext.xml

4、spel注入(了解)

5、复杂类型注入

(1)数组

(2)List/Set

(3)Map

(4)Properties类型

<!-- 复杂类型注入 --><bean name="cb" class="com.sh.c_injection.CollectionBean"><!-- 如果数组中只准备注入一个值(对象),直接使用value|ref即可 --><!-- <property name="arr" value="tom"></property> --><!-- arr注入,多个元素注入 --><property name="arr"><array><value>林黛玉</value><value>晴雯</value><value>西施</value><ref bean="user4"/></array></property><!-- 如果List中只准备注入一个值(对象),直接使用value|ref即可 <property name="list" value="jack" ></property>--><property name="list"  ><list><value>jack</value><value>rose</value><ref bean="user3" /></list></property><!-- map类型注入 --><property name="map"  ><map><entry key="url" value="jdbc:mysql:///crm" ></entry><entry key="user" value-ref="user4"  ></entry><entry key-ref="user3" value-ref="user2"  ></entry></map> </property><!-- prperties 类型注入 --><property name="prop"  ><props><prop key="driverClass">com.jdbc.mysql.Driver</prop><prop key="userName">root</prop><prop key="password">1234</prop></props></property></bean>

参考源码:https://github.com/AmazeLee/Spring.git

Knowledge is a treasure but practice is the key to it. 

学识是宝藏,但实践才是打开它的钥匙。




原创粉丝点击