参考链接:

HOW2J.CN:Spring

idea创建一个spring项目

一、IDEA创建Spring项目

创建方法:idea创建一个spring项目

maven管理项目,生成项目结构如下:



在main文件夹下新建Resources目录,并且将此目录设置为资源文件夹,在此文件夹下创建文件applicationContext.xml





然后在pom.xml中添加spring的依赖:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>

到这里Spring的基本配置弄完了。

二、Spring: IOC和DI

IOC:反转控制,将对象创建的过程交给Spring

DI:依赖注入,拿到的对象,属性可以被注入了相关值,直接可以使用。

我觉得how2j的站长对于spring的IOC的比喻非常好:

传统方式:相当于你自己去菜市场new 了一只鸡,不过是生鸡,要自己拔毛,去内脏,再上花椒,酱油,烤制,经过各种工序之后,才可以食用。

用 IOC:相当于去馆子(Spring)点了一只鸡,交到你手上的时候,已经五味俱全,你就只管吃就行了。

接下来我们来看看我们如何直接去Spring“餐馆”点一只做好的鸡(对象),以下为写配置文件实现,还可以使用注解实现,注解方法不在此展示

  1. 创建一个Student类和一个Book类

public class Book {
private String name;
private double money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
public class Student {
private String name;
private Book book;
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
  1. 在applicationContext.xml中编写关于Student类和Book类的配置:

下面这段代码的意思是通过Spring拿到Student对象时,Spring会注入属性name和book,并且name被赋值为张三,book被赋予名字语文,好比你直接拿到了一个姓名为张三,带着语文书的Student对象

<?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="Student" class="whu.xsy.spring.po.Student">
<property name="name" value="张三"></property>
<property name="book" ref="Book"></property>
</bean>
<bean name="Book" class="whu.xsy.spring.po.Book">
<property name="name" value="语文" />
</bean> </beans>
  1. 获取对象
public class App {
public static void main( String[] args ){
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
Student s = (Student)context.getBean("Student");
System.out.println(s.getName());
System.out.println(s.getBook().getName());
}
}

结果如下:

张三

语文

三、Spring: AOP

AOP 即 Aspect Oriented Program 面向切面编程。

在面向切面编程的思想中,把功能分为了核心业务功能和周边功能。

核心业务功能:比如登录、增删改查数据库等逻辑业务功能

周边功能(切面):比如记日志、事务管理等等

在项目中,通常将核心业务功能周边功能分别独立开发,在项目运行时又“交织”在一起进行。这种编程思想就叫AOP

  1. 创建SaleService.java文件,作为学生卖书的逻辑业务类
public class SaleService {
//标价
public void markPrice(){
System.out.println("标价服务");
}
}
  1. 如果每次调用SaleService的对象之前,都要提醒一句话:”当前正在进行交易“,那么每次调用之前,都要写一句(非常麻烦):
System.out.println("当前正在进行交易")

如果使用AOP思想,只需要在applicationContext.xml中配置一下就行。

先在pom.xml中加入如下依赖:

    <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>

创建专门的切面类(用于日志提示):

public class LoggerAspect {

    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}

编写配置文件:

<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean name="Student" class="whu.xsy.spring.po.Student">
<property name="name" value="张三"></property>
<property name="book" ref="Book"></property>
</bean>
<bean name="Book" class="whu.xsy.spring.po.Book">
<property name="name" value="语文" />
</bean> <bean name="SaleService" class="whu.xsy.spring.service.SaleService">
</bean> <bean id="loggerAspect" class="whu.xsy.spring.log.LoggerAspect"/> <aop:config>
<aop:pointcut id="loggerCutpoint"
expression=
"execution(* whu.xsy.spring.service.SaleService.*(..)) "/> <aop:aspect id="logAspect" ref="loggerAspect">
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
</aop:config> </beans>
  1. 三次调用SaleService对象
public class App {
public static void main( String[] args ){
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
Student s = (Student)context.getBean("Student");
SaleService service = (SaleService)context.getBean("SaleService"); for(int i = 0; i < 3; i++){
service.markPrice();
}
}
}

结果为(每次调用markPrice时,都在前后打印了日志):

start log:markPrice

标价服务

end log:markPrice

start log:markPrice

标价服务

end log:markPrice

start log:markPrice

标价服务

end log:markPrice

Spring框架零基础学习(一):IOC|DI、AOP的更多相关文章

  1. 深入浅出学习Spring框架(四):IoC和AOP的应用——事务配置

    在前文 深入浅出学习Spring框架(一):通过Demo阐述IoC和DI的优势所在. 深入浅出学习Spring框架(三):AOP 详解 分别介绍了Spring的核心功能——IoC和AOP,光讲知识远远 ...

  2. Yaf零基础学习总结2-Yaf框架的安装

    接着上一篇文章<Yaf零基础学习总结1-Yaf框架简介>我们对Yaf框架有那么一个大概的了解了,但是对于程序员来说,那些文字都是表面的,他们最想的就是开始敲代码了.当然这也是学习Yaf框架 ...

  3. 如何从零基础学习VR

    转载请声明转载地址:http://www.cnblogs.com/Rodolfo/,违者必究. 近期很多搞技术的朋友问我,如何步入VR的圈子?如何从零基础系统性的学习VR技术? 本人将于2017年1月 ...

  4. HTML5零基础学习Web前端需要知道哪些?

    HTML零基础学习Web前端网页制作,首先是要掌握一些常用标签的使用和他们的各个属性,常用的标签我总结了一下有以下这些: html:页面的根元素. head:页面的头部标签,是所有头部元素的容器. b ...

  5. Yaf零基础学习总结5-Yaf类的自动加载

    Yaf零基础学习总结5-Yaf类的自动加载 框架的一个重要功能就是类的自动加载了,在第一个demo的时候我们就约定自己的项目的目录结构,框架就基于这个目录结构来自动加载需要的类文件. Yaf在自启动的 ...

  6. Yaf零基础学习总结4-Yaf的配置文件

    在上一节的hello yaf当中我们已经接触过了yaf的配置文件了, Yaf和用户共用一个配置空间, 也就是在Yaf_Application初始化时刻给出的配置文件中的配置. 作为区别, Yaf的配置 ...

  7. Yaf零基础学习总结3-Hello Yaf

    Yaf零基础学习总结3-Hello Yaf 上一次我们已经学习了如何安装yaf了,准备工作做好了之后我们来开始实际的编码了,码农都知道一个经典的语句就是“Hello World”了,今天我们开始入手Y ...

  8. [原]零基础学习视频解码之android篇系列文章

    截止今天,<零基础学习视频解码系列文章>.<零基础学习在Android进行SDL开发系列文章>以及<零基础学习视频解码之android篇>系列文章基本算是告一段落了 ...

  9. 译:Spring框架参考文档之IoC容器(未完成)

    6. IoC容器 6.1 Spring IoC容器和bean介绍 这一章节介绍了Spring框架的控制反转(IoC)实现的原理.IoC也被称作依赖注入(DI).It is a process wher ...

随机推荐

  1. Mysql报错Error Code: 1175. You are using safe update

    使用MySQL执行update的时候报错:Error Code: 1175. You are using safe update mode and you tried to update a tabl ...

  2. Dubbo面试专题

    Dubbo面试专题 1. 什么是dubbo Dubbo是阿里巴巴SOA服务化治理方案的核心框架,是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 2.  ...

  3. Blazor带我重玩前端(一)

    写在前面 曾经我和前端朋友聊天的时候,我说我希望有一天可以用C#写前端,不过当时更多的是美好的想象,而现在这一切正变得真实…… 什么是Blazor 我们知道浏览器可以正确解释并执行JavaScript ...

  4. ImageLoader在Gridview中的使用

    原理和ImageLoader在Listview中的使用一样,只有下面的几点变化 主页面的布局 <?xml version="1.0" encoding="utf-8 ...

  5. java scoket Blocking 阻塞IO socket通信二

    在上面一节中,服务端收到客户端的连接之后,都是new一个新的线程来处理客户端发送的请求,每次new 一个线程比较耗费系统资源,如果100万个客户端,我们就要创建100万个线程,相当的 耗费系统的资源, ...

  6. 个人项目-Wc-Java

    一.Github项目地址: https://github.com/Heiofungfming/xiaoming01 二.PSP表格 PSP2.1 任务内容 计划完成需要的时间(min) 实际完成需要的 ...

  7. Git【入门】这一篇就够了

    前言 欢迎关注公众号,白嫖原创PDF,也可以催更,微信搜:JavaPub,回复:[666] Git 在生产工作中是使用频率很高的工具,但我发现很多文章只是对它做了简单的提交命令说明,真正遇到 版本冲突 ...

  8. VMware 15安装Ubuntu 16.04并配置环境

    VMware(虚拟机)是指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算机系统,它能在Windows系统上虚拟出多个计算机,每个虚拟计算机可以独立运行,可安装各种软件与应用等 ...

  9. CCNA-Part3 - 数据链路层的趣事 - 走进交换机

    在这篇文章中,会先介绍局域网及其的组件,通过交换机延伸到 TCP/IP 中数据链路层,了解数据的传输介质,以及交换机的发展历程及原理. 最后介绍数据帧的格式. 在阅读后应该了解如下的内容: 什么是局域 ...

  10. 入门大数据---通过Flume、Sqoop分析日志

    一.Flume安装 参考:Flume 简介及基本使用 二.Sqoop安装 参考:Sqoop简介与安装 三.Flume和Sqoop结合使用案例 日志分析系统整体架构图: 3.1配置nginx环境 请参考 ...