spring in action 4th --- quick start

读spring in action. 不是特别聪明的人,只好多练习,多实践。读书和思考很容易变得混乱,因为没有实践。因此,虽然花了很多时间,还是要动手记录下来。

  1. 环境搭建
  2. quick-start

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!

时间: 2024-08-09 23:59:45

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

Spring实战-Spring in Action, 4th Edition-2015年第4版本

In Action系列中最畅销的Spring图书,近十万读者学习Spring的共同选择!In Action系列中最畅销的Spring图书,有近10万读者选择本书来学习Spring! Spring框架已经成为Java开发人员的必备知识,而且Spring 3引入了强大的新特性,例如SpEL.Spring表达式语言.IoC容器的新注解以及用户急需的对REST的支持.无论你是刚刚接触Spring还是被Spring 3.0的新特性所吸引,本书都是掌握Spring的最佳选择. 下载地址: Spring in

Spring in Action 4th 学习笔记 之 AOP

先说说为什么需要AOP 最简单的一个例子就是日志记录,如果想记录一些方法的执行情况,最笨的办法就是修改每一个需要记录的方法.但这,真的很笨... 好的方法,应该是通过反射获取方法,然后去匹配,如果需要记录日志,那就调用日志方法即可. 这就是AOP 的Weaving,俗称编织.织入,就是将需要添加的功能编织到现有功能中,而不需要修改现有代码. 另一个例子,不那么大众的需求:我想给一个对象添加方法,怎么实现? 如果有学过js.Python等动态语言,你肯定知道它们支持给对象添加方法,直接添加即可.

Spring in Action 4th 学习笔记

约定: 一.@Xxx Class 表示被@Xxx注解的类.同理还有@Xxx注解的字段或方法. 例如:@Bean Method. 二.@Component Class 同时代指 @Controller.@Service.@Repository. All beans in a Spring application context are given an ID. -- 如果不指明,也会给定一个默认的ID:类名,首字母小写.[]上面这句,不适合XML方式,因为XML方式的默认ID是全路径再加上#{n}

spring in action 4 6.3使用Apache Tiles

配置Tiles视图解析器 需要一个TilesConfigurer bean,该bean用于定位和加载tile定义,还需要一个TilesViewResolver用于将逻辑视图名与tile定义映射起来.在WebConfig.java中 @Configuration @EnableWebMvc @ComponentScan(basePackageClasses={AutoScan.class}) public class WebConfig extends WebMvcConfigurerAdapte

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

这里用到了placeholder特有的一个语言或者将表达形式:${},spring in action 描述如下: In spring wiring ,placeholder values are property names wrapped with ${...},as an exampl,you can resolve the constructor arguments for a BlankDisc in xml like this : <bean id="sgtPeppers&qu

spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入

一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) set 注入.这篇随笔讲的是第一种构造方法注入(Constructor Injection). 其实DI(Dependency Injection)依赖注入你不妨反过来读:注入依赖也就是把"依赖"注入到一个对象中去.那么何为"依赖"呢?依赖就是讲一个对象初始化或者将实例

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

在spring in action中论述了:DispatcherServlet和ContextLoaderListener的关系,简言之就是DispatcherServlet是用于加载web层的组件的上下文.ContextLoadListener是加载 其他组件的上下文. 第一种方式:纯注解的方式: 在spring4.0版本以上,倾向用注解的方式配置DispatcherServlet和ContextLoaderListener.配置方式如下: 思路:一个类只要继承AbstractAnnotati

spring中action和url的对应关系

spring 中, action和url的对应关系 在web.xml中,这样配置: <servlet-mapping > <servlet-name> spring</ servlet-name> <url-pattern> *.html</url-pattern > </servlet-mapping > 由此,我们可以看到,要进入到LoginController,我们需要在浏览器中输入:localhost:8080/springM

SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-003-Pizza例子的基本流程

一. 1. 2.pizza-flow.xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <flow xmlns="http://www.springframework.org/schema/webflow" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="h