一、注解方式

编写实体类:

package com.example.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**

* Created by Loser100 on 2017/10/9.

*/

@Component

@ConfigurationProperties(prefix = "hello")

public class HelloServiceProperties {
private static final String MSG = "hello";

private String msg = MSG;
public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

}

其中Component注解为自动装载,注解ConfigurationProperties的prefix = “hello”可以在application.yml或application.properties中进行赋值,如:hello.msg = Loser100。若不赋值则默认为”hello”!

二、配置文件方式

编写实体:

package com.example.services;

/**

* Created by Loser100 on 2017/10/9.

*/

public class HelloService {

private String msg;

public String sayHello(){

return "HELLO" + msg;

}
public String getMsg() {

return msg;

}
public void setMsg(String msg) {

this.msg = msg;

}

}

rce.META-INF下新建spring.factories文件,内容为:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=main.java.com.example.services.HelloService

三、使用

使用时直接使用@Autowired标签即可,如:

    @Autowired

private HelloService helloServiceProperties;

String sayHello(){

return helloServiceProperties.sayHello();

}

附上学习工程截图:

SpringBoot配置Bean的两种方式--注解以及配置文件的更多相关文章

  1. 使用工厂方法配置bean的两种方式

    1.使用静态工厂方法: 对应的配置.xml文件: 2.通过实例工厂的方法来配置bean: 对应的xml文件:

  2. SpringBoot配置定时任务的两种方式

    一.导入相关的jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  3. Spring(十三):使用工厂方法来配置Bean的两种方式(静态工厂方法&实例工厂方法)

    通过调用静态工厂方法创建Bean 1)调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中.当客户端需要对象时,只需要简单地调用静态方法,而不需要关心创建对象的具体细节. 2)要声明通过静态 ...

  4. SpringBoot整合Servlet的两种方式

    SpringBoot整合Servlet有两种方式: 1.通过注解扫描完成Servlet组件的注册: 2.通过方法完成Servlet组件的注册: 现在简单记录一下两种方式的实现 1.通过注解扫描完成Se ...

  5. 【SpringBoot】03.SpringBoot整合Servlet的两种方式

    SpringBoot整合Servlet的两种方式: 1. 通过注解扫描完成Servlet组件注册 新建Servlet类继承HttpServlet 重写超类doGet方法 在该类使用注解@WebServ ...

  6. springmvc配置AOP的两种方式

    spingmvc配置AOP有两种方式,一种是利用注解的方式配置,另一种是XML配置实现. 应用注解的方式配置: 先在maven中引入AOP用到的依赖 <dependency> <gr ...

  7. SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)

    前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...

  8. Spring Boot配置过滤器的两种方式

    过滤器(Filter)是Servlet中常用的技术,可以实现用户在访问某个目标资源之前,对访问的请求和响应进行拦截,常用的场景有登录校验.权限控制.敏感词过滤等,下面介绍下Spring Boot配置过 ...

  9. spring 注入bean的两种方式

    我们都知道,使用spring框架时,不用再使用new来实例化对象了,直接可以通过spring容器来注入即可. 而注入bean有两种方式: 一种是通过XML来配置的,分别有属性注入.构造函数注入和工厂方 ...

随机推荐

  1. Dapper系列之三:Dapper的事务修改与删除

    Dapepr的Update和Delete Dapper入门Dapper查询 上两篇文章我们介绍Dapper中添加和查询.本篇文章我们继续讲解修改和删除....如果本篇文章看不懂,请看阅读上两篇Dapp ...

  2. hdu1513 Palindrome

    思路: dp+滚动数组. 实现: #include <iostream> #include <cstdio> #include <string> #include ...

  3. 初试springWebMVC

    最近在尝试配置SpringMVC,发现各种坑. 首先遇到了这个问题. 'component-scan' and its parser class [org.springframework.contex ...

  4. uva10735 Euler Circuit

    题外话:很多混合图问题可以转化为有向图问题(将无向边拆为两条有向边) 本题不行,因为只能经过一次 这种问题能想到网络流.. 复习欧拉回路:入度==出度 和uva1380有点相似,要先给无向边定向.原图 ...

  5. hasOneOf # if (data.otherDescArr.some(_ => '7'.indexOf(_) > -1)) {

    if (data.otherDescArr.some(_ => '7'.indexOf(_) > -1)) { export const hasOneOf = (targetarr, ar ...

  6. 剑指offer8 旋转数组的最小数字

    一种错误写法: class Solution { public: int minNumberInRotateArray(vector<int> rotateArray) { int len ...

  7. PHP21 MVC

    学习目标 MVC设计模式 单一入口机制 MVC的实现 MVC设计模式 Model(模型) 是应用程序中用于处理应用程序数据逻辑的部分.通常模型对象负责在数据库中存取数据. View(视图) 是应用程序 ...

  8. docker nginx 负载均衡

    index1.html <html> <head> <title>lb-test</title> </head> <body> ...

  9. [LUOGU] P2920 [USACO08NOV]时间管理Time Management

    见7.3测试 #include<iostream> #include<algorithm> #include<cstdio> using namespace std ...

  10. openjdk-alpine镜像无法打印线程堆栈和内存堆栈问题

    基于openjdk:8u171-alpine构建的java镜像,使用jstack命令打印线程的时候会提示以下错误: /opt # ps -ef PID USER TIME COMMAND 1 root ...