SpringBoot配置Bean的两种方式--注解以及配置文件
来源:互联网 发布:淘宝软件下载电脑版 编辑:程序博客网 时间:2024/06/05 23:16
一、注解方式
编写实体类:
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(); }
附上学习工程截图:
阅读全文
0 0
- SpringBoot配置Bean的两种方式--注解以及配置文件
- 关于springboot整合mybatis,做关联查询的两种方式(注解和配置文件)
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- SpringBoot开发详解(二)--SpringBoot的配置文件以及注解
- 基于springboot搭建dubbo框架(注解和xml配置两种方式)
- 基于注解@Bean的配置使用方式
- Servlet简介以及通过配置文件和注解两种方式部署Servlet
- springboot通过java bean集成通用mapper的两种方式
- 非注解方式获取spring bean对象以及配置值
- 2、Spring4之Bean的两种配置方式
- C# 修改保存 配置文件的两种方式以及区别
- Spring4----使用注解方式配置bean和bean的属性
- SpringBoot配置文件以及配置项
- EJB会话bean查找和注入注解两种方式
- spring 注解方式配置Bean
- spring 注解方式配置Bean
- Java 通过URL地址下载文本内容到本地文件中
- HDU 2844Coins(涉及包含三种背包问题)
- 我的“View的事件体系”知识点总结
- 文件下载
- HTTP中的重定向和请求转发的区别
- SpringBoot配置Bean的两种方式--注解以及配置文件
- nvidia jetson TX2配置caffe
- android stuido环境运行出错
- LA4329 树状数组
- SprintBoot学习笔记(5):解决中文乱码问题
- 第二章 SQL命令参考-ALTER FILESPACE
- Redis高级特性及应用场景
- leetcode 152 O(n)解决方案
- rtabmap (一)