读spring in action.

  1. 环境搭建
  2. quick-start依赖注入
  3. 面向切面

1.环境搭建

  • jdk1.8
  • gradle 2.12
  • Intelij idea 2016.2.1

1.1创建一个gradle项目

在idea中,new -> project -> gradle 创建一个空项目。创建成功后修改build.gradle :

group 'com.test'
version '1.0-SNAPSHOT' apply plugin: 'java'
apply plugin: 'war' sourceCompatibility = 1.8
targetCompatibility = 1.8 repositories {
mavenLocal()
mavenCentral()
} dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11' }

根目录下创建.gitignore:

# Created by .ignore support plugin (hsz.mobi)
.idea/
.gradle/
build/
out/
*/build/

1.2 quick start

spring的核心是依赖注入,那么简单的做一个入门测试。

在项目名上右键,new->module->gradle->创建一个java项目quick-start. 修改生产的build.gradle:

group 'com.test'
version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.8 repositories {
mavenLocal()
mavenCentral()
} dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11' compile 'org.springframework:spring-context:4.3.2.RELEASE'
}

这里参考:http://projects.spring.io/spring-framework/#quick-start 的案例.

在quick-start module下创建一个package src/main/java,在java文件夹上右键,Mark Directory as -> Sources Root.

这时候idea的项目配置还没有刷新,需要手动点击一下刷新:

这时候,idea就可以resolve quick-start这个项目以及他的dependency了。

添加spring-context会添加其他依赖:

dependencies {
compile 'org.springframework:spring-context:4.3.2.RELEASE'
}

1.2.1 Hello World

我们来创建打印消息的组件。MessagePrinter打印一个MessageService的实例的信息。:

创建接口com.test.hello.MessageService:

package com.test.hello;

/**
* Created by rmiao on 8/15/2016.
*/
public interface MessageService {
String getMessage();
}

创建组件com.test.hello.MessagePrinter:

package com.test.hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; /**
* Created by rmiao on 8/15/2016.
*/
@Component
public class MessagePrinter { final private MessageService service; @Autowired
public MessagePrinter(MessageService service){
this.service = service;
} public void printMessage(){
System.out.println(this.service.getMessage());
}
}

@Component声明MessagePrinter是一个bean,由spring容器来管理。

@Autowired 这里是构造器注入,会根据构造器参数的类型和参数名来将spring容器中的bean注入构造器。

针对MessagePrinter的注入参数,我们需要一个MessageService的实现:

MessageService mockMessageService(){
return () -> "Hello World!";
}

下面开始启动spring容器来测试这个打印消息组件:

创建com.test.hello.Application:

package com.test.hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* Created by rmiao on 8/15/2016.
*/
@Configuration
@ComponentScan
public class Application { @Bean
MessageService mockMessageService(){
return () -> "Hello World!";
} public static void main(String[] args){
ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
MessagePrinter printer = context.getBean(MessagePrinter.class);
printer.printMessage();
}
}

@Configuration来声明Application是一个配置类,相当于xml配置文件。这里只配置了一个bean mockMessageService.

@Bean 用来声明一个bean并交由spring容器管理。相当于xml配置文件中<bean>. 这种方式表示声明一个MessageService的类的bean,bean id为mockMessageService。

@ComponentScan来声明spring容器扫描范围,这种方式表示扫描Application所在包以及子包下的所有类,然后将识别到的bean放到spring容器中。

AnnotationConfigApplicationContext用来创建spring容器。getBean来获取容器中的bean。

最终,打印Hello World!

1.2.2 Aop面向切面

spring的另一个强大特性是面向切面编程。可以在任意方法的调用前后进行一些操作。比如记录日志:

添加aop依赖:

dependencies {

    compile group: 'org.springframework', name: 'spring-aop', version: '4.3.2.RELEASE'
compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.8.9' }

添加logback日志组件依赖:

dependencies {

    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.7'
compile group: 'ch.qos.logback', name: 'logback-core', version: '1.1.7'
compile group: 'org.codehaus.groovy', name: 'groovy', version: '2.4.7' }

在src/main/resources下创建logback.groovy:

import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.core.ConsoleAppender
import ch.qos.logback.core.rolling.FixedWindowRollingPolicy
import ch.qos.logback.core.rolling.RollingFileAppender
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy import static ch.qos.logback.classic.Level.DEBUG
import static ch.qos.logback.classic.Level.INFO appender("STDOUT", ConsoleAppender) {
encoder(PatternLayoutEncoder) {
pattern = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n"
}
}
appender("FILE", RollingFileAppender){
file = "quick-start/log/project.log"
encoder(PatternLayoutEncoder) {
pattern = "%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
}
rollingPolicy(FixedWindowRollingPolicy) {
fileNamePattern = "quick-start/log/project.%i.log.zip"
minIndex = 1
maxIndex = 10
}
triggeringPolicy(SizeBasedTriggeringPolicy) {
maxFileSize = "2MB"
}
} // specify level
logger("com.test.hello", DEBUG)
//By default, the level of the root level is set to DEBUG
root(DEBUG, ["STDOUT"])

下面开始面相切面编程。

我们想要在消息打印组件的前后做一些工作,但又不想要修改打印组件的内容。那么可以使用@Aspect:

创建:com.test.hello.Monitor.java:

package com.test.hello;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; /**
* Created by rmiao on 8/15/2016.
*/
@Aspect
@Component
public class Monitor { private final Logger logger = LoggerFactory.getLogger(Monitor.class); @Pointcut("execution(* com.test.hello.MessagePrinter.printMessage())")
public void message(){} @Before(value = "message()")
public void pre(){
logger.info("before print.");
} @After(value = "message()")
public void after(){
logger.info("after print.");
} }

@Aspect表示这是一个aop切面。等价于xml配置中的<aop>

@Pointcut表示切面的匹配方式

@Before表示切面调用前执行

@After表示切面调用后执行。

要使上述的配置生效,还需开启切面,在配置类中声明@EnableAspectJAutoProxy:

package com.test.hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*; /**
* Created by rmiao on 8/15/2016.
*/
@Configuration
@ComponentScan
@EnableAspectJAutoProxy
public class Application { @Bean
MessageService mockMessageService(){
return () -> "Hello World!";
} public static void main(String[] args){
ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
MessagePrinter printer = context.getBean(MessagePrinter.class);
printer.printMessage();
}
}

运行:

八月 15, 2016 9:13:45 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@31cefde0: startup date [Mon Aug 15 21:13:45 CST 2016]; root of context hierarchy
21:13:49.278 [main] INFO c.t.h.Monitor - before print.
Hello World!
21:13:49.306 [main] INFO c.t.h.Monitor - after print.

spring in action 4th --- quick start的更多相关文章

  1. 1、Spring In Action 4th笔记(1)

    Spring In Action 4th笔记(1) 2016-12-28 1.Spring是一个框架,致力于减轻JEE的开发,它有4个特点: 1.1 基于POJO(Plain Ordinary Jav ...

  2. Spring in Action 4th 学习笔记 之 AOP

    前提:本文中的AOP仅限于Spring AOP. 先说说为什么需要AOP 最简单的一个例子就是日志记录,如果想记录一些方法的执行情况,最笨的办法就是修改每一个需要记录的方法.但这,真的很笨... 好的 ...

  3. Spring in Action 4th 学习笔记

    约定: 一.@Xxx Class 表示被@Xxx注解的类.同理还有@Xxx注解的字段或方法. 例如:@Bean Method. 二.@Component Class 同时代指 @Controller. ...

  4. ssh整合随笔(注解方式,Spring 管理action)

    Web.xml<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...

  5. Spring in action记录

    最近一段时间重新学习了一遍SPRING,现在对这些笔记整理一下,一来算是对之前的学习有一个交代,二来当是重新学习一次,三来可以留下备份 这次学习中以SPRING IN ACTION 4这学习资料,整书 ...

  6. 学习spring in action 第一天

    这段时间,开始学习java吧,因为C sharp 学习了java的大量语法格式,所以,留意下,就不会错了,java 有的c sharp也有,而且之前我也学习过java的桌面开发,但是一下子上来就要自己 ...

  7. spring in action学习笔记十五:配置DispatcherServlet和ContextLoaderListener的几种方式。

    在spring in action中论述了:DispatcherServlet和ContextLoaderListener的关系,简言之就是DispatcherServlet是用于加载web层的组件的 ...

  8. spring in action 学习笔记十四:用纯注解的方式实现spring mvc

    在讲用纯注解的方式实现springmvc之前先介绍一个类:AbstractAnnotationDispatcherServletInitializer.这个类的作用是:任何一个类继承AbstractA ...

  9. spring in action 学习十一:property placeholder Xml方式实现避免注入外部属性硬代码化

    这里用到了placeholder特有的一个语言或者将表达形式:${},spring in action 描述如下: In spring wiring ,placeholder values are p ...

随机推荐

  1. angularJs 指令的理解

    指令: 可以把指令理解成为特定的DOM 上运行的函数,指令扩展了这个元素的功能: 例如:ng-click--在DOM元素上绑定了一个click事件的监听,并在监听到事件时运行angular表达式 自定 ...

  2. 安卓刷机--fastboot线刷

    首先需要下载fastboot.exe,copy到system32文件夹下. 对于安卓系统的智能手机,同时按住开机键和音量减键,或手机连上电脑,输入adb reboot bootloader进入fast ...

  3. python字符串的使用

    之前在网上看了关于python最基础的一些教程,看着都通俗易懂,但是在写的过程中却感觉还是很生涩.关于字符串的使用还是应该多写多练!如何将“teacher_id = 123 #老师ID”转换成字典或者 ...

  4. WPF +MVVM(Caliburn.Micro)项目框架

    最近做了一个软件,这个软件不是网站,但是与HTML,AJAX等技术密切相关,也不是只有单纯的数据库增删改查,还涉及到线程协调,比较复杂的文本处理…… 这样的软件,用OA,ERP的框架显然是不合适的,因 ...

  5. .NET项目版本号的小随笔

    [题外话] 一直以来都对.NET项目中的几个版本号(AssemblyVersion.AssemblyFileVersion.AssemblyInformationalVersion)以及版本号中的Re ...

  6. 【单元测试】NUint使用详解及Visual Studio配置

    阅读目录 什么是单元测试? 为什么使用单元测试? NUint使用详解: 示例 属性 断言 简单测试 VS配置: External Tools Visual Nunit 2010 NUnit Test ...

  7. Please Call Me NIO

    与其他语言相比,Java的IO功能显得异常复杂,各种流操作,通过程序员多次封装才可以达到操作文件的目的.自从jdk1.4之后,java提供了一个新的api完成IO操作,人称New IO(NIO),使用 ...

  8. Windows Azure Storage (18) 使用HTML5 Portal的Azure CDN服务

    <Windows Azure Platform 系列文章目录> Update:2015-04-15 如果读者使用的是国内由世纪互联运维的Azure China服务,请参考笔者的文档:Azu ...

  9. 60分钟Python快速学习(给发哥一个交代)

    60分钟Python快速学习 之前和同事谈到Python,每次下班后跑步都是在听他说,例如Python属于“胶水语言啦”,属于“解释型语言啦!”,是“面向对象的语言啦!”,另外没有数据类型,逻辑全靠空 ...

  10. 3.用Redis Desktop Manager连接Redis

    Redis Desktop Manager是Redis图形化管理工具,方便管理人员更方便直观地管理Redis数据. 然而在使用Redis Desktop Manager之前,有几个要素需要注意: 一. ...