SSM框架(一)Spring基本介绍和配置

目录

 

1. 什么是Spring?

2. Spring容器和Java Bean

3. Spring开发步骤

3.1 导入需要用到的jar工具包(版本统一)

3.2 编写JavaBean

3.3 编写配置文件applicationContext.xml和配置Bean

3.4 获得JavaBean实例

4. Spring中Bean的五个作用域

5. SpringIOC控制反转

6. SpringDI依赖注入


1. 什么是Spring?

Spring是一个开源的的轻量级的应用开发框架,其目的是用来简化企业级应用程序开发,减少代码之间的侵入性。Spring为系统提供了一个整体的解决方案,开发者除了可以利用它本身提供的功能外,还可以与第三方 的框架和技术整合应用,可以自由的选择采用哪种技术进行开发。使用Spring的目的:Spring 的本质是管理软件中的对象,即如何创建对象和维护对象之间的关系。

2. Spring容器和Java Bean

在Spring中,任何java类和javaBean都会被当成Bean处理,这些Bean通过Spring容器进行管理和应用,Spring容器是实现了IOC和AOP机制,这些机制可以简化Bean的创建和Bean对象之间的解耦。

  Spring容器有BeanFactory和ApplicationContext两种类型。  BeanFacotry是Spring中比较原始的Factory,ApplicationContext继承自BeanFactory接口,拥有更多的企业级方法,原始的BeanFactory无法支持Spring的许多插件,如AOP功能、Web应用等, 所以在实际应用中推荐使用ApplicationContext,实例化ApplicationContext有两种方式(两种方式下都需要有applicationContext.xml配置文件)

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ApplicationContext context2 = new FileSystemXmlApplicationContext("src/applicationContext.xml");

3. Spring开发步骤

3.1 导入需要用到的jar工具包(版本统一)

“commons-logging.jar”                     

“spring-beans-4.2.4.RELEASE.jar”

“spring-context-4.2.4.RELEASE.jar”   

“spring-core-4.2.4.RELEASE.jar”

“spring-expression-4.2.4.RELEASE.jar”

commons-logging.jar包是使用spring的必备包。是一个高层的日志框架,但是它本身没有记录日志的功能,它需要依赖其他的日志系统如log4j来实现日志记录。

spring-beans-4.2.4.RELEASE.jar:完成Bean工厂与bean的装配,配置Java bean和工厂对应

   spring-context-4.2.4.RELEASE.jar:spring的context上下文即IoC容器

   spring-expression-4.2.4.RELEASE.jar:spring表达式语言

   spring-core-4.2.4.RELEASE.jar:依赖注入IoC与DI的最基本实现

   因为spring-core依赖了commons-logging,而其他模块都依赖了spring-core,所以整个spring框架都依赖了commons-logging,所以commons-logging.jar是Spring必不可少的一个jar包。

3.2 编写JavaBean

一个JavaBean简单的来说就是一个类,他对于到数据库里面的字段编写,一个bean对应一张表,有时候也可以根据实际情况进行添加Spring框架采取这种机制,就可以简化bean的创建和bean对象之间的解耦。

下面是一个javabean编写的实例

数据库中的某一张表为person,字段有name,password,age,address;那么他的bean为

package com.chtw.entity;

public class Person {
	private String name;
	private String password;
	private String age;
	private String address;
	
	//构造器
	public Person(String name, String password, String age, String address) {
		super();
		this.name = name;
		this.password = password;
		this.age = age;
		this.address = address;
	}
	public Person() {
		
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
}

3.3 编写配置文件applicationContext.xml和配置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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!--没有进行任何配置-->
</beans>

在容器配置文件applicationContext.xml中添加对要用的Bean的定义

<bean  id="userBean"    class="com.chtw.UserBean"/>

id 或name属性用于指定该Bean的名称,以便从Spring中查找该Bean,class 用来指定该Bean的类型,即我们要实例化哪个类,写全类名。

随后利用Spring实例化bean

Spring中实例化Bean一般有三中方式:

每一种实例化在applicationContext.xml配置文件中配置的方式都不一样:

  1、使用构造器实例化;(90%通常使用方法)

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

    <!-- 构造实例化person,Spring容器会通过无参构造器来实例化该类,所以使用这个方式来实例化Bean要求要实例化的类中一定要求无参构造器-->
    <bean id="person" class="com.chtw.entity.Person"/>

    
    <!--构造器注入,后面会用到,先写在这儿-->
    <bean id="person" class="com.chtw.entity.Person">
		<constructor-arg index="0" value="Alice"></constructor-arg>
		<constructor-arg index="1" value="123456"></constructor-arg>
		<constructor-arg index="2" value="25"></constructor-arg>
		<constructor-arg index="3" value="四川成都"></constructor-arg>
	</bean>
</beans>

  2、使用静态工厂方法实例化;

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!-- 静态工厂方法:这种方法需要从新写一个工具类,下面这个工具类就是PersonFactory-->
	<bean id="personFactory" class="com.chtw.entity.PersonFactory" factory-method="getPerson"></bean>
</beans>
package com.chtw.entity;

public class PersonFactory {

	//静态工厂
	public static Person getPerson() {
		return new Person("Bob","654321","52","四川广元");
	}
	
}

  3、使用实例化工厂方法实例化。

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

    <!-- 实例化工厂:和静态工厂一样需要一个工具类 -->
    <!-- 使用实例化工厂的时候不使用构造器 -->
	<bean id="personFactory" class="com.chtw.entity.PersonFactory"></bean>
	<bean id="person" class="com.chtw.entity.Person" factory-bean="personFactory" factory-method="getaPerson"></bean>
</beans>
package com.chtw.entity;

public class PersonFactory {

	//实例化工厂
	public Person getaPerson() {
		return new Person("Farch","852025","99","四川成都");
	}
}

3.4 获得JavaBean实例

获取JavaBean实例有两种方法

方式一: 通过ClassPathXmlApplicationContext加载配置文件并实例化:

默认在类的路径下去找xml配置文件,类路径就是编译后.class文件所在的路径,一般在web-info/classes

ApplicationContext  context  = new  ClassPathXmlApplicationContext("applicationContext.xml");
//当需要加载多个applicationContext.xml文件时可以是数组:
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","ac1.xml","ac2.xml"});
//也可以是绝对路径(需要加file前缀):
ApplicationContext  context1  = new  ClassPathXmlApplicationContext("file:D:/FirstSpringPJ/src/ac.xml");

//获取一个person实例
Person p = context.getBean(Person.class);
System.out.println(p.getName()+"--"+p.getPassword()+"--"+p.getAge()+"--"+p.getAddress());

方式二:通过FileSystemXmlApplicationContext加载配置文件并实例化:

默认项目路径下寻找applicationContext.xml文件

ApplicationContext  context = new 	FileSystemXmlApplicationContext("src/applicationContext.xml");	
//加载多个配置文件时可以是数组:
ApplicationContext  context1 =  new FileSystemXmlApplicationContext(new String[]{"src/applicationContext.xml"});
//绝对路径(可以省略file前缀):
ApplicationContext context2 = new FileSystemXmlApplicationContext  ("D:\FirstSpring_Project\src\ac.xml");	

//获取一个person实例
Person p = context.getBean(Person.class);
System.out.println(p.getName()+"--"+p.getPassword()+"--"+p.getAge()+"--"+p.getAddress());

测试类进行测试(不需要我们创建person对象了,在Spring中已经进行了实例化了)

package com.chtw.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.chtw.entity.Cat;
import com.chtw.entity.Person;
import com.chtw.entity.Student;
import com.chtw.entity.User;

public class TestMain {
	public static void main(String[] args) {
		
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		
		//默认路径src下开始
		//实例化bean:通过id获取
		/*Person p = (person)context.getBean("person");
		p.setName("Alice");
		System.out.println(p.getName());*/
		
		
		//默认路径从项目下开始
		//ApplicationContext context2 = new FileSystemXmlApplicationContext("src/applicationContext.xml");
		
		//Person p = context.getBean(Person.class);
		//System.out.println(p.getName());
		
		//获取一个person实例
		Person p = context.getBean(Person.class);
		System.out.println(p.getName()+"--"+p.getPassword()+"--"+p.getAge()+"--"+p.getAddress());
		
	}
}

4. Spring中Bean的五个作用域

singleton:单例模式,在整个Spring IoC容器中,使用singleton定义的Bean将只有一个实例。

prototype:原型模式,每次通过容器的getBean方法获取prototype定义的Bean时,都将产生一个新的Bean实例。

request:对于每次HTTP请求,使用request定义的Bean都将产生一个新实例,即每次HTTP请求将会产生不同的Bean实例。只有在Web应用中使用Spring时,该作用域才有效。

session:对于每次HTTP Session,用法和使用时机同上。

globalsession:每个全局的HTTP Session,使用session定义的Bean都将产生一个新实例。一般用于Porlet应用环境 , 分布式系统存在全局session概念。如果不是porlet环境,globalSession 等同于Session 。

5. SpringIOC控制反转

IOC不是一种技术,而是一种思想,由最初的new方式创建,转变成由第三方框架创建、注入。把创建和查找依赖对象的控制权交给了容器,由容器进行注入组合对象。

6. SpringDI依赖注入

实现依赖注入主要有两种方式:

举一个例子,现在有一个学生类,每一个学生有姓名,可以养一只猫,那现在我们就需要两个bean,Student和Cat

package com.chtw.entity;

public class Student {

	private String name;
	private Cat cat;
	
	
	public Student(String name, Cat cat) {
		super();
		this.name = name;
		this.cat = cat;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Cat getCat() {
		return cat;
	}
	public void setCat(Cat cat) {
		this.cat = cat;
	}
	
	
}
package com.chtw.entity;

public class Cat {
	
	private String name;
	private int age;

	//初始化方法
	public void init() {
		System.out.println("初始化成功");
	}

	public void destory() {
		System.out.println("对象销毁成功");
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}

1setter注入

通过调用无参构造器或者无参static工厂方法实例化bean以后,调用该Beanset方法,即可实现setter注入。

    <!-- 指定一个类的初始化调用方法以及销毁调用方法,延迟实例化:lazy-init 当beans添加default-lazy-init="true"的时候,所有bean全部延迟实例化-->
	<bean id="cat" class="com.chtw.entity.Cat" init-method="init" destroy-method="destory" lazy-init="true"></bean>
    <!--setter注入-->
	<bean id="student" class="com.chtw.entity.Student">
		<property name="name" value="Alice"></property>
		<property name="cat" ref="cat"></property>
	</bean>
	

2、构造器注入

通过调用带参数的构造器来实现,容器会在Bean被实例化的时候,根据参数的类型,执行相应的构造器。

<!-- 指定一个类的初始化调用方法以及销毁调用方法,延迟实例化:lazy-init 当beans添加default-lazy-init="true"的时候,所有bean全部延迟实例化-->
	<bean id="cat" class="com.chtw.entity.Cat" init-method="init" destroy-method="destory" lazy-init="true"></bean>
    <!--构造器注入-->
	<bean id="student" class="com.chtw.entity.Student">
		<constructor-arg index="0" value="Bob"></constructor-arg>
		<constructor-arg index="1" ref="cat"></constructor-arg>
	</bean>
	

本人联系方式2329095893,欢迎各位进行学习讨论

欢迎关注熊熊出没ING公众号,不定时跟新Java、python、信息安全等相关知识哦。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值