本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性


1 环境搭建

1.1 Maven依赖

目前只用到依赖注入的功能,故以下三个包已满足使用。

    <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.7.RELEASE</spring.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>

1.2 Spring相关配置

在resources目录新建spring-config.xml文件,主要内容如下:

    <!--配置包扫描路径-->
<context:component-scan base-package="com.study"></context:component-scan> <!--启用注解-->
<context:annotation-config></context:annotation-config>

1.3 主要代码

只展示ProductServiceImpl.java和APP.java文件,其他工程代码放到了GitHub上。

package com.study.service.impl;

import com.study.service.ProductService;
import org.springframework.stereotype.Service; /**
* @author frankwin608
* @create 2018-07-09 23:56
* @desc 商品服务实现类
**/
@Service
public class ProductServiceImpl implements ProductService{ @Override
public String queryProductNameById(String id) {
return "Hello Kitty";
}
}
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring-config.xml"});
ProductService productService = (ProductService) context.getBean("productServiceImpl");
String productname = productService.queryProductNameById("23333");
System.out.println("productname = " + productname);
}
}

2 运行结果

3 总结分析

(1)通过context:annotation-config开启支持注解模式

(2)通过context:component-scan设置扫描,base-package指定了扫描的包路径

(3)在ProductServiceImpl类中将该类注解为@Service,该注解将该类标记为一个bean,交由Spring容器进行管理

(4)在App类中,通过读取配置文件并通过上下文的getBean方法获得productServiceImpl的实例,最后调用相应的方法获得结果。

(5)通过引入Spring的自动注入(实例化)的功能减少了自己手动new对象(具体好处参考Spring依赖注入的好处),

4 相关代码下载

Spring实践系列-入门篇(一)GitHub地址

Spring实践系列-入门篇(一)的更多相关文章

  1. Spring Boot -01- 快速入门篇(图文教程)

    Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...

  2. Google C++测试框架系列入门篇:第三章 基本概念

    上一篇:Google C++测试框架系列入门篇:第二章 开始一个新项目 原始链接:Basic Concepts 词汇表 版本号:v_0.1 基本概念 使用GTest你肯定会接触到断言这个概念.断言是用 ...

  3. Google C++测试框架系列入门篇:第二章 开始一个新项目

    上一篇:Google C++测试框架系列入门篇:第一章 介绍:为什么使用GTest? 原始链接:Setting up a New Test Project 词汇表 版本号:v_0.1 开始一个新项目 ...

  4. 深入浅出ASP.NET Core系列(入门篇)

    入门篇 1.1.专题介绍 1.2.环境安装 1.3.创建项目 1.4部署到IIS 1.5准备CentOS和Nginx环境 1.6部署到CentOS 2.1命令行和JSON的配置 2.2Bind建立配置 ...

  5. Spring Cloud Alibaba入门篇

    学习条件 了解web三层架构 熟练应用SSM架构 了解Maven管理工具的使用 熟练使用SpringBoot,以及了解SpringBoot基本原理. 了解部分术语:应用.工具.耦合.负载等 温馨提示: ...

  6. Hibernate Validator实践之一 入门篇

    在后台的业务逻辑中,对数据值的校验在各层都存在(展示层,业务层,数据访问层等),并且各层校验的规则又不尽相同,如下图所示 注:该图片来自于Hibernate Validator官网 在各层中重复的校验 ...

  7. spring boot 学习入门篇【spring boot项目的搭建以及如何加载jsp界面】

    [ 前言]  Spring Boot 简介:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置, ...

  8. 0x00-Kali Linux 系列入门篇

    Kali Linux介绍篇 Kali Linux 官网:https://www.kali.org/ Kali Linux 前身是著名渗透测试系统BackTrack ,是一个基于 Debian 的 Li ...

  9. Spring Data JPA 入门篇

    Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...

随机推荐

  1. FMDB----SQL----数据库

    #pragma mark -- 数库 - (void)createDatabase{ //路径 NSString *path = [NSString stringWithFormat:@"% ...

  2. pacemaker +corosync高可用

    server1:yum install pssh-2.3.1-2.1.x86_64.rpm  crmsh-1.2.6-0.rc2.2.1.x86_64.rpm -yyum install -y pac ...

  3. Spring Boot中JPA如何实现按日期合计

    1. 用queryDsl方法 JPAQueryFactory.select( Projections.fields(OrderCountByDayBean.class, qOrder.amount.s ...

  4. Java RESTful框架的性能比较

    https://colobu.com/2015/11/17/Jax-RS-Performance-Comparison/

  5. hive取等分数据

    %sql select t3.* from ( select t2.* ,row_number() over(partition by t2.pt order by t2.pv) as rn2 fro ...

  6. thinkPHP5.0分页传参

    分页函数paginate(),主要参数有:list_rows每页数量.page当前页.path URL路径.query URL额外参数.fragment URL锚点.type分页l类型 public ...

  7. SSL证书切换

    SSL证书:SSL证书是数字证书的一种,类似于驾驶证.护照和营业执照的电子副本.因为配置在服务器上,也称为SSL服务器证书.SSL 证书就是遵守 SSL协议,由受信任的数字证书颁发机构CA,在验证服务 ...

  8. math.random()方法的使用

    一:导言 以前总是被数字的范围正则搞的头大,在此总结了一下 二:用法 Math.random()函数返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1) 生成n-m,包含n但不包含m的整数: ...

  9. android studio NDK配置

    向您的项目添加 C 和 C++ 代码 本文内容 下载 NDK 和构建工具 创建支持 C/C++ 的新项目 构建和运行示例应用 向现有项目添加 C/C++ 代码 创建新的原生源文件 创建 CMake 构 ...

  10. C++学习笔记:

    一 友元函数:友元函数没有this指针,不是类的成员,在外部定义无需类标识符,引用全局或者静态对象不需要类对象标识符,而访问非静态对象则需要. 二 类继承:如果不指定继承方式,默认是私有继承.但私有继 ...