springmvc 属性放数据库中解决方法 以及 @Controller 中 使用@vlaue无法注入属性值问题详解

来源:互联网 发布:基因大数据公司 编辑:程序博客网 时间:2024/06/03 09:33
springmvc  属性放数据库中解决方法 
    自定义一个DatabaseProperties extends PropertyPlaceholderConfigurer implements ApplicationContextAware
    重写 mergeProperties 方法 加载数据库的配置 生成新的  Properties 类返回
    ==》 applicationContext.xml文件中配置  如果要在@controller中使用  
            @vlaue 注入属性值  就需要在默认的  spring-servlet.xml中也配置以下配置
    <!-- 这里不能用${} -->
   <bean id="dataBaseProperties" class="com.sfiec.oms.common.DatabaseProperties">
<property name="order" value="2" />
<constructor-arg value="构造参数" />
   </bean>

@Controller 中 使用@vlaue无法注入属性值问题详解  转载 http://blog.csdn.net/arsenic/article/details/48287745
1,spring-servlet.xml 是一个,我们的controller就在这里,所以这个里面也需要注入属性文件
org.springframework.web.servlet.DispatcherServlet
这里最终是使用WebApplicationContext parent =WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 创建spring容器,代码在FrameworkServlet中

2,applicationContext.xml 是另外一个,也需要注入属性文件
org.springframework.web.context.ContextLoaderListener

在我们的service中可以拿到@Value注入的值,那是因为我们通常都会把获取属性文件定义在applicationContext.xml中,这样在 Controller中是取不到的,必须在spring-servlet.xml 中把获取属性文件再定义一下
1 0
原创粉丝点击