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

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包:(我提供节点)

        <!--单元测试的依赖  ctrl+shif+/-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> <!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2..RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2..RELEASE</version>
</dependency> <!--aop使用的jar-->
<dependency>
<groupId> org.aspectj</groupId >
<artifactId> aspectjweaver</artifactId >
<version> 1.8.</version>
</dependency>

    2.一个普通类

package cn.dawn.day01.service;

/**
* Created by Dawn on 2018/3/3.
*/
public class DawnService { private String workInfo;
private Integer age;
public void work(){
System.out.println("info"+workInfo);
} @Override
public String toString() {
return "DawnService{" +
"workInfo='" + workInfo + '\'' +
", age=" + age +
'}';
} public String getWorkInfo() {
return workInfo;
} public void setWorkInfo(String workInfo) {
this.workInfo = workInfo;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}

    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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="service" class="cn.dawn.day01.service.DawnService">
<property name="workInfo" value="第一个Spring程序"></property>
<property name="age" value=""></property>
</bean>
</beans>

    4.单测

package cn.dawn.day01;

import cn.dawn.day01.service.DawnService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by Dawn on 2018/3/3.
*/
public class test20180303 {
@Test
/*入门案例*/
public void t01(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext01.xml");
DawnService service = (DawnService) context.getBean("service");
System.out.println(service);
}
}

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

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

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

    

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

--------------------------------------------------------------------------------------------------------------------------------------------------

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

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

public DawnService(){
System.out.println("========================DawnService创建=======================");
}

    单测方法如下

    @Test
/*入门案例*/
public void t01(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext01.xml");
/*DawnService service = (DawnService) context.getBean("service");
System.out.println(service);*/
}

    运行结果:

    

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

最新文章

  1. ubuntu系统修改mysql字符集
  2. Node.js Stream-基础篇
  3. whose view is not in the window hierarchy
  4. NET Core中实现一个Token base的身份认证
  5. [Asp.net mvc]jquery.form.js无刷新上传
  6. jquery eval解析JSON中的注意点介绍
  7. Codeforces Round #292 (Div. 2)
  8. Android Studio如何发布APK
  9. subclipse svn 在64位win7下报Failed to load JavaHL Library
  10. 使用Yeoman搭建 AngularJS 应用 (2) —— 让我们搭建一个网页应用
  11. [AngularJS] Services, Factories, and Providers -- value &amp; Providers
  12. #include &lt;boost/asio.hpp&gt;
  13. 重操JS旧业第八弹:面向对象与继承
  14. java多线程:ReentrantReadWriteLock读写锁使用
  15. iOS开发 自定义UIAlertController的样式
  16. spring:org.springframework.web.servlet.DispatcherServlet noHandlerFound解决方法
  17. lombok学习
  18. final发布评论Ⅱ
  19. 第四章 logstash - type与tags属性
  20. Andoird通信之简单聊天ChatApp

热门文章

  1. VCC、 VDD、VEE、VSS 电压理解
  2. LeetCode之“链表”:Partition List
  3. web报表工具FineReport常用函数的用法总结(日期和时间函数)
  4. JFace dailog button事件中刷新透视图异常 Trying to execute the disabled command org.eclipse.ui.window.closePerspective
  5. java keytool
  6. windows环境下zookeeper安装和使用
  7. php namespace与use
  8. SpringMvc+AngularJS通过CORS实现跨域方案
  9. ADT Android开发环境搭建小记
  10. Jersey VS Django-Rest