当前位置: 移动技术网 > IT编程>开发语言>Java > Spring学习手札(一)

Spring学习手札(一)

2018年09月28日  | 移动技术网IT编程  | 我要评论

白小俺整容,形象墙设计,寄生前夜 第三次生日

 

spring能做什么

1. 能根据配置文件创建及组装对象之间的依赖关系;

2. 面向切面编程,能帮助我们无耦合的实现日志记录,性能统计,安全控制等;

3. 提供第三方数据访问框架(如hibernate),而且自己也提供了一套jdbc访问模板方便访问数据库;

4. 非常简单的管理数据库事务;

5. 集成第三方web框架(如struts1/2),而且本身有spring mvc方便搭建web层

6. 与java ee整合,与更多技术整合(比如缓存)

 

spring的特色

1. 方便解耦,简化开发

 spring ioc完成对象创建,依赖关系维护等

2. aop的支持

 spring提供面向切面编程,实现拦截,监控等

3. 声明式事务管理

 只需通过配置完成对事务的管理,而无需手动操作

4. 方便测试

 支持junit4,可以通过注解方便测试

5. 方便集成其他框架

 一直在提供并完善各种框架(如struts,mybatis,hibernate等)

6. 降低开发难度,一站式开发项目

 提供各种优秀前端框架,以及后台框架,一站式开发网站

 

spring的定义

先看看百度百科的解释

spring框架是一个轻量级的di/ioc和aop容器的开源框架;

spring为了解决企业应用开发的复杂性而创建的;

spring的用途不仅限于服务器端的开发,只是为了简化java开发。从简单性、可测试性和松耦合的角度而言,任何java应用都可以从spring中受益。spring的核心是控制反转(ioc)和面向切面(aop)简单来说,spring是一个分层的javase/ee full-stack(一站式) 轻量级开源框架。

spring使用基本的javabean来完成开发。

 

spring框架结构

 

  • data access/integration层包含有jdbc、orm、oxm、jms和transaction模块。
  • web层包含了web、web-servlet、websocket、web-porlet模块。
  • aop模块提供了一个符合aop联盟标准的面向切面编程的实现。
  • core container(核心容器):包含有beans、core、context和spel模块。
  • test模块支持使用junit和testng对spring组件进行测试。

 

bean规范

每一个类实现了bean的规范才能有spring来接管

    必须有个公有类

    有无参数构造函数

    用公共方法暴露内部成员属性(getter,setter )

bean的生命周期

   

bean的作用域

spring定义了多种bean作用域,可基于这些作用域创建bean,包括:

    单例(singleton):在整个应用中,只创建一个bean实例

    原型(prototype):每次注入或者通过spring应用上下文获取的时候,都会创建一个新的bean实例

    会话(session):在web应用中,为每个会话创建一个bean实例

请求(request):在web应用中,为每个请求创建一个bean实例。默认情况下,spring应用上下文中所有bean都是作为单例的形式创建。也就是说,不管既定的一个bean被注入到其他bean多少次,每次所注入的都是同一个实例。

 

ioc了解一下

一种设计思想。就是将原本在程序中手动创建对象的控制权,交由spring框架来管理。不用new对象,而直接从spring那里获取对象。

 

开始spring的coding漫漫路 

1. 新建java项目,命名 spring

2.新建lib目录,并添加jar包,

 

3.开始编写程序

  3.1 在src下新建bean包,然后新建user类

package bean;

public class user {
    private integer id;
    private string name;
    private string gender;

    public integer getid() {
        return id;
    }

    public void setid(integer id) {
        this.id = id;
    }

    public string getname() {
        return name;
    }

    public void setname(string name) {
        this.name = name;
    }

    public string getgender() {
        return gender;
    }

    public void setgender(string gender) {
        this.gender = gender;
    }

}
view code

 

  3.2 在src下新建 applicationcontext.xml

<?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 name="beanuser" class="bean.user">
        <property name="id" value="1001"></property>
        <property name="name" value="jesse"></property>
        <property name="gender" value="male"></property>
    </bean>

</beans>
view code

 

4.测试程序

  4.1 在src下新建test类

import bean.user;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;

public class test {

    @test
    public void demo1() {
        applicationcontext context = new classpathxmlapplicationcontext(
                new string[]{"applicationcontext.xml"});
        user user = (user) context.getbean("beanuser");
        system.out.println(user.getid());
        system.out.println(user.getname());
        system.out.println(user.getgender());
    }
}

 运行结果: 

 

di:dependency injection (依赖注入)

spring 在创建对象的过程中,将对象依赖属性(简单值,集合,对象)通过配置设值给该对象。或者说拿到对象的属性,已经被注入到了相关值了,可以直接使用。

1. 在bean下新建 saysomething类

package bean;

public class saysomething {
    private user user = null;

    public user getuser() {
        return user;
    }

    public void setuser(user user) {
        this.user = user;
    }

    public string sayhi() {
        return user.getid() + " " + user.getname() + " " + user.getgender();
    }
}
view code

 

2.修改applicationcontext.xml,如下:

<?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 name="beanuser" class="bean.user">
        <property name="id" value="1001"></property>
        <property name="name" value="jesse"></property>
        <property name="gender" value="male"></property>
    </bean>

    <bean name="saysomething" class="bean.saysomething">
        <property name="user" ref="beanuser"></property>
    </bean>

</beans>
view code

 

3.在test.java里,添加新的方法,测试上面代码

  @test
    public void demo2() {
        applicationcontext context = new classpathxmlapplicationcontext(
                new string[]{"applicationcontext.xml"});
        saysomething saysomething = (saysomething) context.getbean("saysomething");
        system.out.println(saysomething.sayhi());
    }

 运行结果: 

 

aop aspect oriented program 面向切面编程

aspect oriented program面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。aop采取横向抽取机制,取代了传统纵向继承体系重复性代码

      核心业务,比如登陆,cud

      周边功能,统计,日志,事务管理。在spring的面向切面编程aop思想里,即被定义为切面

      在面向切面的思想里,核心业务功能和切面功能单独开发,然后把两个组合在一起,就是aop 

目的:aop 能够将那些与业务无关,却为业务模块所共同调用的逻辑或责任(事务,日志,权限等)封装起来,便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可拓展性和可维护性。

aop实现原理

    aop底层将采用代理机制进行实现

    接口+实现类:spring采用jdk的动态代理proxy

    实现类:spring采用cglib字节码增强

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网