`
y806839048
  • 浏览: 1108009 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

spring-boot使用注解获取配置文件中的值

阅读更多

ylxy.properties


#其他属性配置
mail.default.from=admin@ylxy.com
mail.debug=false
mail.transport.protocol=smtp
mail.host=192.168.75.100
mail.username=ylxy
mail.password=1234

 

2、启动类中引入配置文件

加入注解:@PropertySource("ylxy.properties"),

如果想引入多个配置文件,则可以引入多个:@PropertySource({"aa.properties","bb.properties","cc.properties"),

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(value = "cn.com.ylxy.business")
@PropertySource("ylxy.properties")
@ImportResource("/spring/spring-data.xml")
public class App {

    public static void main( String[] args ){
       SpringApplication.run(App.class, args);
        System.out.println( "启动完成!" );
    }
}
    
    

 

3、启动项目后,直接可以在其他项目中引用即可

 

public class UserController {
   
    @Value("${mail.default.from}")
    private String mailFrom;
    @Value("${mail.host}")
    private String mailHost;
    @Value("${mail.username}")
    private String mailName;
    /**
     * 跳转到登陆页面
     * @return
     */
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String toIndex(){
        System.out.println(mailFrom);//会输出 admin@ylxy.com
        System.out.println(mailHost);//会输出 192.168.75.100
        System.out.println(mailName);//会输出 ylxy
        return "index";
    }

    
}    
分享到:
评论

相关推荐

    spring-boot-starter-mybatis-spring-boot-1.0.2.zip

    它包含了MyBatis、MyBatis-Spring以及相关的配置类,使开发者能够快速地在Spring Boot项目中使用MyBatis。"spring-boot-starter-mybatis-spring-boot-1.0.2"这个版本即为该起步依赖的一个特定版本,适用于Windows...

    spring-boot-reference2018最新版

    Spring Boot应用可以通过Maven或Gradle进行构建,使用`spring-boot-maven-plugin`或`spring-boot-gradle-plugin`插件,可以实现一键打包成可执行的JAR或WAR文件。 7. **YAML与Properties配置** Spring Boot支持...

    spring-boot-demo

    在Spring Boot项目中,主要的配置文件是`application.properties`或`application.yml`,它们定义了应用的各种属性,包括服务器端口、数据库连接信息、日志级别等。在"spring-boot-demo"项目中,我们可以找到这些配置...

    spring-boot-2.7.0.zip源码

    7. **属性绑定**:`@Value`和`@ConfigurationProperties`注解用于将配置文件中的属性值注入到bean中。`@ConfigurationProperties`允许我们创建强类型的bean来封装配置。 8. **启动器(Starters)**:Spring Boot的...

    springboot2.0.x+dubbo-spring-boot-starter

    例子(springboot+com.alibaba.boot)" 暗示了这是一个示例项目,展示了如何在 Spring Boot 2.0.x 环境中使用 Dubbo 2.6.x 版本。`com.alibaba.boot` 是阿里巴巴提供的一个 Maven 子模块,专门用于简化 Dubbo 在 ...

    spring-boot-2.0.0.M7 源码包

    4. **SpringApplication**:这是Spring Boot应用的入口点,`org.springframework.boot.SpringApplication`类负责加载和初始化Spring应用上下文,处理命令行参数,加载配置文件等。 5. **YAML/Properties配置**:...

    spring-boot-starter-websocket.zip

    1. **配置WebSocket**:在Spring Boot的配置文件(application.yml或application.properties)中,我们需要启用WebSocket并配置相关的端点。 2. **创建WebSocket配置类**:创建一个@Configuration注解的类,使用@...

    spring-boot-04-web-restfulcrud

    在Spring Boot中,我们使用`@RestController`注解创建REST控制器,结合`@RequestMapping`、`@GetMapping`、`@PostMapping`、`@PutMapping`和`@DeleteMapping`等注解来处理不同的HTTP请求方法。 5. **实体类与数据...

    spring-boot源码

    在`spring-boot-autoconfigure`模块中,通过条件注解(如`@ConditionalOnClass`, `@ConditionalOnBean`等)来判断类路径中的类是否存在,进而决定是否加载特定的配置。例如,如果发现`JdbcTemplate`类存在,那么会...

    spring-boot2.0全新教程实例20例.zip

    - [spring-boot-file-upload](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-file-upload):使用 Spring Boot 上传文件示例 - [spring-boot-fastDFS]...

    spring-boot-starter-mybatis-spring-boot-2.2.0.zip

    - **配置文件**:在`application.properties`或`application.yml`中,设置数据源和MyBatis的相关属性,如数据库连接信息、MyBatis的日志级别等。 - **Mapper接口**:创建MyBatis的Mapper接口,该接口继承自`org....

    spring-boot aop

    1. **引入依赖**:在`pom.xml`或`build.gradle`文件中添加Spring AOP和AspectJ的依赖。对于Maven,添加以下依赖: ```xml <groupId>org.springframework.boot <artifactId>spring-boot-starter-aop ``` 2. ...

    spring-boot-starter-mybatis-spring-boot-3.0.0.tar.gz

    - 配置数据源:Spring Boot提供了多种数据源选择,如HikariCP等,通过配置文件(`application.properties`或`application.yml`)设定数据库连接信息。 - 配置MyBatis:定义Mapper接口,编写Mapper XML文件,映射SQL...

    spring-boot-starter-mybatis-spring-boot-1.3.3.zip

    本文将详细介绍如何在Spring Boot项目中集成MyBatis,以及如何获取和使用相关的资源。 一、Spring Boot与MyBatis的集成原理 Spring Boot通过`spring-boot-starter`系列模块简化了依赖管理,`spring-boot-starter-...

    spring-boot-reference-zh

    5. **YAML/Properties 配置**:Spring Boot 使用 YAML 或 Properties 文件进行配置,提供了一种灵活的方式来管理应用的属性,支持外部化配置。 6. **命令行接口(CLI)**:Spring Boot 提供了一个命令行工具,用于...

    Spring Boot源码(spring-boot-2.6.2.tar.gz)

    通过`@EnableAutoConfiguration`注解,Spring Boot能够根据项目中的依赖自动配置Bean。源码中,这个功能主要在`META-INF/spring.factories`文件内的`org.springframework.boot.autoconfigure....

    spring-boot-reference中文版

    在"spring-boot-reference-guide-zh.pdf"中,你会了解到如何创建一个基本的Spring Boot应用,包括使用`@SpringBootApplication`注解来启动Spring Boot应用,以及如何定义主类。此外,指南还会详细讲解如何配置Spring...

    spring-boot-reference.pdf

    根据提供的文件信息,这份文档是一份英文版的Spring Boot参考指南,标题为"spring-boot-reference.pdf",描述提到该文档对于学习Spring Boot及搭建微服务框架具有重要帮助,标签为"spring-boot"。文件内容涉及Spring...

    spring-boot-starter-mybatis-spring-boot-1.3.4.zip

    `spring-boot-starter-mybatis`是Spring Boot为MyBatis提供的一个starter,它包含了MyBatis、MyBatis-Spring、以及相关的依赖,使得开发者可以方便地在Spring Boot项目中使用MyBatis。在`pom.xml`或`build.gradle`...

Global site tag (gtag.js) - Google Analytics