本文主要介绍了在本地搭建并运行一个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. 数据结构4:顺序表(线性表的顺序存储结构)及C语言实现

    逻辑结构上呈线性分布的数据元素在实际的物理存储结构中也同样相互之间紧挨着,这种存储结构称为线性表的顺序存储结构. 也就是说,逻辑上具有线性关系的数据按照前后的次序全部存储在一整块连续的内存空间中,之间 ...

  2. springboot整合mybatis,redis,代码(五)

    redis注解开发过程中包含许多注解 1.@Cacheable 可以标记在方法上,也可以标记在类上.当标记在方法上时表示该方法是支持缓存的,当标记在类上时则表示该类所有的方法都是支持缓存的.应用到读取 ...

  3. php版 日文半角转全角

    工作需要,写的这个 /* *转载请注明 http://www.cnblogs.com/kclteam/p/5278923.html$str //参数可以是字符串或数组*/ function HkToF ...

  4. Liunx php函数 smtp 发送邮件

    1. 查看防火墙是否开放端口 默认smtp 25 iptables -L -n 如果没有,添加25端口 iptables -A INPUT -p tcp --dport 25 -j ACCEPT ip ...

  5. 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_元数据

    1.元数据简介 全称:metadata 属性:数据表集合 产地:面向 CLR 的编译器在托管模块中生成 2.元数据内部结构及与托管模块的关系 [概述] 托管模块中包含着元数据,元数据是由一组数据表组成 ...

  6. 精简的网站reset和css通用样式库

    一.CSS reset body{ line-height:1.4; color:#; font-family:arial; font-size: 12px; } input,textarea,sel ...

  7. LDAP环境搭建 OpenLDAP和phpLDAPadmin -- yum版

      前言: 前两天公司要求做一个使用LDAP和Kerberos做一个认证授权系统,然后开始学习LDAP相关知识,期间找了不少博客按照步骤来安装,可是很多博客在配置的时候,都会遇到安装过程中一两个问题卡 ...

  8. 关于 java中的SecureRandom在linux中每次生成不同结果

    使用AES算法的时候,会发现下面的代码在windows每次产生确定的结果,但Linux就不同,导致无法正确解密 public static String encrypt(String content, ...

  9. asp.net模板页实现类似jquery中document.ready

    模板页先判断是否有方法DocumentReady,有的话就调用 1.模板页 <script type="text/javascript" language="jav ...

  10. 17.JavaMail

    1.电子邮件 电子邮件是目前网络上使用最多的服务,电子邮件的应用越来越广泛正常的通信往来账号注册时,找回密码时等一般发送的邮件主要可以分解成2大部分一部分是发信人.接信人.主题等邮件标头另外一部分是邮 ...