java EE开发的颠覆者 spring boot 实战 随书学习-1

1.学习案例都是maven项目,首先要在eclipse 中配置 maven,主要修改maven的配置文件:配置文件下载链接: https://github.com/liuch0228/springboot-wangyunfei-learn ,最主要的是修改镜像地址,这里使用阿里云的镜像:

 <mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>aliyun</id>
<name>aliyun Maven</name>
<mirrorOf>*</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
</mirrors>

2. 新建一个maven项目

pom文件修改,添加spring相关依赖

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wisely</groupId>
<artifactId>highlight-spring4</artifactId>
<version>0.0.1-SNAPSHOT</version> <!-- 在properties中定义一个名字为java.version的变量,在dependency中引用 -->
<properties>
10 <java.version>1.7</java.version>
11 </properties>

<!-- 添加pom依赖 -->
<dependencies>
<!-- 添加spring-context依赖 -->
15 <dependency>
16 <groupId>org.springframework</groupId>
17 <artifactId>spring-context</artifactId>
18 <version>4.1.6.RELEASE</version>
19 </dependency>
<!-- 添加AOP依赖 -->
22 <dependency>
23 <groupId>org.springframework</groupId>
24 <artifactId>spring-aop</artifactId>
25 <version>4.1.6.RELEASE</version>
26 </dependency>

<!-- 添加AspectJ依赖 -->
28 <dependency>
29 <groupId>org.aspectj</groupId>
30 <artifactId>aspectjrt</artifactId>
31 <version>1.8.5</version>
32 </dependency>
33 <dependency>
34 <groupId>org.aspectj</groupId>
35 <artifactId>aspectjweaver</artifactId>
36 <version>1.8.5</version>
37 </dependency>

</dependencies> <!-- 编译插件依赖 -->
<build>
<plugins>
<plugin>
44 <groupId>org.apache.maven.plugins</groupId>
45 <artifactId>maven-compiler-plugin</artifactId>
46 <version>2.3.2</version>
47 <configuration>
48 <source>${java.version}</source> <!-- 通过变量指定编译源代码使用的jdk版本 -->
49 <target>${java.version}</target> <!-- 通过变量指定目标代码使用的jdk版本 -->
50 </configuration>
51 </plugin>
</plugins>
</build> </project>

改完保存后,鼠标右键单击项目,maven  update project,下载依赖jar包:

3.依赖注入示例:

3.1  FunctionService 类

 package com.wisely.highlight_spring4.ch1.di;

 import org.springframework.stereotype.Service;
/**
* 定义功能类的bean
* @Service注解声明当前FunctionService类是spring管理的一个bean
* 声明bean的注解还有:
* @Repository 数据访问层使用
* @Controller 展现层使用
* @Component 声明被spring管理的组件(也就是自动注入spring容器),没有明确角色
* @author Administrator
*
*/
@Service
public class FunctionService { public String sayHello(String word) {
return "hello " + word + " !";
} }
3.2 UseFunctionService 类
 package com.wisely.highlight_spring4.ch1.di;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 声明使用功能类FunctionService的bean
* @Service 声明UseFunctionService类是spring管理的一个Bean
* @author Administrator
*
*/
@Service
public class UseFunctionService {
/*@Autowired 把FunctionService的实体bean注入到UseFunctionService类中
* 让UseFunctionService具备FunctionService的功能 ———— 组合关系
* */
@Autowired
private FunctionService functionService; public String sayHello(String word) {
return functionService.sayHello(word);
} }
3.3 Java配置类
java配置是spring4.x推荐的配置方式,可以完全替代xml配置,也是springboot推荐的配置方式。Java 配置是通过@Configuration和@Bean来实现的。
@Configuration 声明当前类是一个配置类 ,@ComponentScan 注解自动扫描指定包下的所有使用@Service @Component @Repository 和@Controller的类,将它们注册为spring的Bean
 package com.wisely.highlight_spring4.ch1.di;

 import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* spring配置类
* @Configuration 声明当前类是一个配置类
* @ComponentScan 注解自动扫描指定包下的所有使用@Service @Component @Repository
* 和@Controller的类,并注册为Bean
* @author Administrator
*
*/
@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch1.di")
public class DiConfig { }

主测试类:获取spring容器实例context ,然后拿context获取注册的bean, 调用bean的方法

 package com.wisely.highlight_spring4.ch1.di;

 import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 public class MainTest {

     public static void main(String[] args) {
//1.使用AnnotationConfigApplicationContext最为spring容器,接受一个配置类作为参数
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DiConfig.class);
//2.获得声明配置的UseFunctionService 的bean
UseFunctionService useFunctionService = context.getBean(UseFunctionService.class);
//3.调用bean的方法
System.out.println(useFunctionService.sayHello("world"));
context.close();
} }

运行结果如下:

springboot实战(汪云飞)学习-1-1的更多相关文章

  1. springboot实战(汪云飞)学习-1-2

    java EE开发的颠覆者 spring boot 实战 随书学习-1 接上一篇,Java配置的学习(还是上一篇的项目中,添加新的包和代码): java配置是spring4.x推荐的配置方式,可以完全 ...

  2. 【IT名人堂】何云飞:阿里云数据库的架构演进之路

    [IT名人堂]何云飞:阿里云数据库的架构演进之路 原文转载自:IT168 ​ 如果说淘宝革了零售的命,那么DT革了企业IT消费的命.在阿里巴巴看来,DT时代,企业IT消费的模式变成了“云服务+数据”, ...

  3. SpringBoot实战 之 异常处理篇

    在互联网时代,我们所开发的应用大多是直面用户的,程序中的任何一点小疏忽都可能导致用户的流失,而程序出现异常往往又是不可避免的,那该如何减少程序异常对用户体验的影响呢?其实方法很简单,对异常进行捕获,然 ...

  4. SpringBoot整合阿里云OSS文件上传、下载、查看、删除

    1. 开发前准备 1.1 前置知识 java基础以及SpringBoot简单基础知识即可. 1.2 环境参数 开发工具:IDEA 基础环境:Maven+JDK8 所用技术:SpringBoot.lom ...

  5. apollo客户端springboot实战(四)

    1. apollo客户端springboot实战(四) 1.1. 前言   经过前几张入门学习,基本已经完成了apollo环境的搭建和简单客户端例子,但我们现在流行的通常是springboot的客户端 ...

  6. SpringBoot实战之异常处理篇

    在互联网时代,我们所开发的应用大多是直面用户的,程序中的任何一点小疏忽都可能导致用户的流失,而程序出现异常往往又是不可避免的,那该如何减少程序异常对用户体验的影响呢?其实方法很简单,对异常进行捕获,然 ...

  7. Docker深入浅出系列 | 单机Nginx+Springboot实战

    目录 Nginx+Springboot实战 前期准备 实战目标 实战步骤 创建Docker网络 搭建Mysql容器 搭建额度服务集群 搭建Nginx服务 验证额度服务 附录 Nginx+Springb ...

  8. springboot实战开发全套教程,让开发像搭积木一样简单!Github星标已上10W+!

    前言 先说一下,这份教程在github上面星标已上10W,下面我会一一给大家举例出来全部内容,原链接后面我会发出来!首先我讲一下接下来我们会讲到的知识和技术,对比讲解了多种同类技术的使用手日区别,大家 ...

  9. 《Node.js开发实战详解》学习笔记

    <Node.js开发实战详解>学习笔记 ——持续更新中 一.NodeJS设计模式 1 . 单例模式 顾名思义,单例就是保证一个类只有一个实例,实现的方法是,先判断实例是否存在,如果存在则直 ...

随机推荐

  1. 牛客提高D4t3 清新题

    分析 树上从下往上线性基合并即可 并不需要启发式/xyx 代码 #include<iostream> #include<cstdio> #include<cstring& ...

  2. jmeter之ServerAgent监控资源

    对linux服务器的服务进行压测时,服务器的运行情况可以通过添加插件来观察,而不用使用top命令实时的去看 1.资源准备 2.环境准备 3.资源监控 1.资源准备 可通过该网址下载jmeter所有插件 ...

  3. EntityFramework经典数据访问层基类——增删改查

    namespace StudentSys.DAL { public class BaseService<T>:IDisposable where T:BaseEntity,new() { ...

  4. AtCoder ABC 140D Face Produces Unhappiness

    题目链接:https://atcoder.jp/contests/abc140/tasks/abc140_d 题目大意 有一对 N 个人, 用字符串 S 表示, S[i] 如果等于 'L' 说明这个人 ...

  5. Codeforces Round #410 (Div. 2)B. Mike and strings(暴力)

    传送门 Description Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In ...

  6. 求bit中1的个数有几种做法

    原文 求bit中1的个数有几种做法: - x & (x - 1) - Hamming weight的经典求法,基于树状累加:http://en.wikipedia.org/wiki/Hammi ...

  7. Learning OSG programing---Multi Camera in one window 在单窗口中创建多相机

    在学习OSG提供的例子osgCamera中,由于例子很长,涉及很多细节,考虑将其分解为几个小例子.本文介绍实现在一个窗口中添加多个相机的功能. 此函数接受一个Viewer引用类型参数,设置图形上下文的 ...

  8. 记一次Laravel 定时任务schedul:run未执行的处理

    关于Laravel的任务调度(定时任务)的配置在此不做赘述,跟着官方文档一步一步的操作是不会导致定时任务不能正常工作的. 为保证能及时捕获定时任务执行出现异常的原因,只需在配置系统crontab时指定 ...

  9. ELK日志分析系统之elasticsearch7.x最新版安装与配置

    1.Elasticsearch 1.1.elasticsearch的简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful ...

  10. java_第一年_JavaWeb(4)

    HttpServletResponse对象 向客户端发送数据的方法: 通过getOutputStream()方法得到OutputStream对象,再通过write发送 通过getWriter()方法得 ...