今天在开发过程中,发现在@Controller层的类,无法像Servicen层的类那样直接通过@Value("${fileUpload.fileName}")方式注入配置值到对象中。
原因是因为在xml中有如下配置,将配置有@Controller注解的Action类(包名有action)排除掉了。
<context:component-scan base-package="com"> <context:exclude-filter type="regex" expression=".*\.web\..*" /> <context:exclude-filter type="regex" expression=".*\.action\..*" /> </context:component-scan>
后通过如下方法解决:
在spring 3.0中,可以通过使用@value,对一些如xxx.properties文件中的文件,进行键值对的注入,例子如下:
1.首先在applicationContext.xml中加入:
1.首先在applicationContext.xml中加入:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">的命名空间,然后
2.
3.创建test.properties
abc=123
4.
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:test.properties</value> </list> </property> </bean>
3.创建test.properties
abc=123
4.
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping("/admin/images") @Controller public class ImageAdminController { private String imageDir; @Value("${abc}") public void setImageDir(String val) { this.imageDir = val; } }这样就将test.abc的值注入了imageDir中了。
相关推荐
Spring Boot技术知识点:如何理解@Value注解
本文将详细探讨Spring Boot中@Scheduled注解的使用,包括其原理、实现流程、步骤和代码示例。通过本文,读者将能够了解如何在Spring Boot应用中轻松创建和管理定时任务。 # @Scheduled注解简介 在Spring框架中,@...
spring-demo13-注解-@Value.zip
主要介绍了结合SpEL使用@Value-基于配置文件或非配置的文件的值注入-Spring Boot的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
在Spring Boot中,我们可以使用@Before注解来实现AOP的Before通知类型。@Before通知表示在目标方法执行之前,将会先执行切面逻辑。这是一种常用的AOP通知类型,常用于日志记录、安全性检查等方面。 下面详细解释...
Spring Boot系列四 Spring @Value 属性注入使用总结一
在本文中,我们深入了解了Spring Boot中的@Pointcut注解。我们首先介绍了@Pointcut注解的作用和其在AspectJ框架中的重要性。然后,我们详细解释了@Pointcut注解的语法和常用的切点表达式规则。 为了帮助读者更好地...
本篇文章主要介绍了spring中@value注解需要注意的问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring @compenent注解详解,对应我的相关博客。注意,不包含相关的依赖包
spring 支持@RequestBody注解依赖包 、 spring 支持@RequestBody注解依赖包 spring 支持@RequestBody注解依赖包
Spring中@Value注解,需要注意的地方
NULL 博文链接:https://forestqqqq.iteye.com/blog/2235292
在火狐中显示可能会有问题,大家都是... @RequestMapping , @RequestParam , @Resource , @ResponseBody , @RestController , @Scope , @Service , @Validated , @Value , @WebFilter , @WebInitParam , @WebListener
获取springbean对象
Spring Boot技术知识点:如何理解@ConfigurationProperties注解
本篇文章主要介绍了Spring @Bean vs @Service注解区别,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本篇文章主要介绍了详解Spring通过@Value注解注入属性的几种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
用一个最最简单的例子,来讲解:使用Springboot @TypeDiscriminator注解实现多态对象的查询,jackson @JsonTypeInfo注解实现controller多态支持。
关于Spring中@Async注解使用