SSM-Spring-01:Spring的概念+入门案例

释放双眼,带上耳机,听听看~!

 

 


1
2
1------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
2

 

 

Spring

 

提起Spring,就会想到企业级框架这个词

企业级系统:

1.大规模:用户数量多,数据规模庞大,数据众多

2.性能和安全性要求更高

3.业务复杂

4.灵活应变

————————————————————————————————————————————————–

我觉得先了解一下Spring的地位和他的作者比较好

Spring:java中的核心框架,没有它就没有现在的java的地位,如果Spring挂掉了,3-5年内java必挂

Spring 的作者:Rod Johnson


他是SpringFramework创始人,interface21 CEO

丰富的C/C++背景,丰富的金融行业背景

1996年开始关注java服务器端技术

2002年著写《Expoert one-on-oneJ2EE设计与开发》,改变了Java世界

技术主张以实用为本,音乐学博士

————————————————————————————————————————————————–

**接下来讲讲Spring的内容,放俩张图片
**


Data/Access:数据访问

Web:网络

AOP:面向切面编程

Instrumentatio:插桩

Core Container:核心(下面的一张图有他的核心的内容,先理解这张**)**

Test:测试,单测


 

官网:Spring.io        拿的图

————————————————————————————————————————————————–

Spring的核心IOC和AOP(本处详解IOC)

IOC:控制反转:(Inverse Of Control)
控制反转(Inversion of Control),是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心,beans。
理解一:将组件对象(业务对象)的控制权从代码本身转移到外部容器()
理解二:IOC控制反转:说的是创建对象实例的控制权从代码控制剥离到IOC容器(spring容器)控制,实际就是你在xml文件控制,侧重于原理。

AOP:面向切面编程;  (Aspect Oritend Programming)

提及一下对象间的关系把


 

————————————————————————————————————————————————–

第一个案例:

1.下载jar包:(我提供节点)

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1        <!--单元测试的依赖  ctrl+shif+/-->
2        <dependency>
3            <groupId>junit</groupId>
4            <artifactId>junit</artifactId>
5            <version>4.12</version>
6            <scope>test</scope>
7        </dependency>
8
9        <!--Spring-->
10        <dependency>
11            <groupId>org.springframework</groupId>
12            <artifactId>spring-beans</artifactId>
13            <version>4.2.0.RELEASE</version>
14        </dependency>
15
16        <dependency>
17            <groupId>org.springframework</groupId>
18            <artifactId>spring-context</artifactId>
19            <version>4.2.0.RELEASE</version>
20        </dependency>
21
22        <!--aop使用的jar-->
23        <dependency>
24            <groupId> org.aspectj</groupId >
25            <artifactId> aspectjweaver</artifactId >
26            <version> 1.8.7</version>
27        </dependency>    
28

 

2.一个普通类


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
1package cn.dawn.day01.service;
2
3/**
4 * Created by Dawn on 2018/3/3.
5 */
6public class DawnService {
7
8    private String workInfo;
9    private Integer age;
10    public void work(){
11        System.out.println("info"+workInfo);
12    }
13
14    @Override
15    public String toString() {
16        return "DawnService{" +
17                "workInfo='" + workInfo + '\'' +
18                ", age=" + age +
19                '}';
20    }
21
22    public String getWorkInfo() {
23        return workInfo;
24    }
25
26    public void setWorkInfo(String workInfo) {
27        this.workInfo = workInfo;
28    }
29
30    public Integer getAge() {
31        return age;
32    }
33
34    public void setAge(Integer age) {
35        this.age = age;
36    }
37}
38

 

3.大配置文件

 


1
2
3
4
5
6
7
8
9
10
11
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5
6    <bean id="service" class="cn.dawn.day01.service.DawnService">
7        <property name="workInfo" value="第一个Spring程序"></property>
8        <property name="age" value="12"></property>
9    </bean>
10</beans>
11

 

4.单测

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1package cn.dawn.day01;
2
3import cn.dawn.day01.service.DawnService;
4import org.junit.Test;
5import org.springframework.context.ApplicationContext;
6import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8/**
9 * Created by Dawn on 2018/3/3.
10 */
11public class test20180303 {
12    @Test
13    /*入门案例*/
14    public void t01(){
15        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext01.xml");
16        DawnService service = (DawnService) context.getBean("service");
17        System.out.println(service);
18    }
19}
20

 

在没有new 的情况下,就拿到了他的实现

**
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext01.xml");**

这行如果不理解我就放一张图


 看到ClassPathXmlApplicationContext前面的C吗?在结合这个,表示他是ApplicationContext的实现类,里氏替换,为什么不可以用

————————————————————————————————————————————————–

还记得我提了的IOC吗?我不是说他把创建对象的控制权交给了Spring容器嘛,那么容器在什么时候创建对象呢,是getBean的时候吗?还是。。。。(小实验)

在刚才的那个普通类中,添加一个构造,如下


1
2
3
4
1public DawnService(){
2        System.out.println("========================DawnService创建=======================");
3    }
4

 单测方法如下

 


1
2
3
4
5
6
7
8
1@Test
2    /*入门案例*/
3    public void t01(){
4        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext01.xml");
5        /*DawnService service = (DawnService) context.getBean("service");
6        System.out.println(service);*/
7    }
8

 

运行结果:


 

     结论就是Spring容器初始化的时候就把bean中的对象实例化了

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++ lambda表达式

2022-1-11 12:36:11

安全经验

使用Nginx+FFMPEG搭建HLS直播转码服务器

2021-11-28 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索