2017-02-03 41 views
1

之前运行验证码我有春MVC控制器这是可能的填充春天@Value

@Controller 
@RequestMapping({ "/user/limits" }) 
public class UserController { 

    @Value("${wsgServiceURL}") 
    private String wsgServiceURL; 
    . 
    . 

其中填充从属性文件wsgServiceURL

是可能的人口在此之前,值运行验证代码

+0

你想要什么样的验证执行?只检查值是否设置或更复杂的东西? – dpr

回答

1

,你可以不喜欢下面,

@Controller 
@RequestMapping({ "/user/limits" }) 
    public class UserController { 

     private String wsgServiceURL; 

     @Autowired 
     public void initProperty(@Value("${wsgServiceURL}") String wsgServiceURL) { 
      if(wsgServiceURL== null) { 
       // Error handling here 
      } 
     } 
    } 
+1

当前的代码工作得很好,我只需要改进它,因为我注入了许多变量并单独验证每个变量,我想集中验证代码并为每次注入运行它 –

2

是的,可以通过@ConfigurationProperties机制使用类型安全配置属性

@Controller 
@RequestMapping({ "/user/limits" }) 
@ConfigurationProperties("uc") 
public class UserController { 
    // will map to uc.wsgServiceURL in property file 
    private String wsgServiceURL; 

您还可以添加验证与@Validated和使用使用JSR-303 javax.validation

+0

是ConfigurationProperties注解在Spring MVC中可用的。我不使用弹簧启动 –

+1

我认为ConfigurationProperties只是Spring引导代码。 – Redlab

+0

感谢您的回复 –