springboot实战(汪云飞)学习-1-1
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的更多相关文章
- springboot实战(汪云飞)学习-1-2
java EE开发的颠覆者 spring boot 实战 随书学习-1 接上一篇,Java配置的学习(还是上一篇的项目中,添加新的包和代码): java配置是spring4.x推荐的配置方式,可以完全 ...
- 【IT名人堂】何云飞:阿里云数据库的架构演进之路
[IT名人堂]何云飞:阿里云数据库的架构演进之路 原文转载自:IT168 如果说淘宝革了零售的命,那么DT革了企业IT消费的命.在阿里巴巴看来,DT时代,企业IT消费的模式变成了“云服务+数据”, ...
- SpringBoot实战 之 异常处理篇
在互联网时代,我们所开发的应用大多是直面用户的,程序中的任何一点小疏忽都可能导致用户的流失,而程序出现异常往往又是不可避免的,那该如何减少程序异常对用户体验的影响呢?其实方法很简单,对异常进行捕获,然 ...
- SpringBoot整合阿里云OSS文件上传、下载、查看、删除
1. 开发前准备 1.1 前置知识 java基础以及SpringBoot简单基础知识即可. 1.2 环境参数 开发工具:IDEA 基础环境:Maven+JDK8 所用技术:SpringBoot.lom ...
- apollo客户端springboot实战(四)
1. apollo客户端springboot实战(四) 1.1. 前言 经过前几张入门学习,基本已经完成了apollo环境的搭建和简单客户端例子,但我们现在流行的通常是springboot的客户端 ...
- SpringBoot实战之异常处理篇
在互联网时代,我们所开发的应用大多是直面用户的,程序中的任何一点小疏忽都可能导致用户的流失,而程序出现异常往往又是不可避免的,那该如何减少程序异常对用户体验的影响呢?其实方法很简单,对异常进行捕获,然 ...
- Docker深入浅出系列 | 单机Nginx+Springboot实战
目录 Nginx+Springboot实战 前期准备 实战目标 实战步骤 创建Docker网络 搭建Mysql容器 搭建额度服务集群 搭建Nginx服务 验证额度服务 附录 Nginx+Springb ...
- springboot实战开发全套教程,让开发像搭积木一样简单!Github星标已上10W+!
前言 先说一下,这份教程在github上面星标已上10W,下面我会一一给大家举例出来全部内容,原链接后面我会发出来!首先我讲一下接下来我们会讲到的知识和技术,对比讲解了多种同类技术的使用手日区别,大家 ...
- 《Node.js开发实战详解》学习笔记
<Node.js开发实战详解>学习笔记 ——持续更新中 一.NodeJS设计模式 1 . 单例模式 顾名思义,单例就是保证一个类只有一个实例,实现的方法是,先判断实例是否存在,如果存在则直 ...
随机推荐
- (转)Centos7 yum 源安装nginx
转:https://www.cnblogs.com/fuhai0815/p/8522868.html 一.建立nginx源 vim /etc/yum.repos.d/nginx.repo [nginx ...
- VS2015发布web服务
一.IIS中 ①添加网站 二.VS2015 ①右键解决方案→发布: ②自定义,设置配置文件名称: ③ ④发布 三.IIS中浏览(图片的ip地址是自己,上面的ip是截图别人的,所以不一样)
- 使dialog可拖拽指令
在项目开发过程中,需要支持dialog弹窗可拖拽,则需要对dialog添加指令.具体操作说明如下: (1)在用于存放指令的文件夹内,新建拖拽指令文件夹,例如命名为:el-dragDialog,如下所示 ...
- lnmp环境下 tp3.2 not found
最近将一个lamp环境下使用tp3.2 开发的项目迁移到本地了, 但是在打开项目的时候,提示 not found,经过多方面查找发现是伪静态问题,解决方法如下: 在nginx 域名配置文件我这里是[v ...
- CentOS 7.0 配置防火墙
停用了 iptables. systemctl stop iptables.service 然后来启动 firewalld 吧 systemctl start firewalld.service 给我 ...
- UCenter 与 DIscuz 通信失败的解决方法
问题状况:Discuz 用户无法成功修改头像且帖子中上传的图片无法保存.进入 Discuz 后台检查,一切正常:进入 UCenter 检查后发现在"应用管理"中与 Discuz 论 ...
- HTML表格<tr>行距调整
CSS文件中: .myTable tr{ display:block; /*将tr设置为块体元素*/ margin-bottom:5px;}
- Elasticsearch+Logstash+Kibana搭建日志平台
1 ELK简介 ELK是Elasticsearch+Logstash+Kibana的简称 ElasticSearch是一个基于Lucene的分布式全文搜索引擎,提供 RESTful API进行数据读写 ...
- 高德地图POI采集(URL-API)
新手从零学起,成功跑通,记一下,技术大神们多多指点. ———————————————— 1-概述 POI:兴趣点.对于百度.高德等电子地图来说,一个POI是地图上的一个店铺/商场/小区等等. 这次要解 ...
- JavaScript DoublyLinkedList
function DoublyLinkedList() { var Node = function(element) { this.element = element; this.next = nul ...