参考链接:

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. opencv C++图像读取

    int main(){ cv::Mat img=cv::imread("/home/nan/图片/highdeepth/starry.jpg",cv::IMREAD_REDUCED ...

  2. 2、尚硅谷_SSM高级整合_创建Maven项目.avi

    第一步我们新建立一个web工程 这里首先要勾选上enable的第一个复选框 这里要勾选上add maven support 我们在pom.xml中添加sevlet的依赖 创建java web项目之后, ...

  3. ImageLoader在Listview中的使用

    图片加载框架之ImageLoader 1_特点 1)多线程下载图片,图片可以来源于网络,文件系统,项目文件夹assets中以及drawable中等 2)支持随意的配置ImageLoader,例如线程池 ...

  4. vue环境配置脚手架搭建,生命周期,钩子

    Vue项目环境搭建 """ node ~~ python:node是用c++编写用来运行js代码的 npm(cnpm) ~~ pip:npm是一个终端应用商城,可以换国内 ...

  5. Python3-os模块-操作系统的各种接口

    Python3中的os模块提供了一个便携的方式去使用操作系统的相关功能 os.name 返回导入的操作系统相关模块的名字,如 posix(unix/linux),nt(windows)等 os.env ...

  6. 入门大数据---Hive数据查询详解

    一.数据准备 为了演示查询操作,这里需要预先创建三张表,并加载测试数据. 数据文件 emp.txt 和 dept.txt 可以从本仓库的resources 目录下载. 1.1 员工表 -- 建表语句 ...

  7. SpringBoot--使用Mybatis分页插件

    1.导入分页插件包和jpa包 <dependency> <groupId>org.springframework.boot</groupId> <artifa ...

  8. python+opencv检测图像清晰度

    直接上代码,list_jian.txt为待检测图像路径列表 import cv2 import numpy as np import os for path in open("list_ji ...

  9. vue全家桶(1)

    1.环境搭建 1.1.脚手架搭建 1.1.1什么是脚手架 百度搜索一下脚手架长什么样子,它们是这样的: 从百度百科抄过来一段话: 脚手架是为了保证各施工过程顺利进行而搭设的工作平台.如果明白了脚手架在 ...

  10. xshell链接到Linux后启动和关闭tomcat

    1.用xshell.链接到服务器 2.使用ps -ef|grep tomcat 3.停止tomcat服务,使用命令:systemctl  stop adq-dses.service 4.再次查看tom ...