优雅的SSM框架

一、Spring框架简介

1.Spring是一个开源的轻量级的应用开发框架。

2.目的是简化企业级应用程序开发,降低侵入性(程序间的耦合性)。

3.Spring是以IOC(控制反转)和AOP(面向切面)为核心的轻量级容器。

4.Spring本质是管理软件中的对象,即创建对象和维护对象之间的关系。

5.Spring对常用的API做了封装和简化

二、Spring的作用

1.管理对象(主要功能)

Spring提供了一个容器,帮助创建对象以及管理对象之间的依赖关系,降低了对象之间的耦合度,方便代码的维护。

---什么是Spring容器:Spring容器是spring框架中最核心的一个模块,用于管理对象。

2.集成其他的框架

Spring可以把其他的框架集成起来,和其他的框架联合使用

三、对Spring容器的操作—标题1

1.启动Spring容器

导包:在pom.xml文件的里面配置对

,进行导包

spring框架Maven依赖

2.用Spring容器创建对象(三种方法)

1.构造器new关键字

2.静态工厂方法:getInstance()

//启动Spring容器--默认情况下自动加载xml文件中的bean,根据每一个bean创建实例,且只创建一个对象简单的配置applicationContext.xml文件然后再java文件中用getBean()方法调用获得对象ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");SpringDemo02demo02=ac.getBean("demo01",SpringDemo02.class);

3.使用实例工厂

四、Spring容器的设计模式—单例模式和模型模式

1.创建对象的参数配置:

-->

2.对象的生命周期配置:

对象的生命周期:new--init---service---destory创建对象Spring容器自动创建,init方法

3.对象的销毁:

对象的销毁 关闭Spring容器销毁对象 关闭Spring容器的方法:

((ClassPathXmlApplicationContext)applicationContext).close();

五、Spring对于对象的解耦操作 IOC

1.IOC(控制反转):

IOC:spring的核心---控制反转 由容器来管理对象之间的依赖关系

DI:依赖注入:容器通过调用对象提供的set方法或者构造器来建立依赖关系。

IDC是目标 DI是手段

2.IOC注入方式:set注入、构造器、自动装载

1.Set注入:

配置文件

A类

public classA {publicA(){System.out.println("A --- A");}public voidf1(){System.out.println("A --- f1");}}

B类

public class B {private A a;publicB(){System.out.println("B -- B");}public voidsetA(A a) {this.a = a;}public voidexecute(){a.f1();}}

测试类

@Testpublic voidtest01(){ApplicationContext applicationContext=newClassPathXmlApplicationContext("applicationContext.xml");applicationContext.getBean("b",B.class).execute();}

set注入的使用步骤:提供set方法-----配置元素的属性 name, ref, value

2.构造器注入:

配置文件

Peson类

public class Person{private Man man;private Woman woman;Public Person(){System.out.println("person");}public Person (Manman,Womanwoman){this.man=man;this.woman=woman;}public void execute(){man.m();woman.w();}}

Man类

public class Man{public Man(){System.out.println("m");}public void m(){System.out.println("man---f");}}

Woman类

public class Woman{public Woman(){System.out.println("w");}public void w(){System.out.println("woman---f");}}

@Testpublic void test02(){ApplicationContext applicationContext= new ClassPathXmlApplicationContext("applicationContext.xml");applicationContext.getBean("person",Person.class).execute();}

3.自动装配:

配置文件

Teacher类

public class Teacher {publicTeacher(){}public voidteach(){System.out.println("teacher -- teach");}}

Student类

public class Student {privateTeacher teacher;public voidsetTeacher(Teacher teacher){this.teacher = teacher;}public voidexecute(){teacher.teach();}}

@Testpublic voidtest03(){ApplicationContext applicationContext =newClassPathXmlApplicationContext("applicationContext.xml");applicationContext.getBean("student", Student.class).execute();}

3.Spring的参数注入和基于注解的组件扫描

1.参数注入:依赖于getter和setter方法

1.getter、setter方法:

张三李四王五3215344354张三23

/**

*给基本数据注入赋值

*/

public class ValueBean {//给基本数据类型注入值int age;//给字符串注入值String name;//给list集合注入值private List list;//给set集合注入值private Set set;//给map集合注入值private Map map;//给Properties注入值private Properties properties;getter();setter();toString();}

/**

*测试参数注入

*/

@Testpublic void test04(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");ValueBean value = applicationContext.getBean("value", ValueBean.class);System.out.println(value);}

2.引用注入:

1234wwwm33

读取properties文件中的配置内容

3.Spring表达式:

//测试用的类

public class PropertiesBean {private String name;private String age;private String sex;public void setAge(String age) {this.age = age;}public void setName(String name) {this.name = name;}public void setSex(String sex) {this.sex = sex;}@Overridepublic String toString() {return "properties name="+name+" age="+age+" sex="+sex;}}

2.注解扫描:减少xml文件的书写量

1.注解扫描:

/**

*注解扫描 组件扫描

*什么是组件扫描:指定一个包路径,Spring会自动扫描包及其子包所有的组件类

*当发现组件定义前有特定的注解标记时,

*就将该组件纳入到Spring容器,等价于原有XML配置中的定义功能

*组件扫描可以替代大量XML配置的定义

*

常用注解

2.注解步骤及常用注解:

----注解实例:

@Scope("prototype")//注解成原型模式 不加这个注解默认是单例模式

@Component("emp")//创建对象的注解 ("emp")表示自定义的id

@Lazy(true)//懒加载的注解

public class Emp {private Dept dept;@Resource(name = "dept") //具有依赖关系的Bean对象的注解public void setDept(Dept dept) {System.out.println("set依赖注入---"+dept);this.dept = dept;}//1.使用组件扫描,首先需要在XML配置中指定扫描路径public Emp(){System.out.println("emp --- 组件扫描");}//2.当一个组件在扫描过程中被检测到时,会生成一个默认id值,//默认id为小写开头的类名,也可以在注解标记中自定义id,//3.开启Spring容器 根据id获取到相应的对象@Overridepublic String toString() {return "emp 组件扫描";}@PostConstruct //注解生成初始化方法public void init(){System.out.println("init 方法 初始化");}@PreDestroy //注解生成销毁方法public void destory(){/*** 原型模式下不能进行调用销毁方法* 单例模式下可以进行调用*/System.out.println("销毁的方法");}}

/**

*指定依赖注入的关系

*/

@Component("dept")//将类添加进Spring进行管理

public class Dept {public Dept(){System.out.println("dept -- dept");}@Overridepublic String toString() {return "dept 的toString方法";}}

本系列作品持续更新,包括Spring,SpringMVC,Mybatis,SpringBoot,等流行框架,由于横跨知识面积较广,时间可能要久一些,希望有意向的读者能够加一下关注,收藏,谢谢大家的支持,来自一个程序员的

关键词: ssm框架