@Value("#{}")与@Value("${}")的区别以及用法

摘要:
当然还有可以表示常量
1 packagecom.ieou.capsule_basic_info.util;
2 
3 
4 importorg.springframework.beans.factory.annotation.Value;
5 importorg.springframework.stereotype.Component;
6 importorg.springframework.web.bind.annotation.RequestMapping;
7 importorg.springframework.web.bind.annotation.RestController;
8 
9 @RestController
10 @RequestMapping("/login")
11 @Component
12 public classTestController {
13 
14     /************************   @Value("#{}")   *********************************/
15 
16     //这是  @Value("#{}")
17 
18     @Value("#{1}")
19     private int number; //获取数字 1
20 
21     @Value("#{'Spring Expression Language'}") //获取字符串常量
22     privateString str;
23 
24 
25     @Value("#{dataSource.jdbcUrl}") //获取bean的属性
26     privateString jdbcUrl;
27 
28     /**************************   @Value("${}")  ***********************************/
29 
30     //@Value("${}") 有两种用法
31     //1.从properties配置文件中读取数据
32     @Value("${test_value}") //获取字符串常量
33     private String str1;  //str1 = hello world
34     
35     //2.为方法参数赋值
36     @Value("${test_value}")
37     public voidtest(String s){
38         System.out.println(s);  //s = hello world
39 }
40     
41     
42 } 

@Value("#{}") SpEL表达式
@Value("#{}") 表示SpEl表达式通常用来获取bean的属性,或者调用bean的某个方法。当然还有可以表示常量

@Value("#{}")与@Value("${}")的区别以及用法第1张

免责声明:文章转载自《@Value("#{}")与@Value("${}")的区别以及用法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇根据wsdl反向生成webservice服务端(3种方法)JAVA线程池调优下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

spring5 源码深度解析----- AOP的使用及AOP自定义标签

我们知道在面向对象OOP编程存在一些弊端,当需要为多个不具有继承关系的对象引入同一个公共行为时,例如日志,安全检测等,我们只有在每个对象里引入公共行为,这样程序中就产生了大量的重复代码,所以有了面向对象编程的补充,面向切面编程(AOP),AOP所关注的方向是横向的,不同于OOP的纵向。接下来我们就详细分析下spring中的AOP。首先我们从动态AOP的使用...

spring基于注解的IOC配置 知识点

明确:注解配置和xml配置要实现的功能都是一样的,都是要降低程序间的耦合。只是配置的形式不一样。 配置注解扫描的包:声明到指定的包下去进行扫描,如果发现类上有对应的注解,将其装配到容器中 <context:component-scan base-package="cn.XXX"></context:component-scan>...

Spring(七)Spring中的四种增强和顾问

Spring中的四种增强有那四种? 前置增强 后置增强 环绕增强 异常增强 先编写接口和实体类 ISomeService和SomeServiceImpl package demo10; /** * Created by mycom on 2018/3/8. */ public interfaceISomeService { public vo...

关联查询报错org.apache.ibatis.builder.BuilderException: Ambiguous collection type for property 'episodeList'. You must specify 'javaType' or 'resultMap'.

三表关联查询报错, 单表不报错, 之前写过一遍没报错,后来查出问题所在了,原来是insert语句多写了半格括号"(" 这个问题查了很久很久... 报错信息如下 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'vi...

设置Shiro超时时间

1、在shiro的配置文件中配置。 <!-- 会话管理器 --> <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> <!-- 设置超时时间 --> <prope...

shiro源码解析

一、web.xml 文件中配置的 DelegatingFilterProxy 的 <filter-name>为啥与Spring文件中配置的ShiroFilterFactoryBean的Bean id 保持一致? <filter> <filter-name>shiroFilter</filter-n...