springboot EL @Value

一,springboot中

看一下代码:

@Controller
public class HelloController {
    //读取枚举值
    @Value("#{T(com.example.demo.model.EnumList.EnumList.TrackTraceState).Booking.getEnumItem().getItemCN()}")
    private String pwd;
 
    //读取方法
    @Value("#{T(com.example.demo.controller.HelloController).GetName()}")
    private String name;
 
    public static String GetName()
    {
        return "hello";
    }
 
    //读取配置文件
    @Value("${girl.age}")
    private Integer age;
 
    @RequestMapping("/index.do")
    public String say(ModelMap mode) {
 
        User u=new User();
        u.setUserName(name);
        u.setAge(age);
        u.setPassword(pwd);
        mode.addAttribute("user", u);
        return "say";
    }
 
}

application.yml:

girl:
  name: uiw
  age: 33

html:

<div th:text="${user.userName}"></div>
<div th:text="${user.password}"></div>
<div th:text="${user.age}"></div>
<div th:text="${T(com.example.demo.model.EnumList.EnumList.IsApprovalEnum).Approved.getEnumItem().getItemCN()}" />
 

pwd: 调用的枚举值,以及方法返回枚举值的中文名

name:调用的静态方法GetName

age:读取配置文件

最后一个:是thymeleaf 调用后台枚举值的方法。

呈现:

PS:

${} 读取上下文的属性值

#{} 启用Spring表达式,具有运算能力

二,SpringMvc / Module模块引用(例子是SqlHelper)

首先springmvc-servlet.xml中 引入属性文件

<!-- 引入属性文件 -->                    
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:properties/env.properties</value>
                <value>classpath:properties/jdbc.properties</value>
            </list>
        </property>
    </bean>    

继续添加模块的bean

<bean id="sqlHelper" class="com.kintech.SQLServer.SqlHelper" />

调用时使用 @Autowired

@Autowired
    SqlHelper sh;
 
    @RequestMapping(value = "test01.do")
    public String test01(ModelMap mode) {
        sh.getConn();
        return "test/test01";
    }

看一下SqlHelper

原文地址:https://www.cnblogs.com/hanjun0612/p/11102583.html