spring配置问题

dunerunner 2017-01-13 02:47:20
web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml,classpath:spring-mybatis.xml</param-value>
</context-param>

<servlet>
<servlet-name>spring mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-base.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

spring-base.xml

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="fileEncoding">
<value>UTF-8</value>
</property>
<property name="locations">
<list>
<value>classpath:*.properties</value>
</list>
</property>
</bean>

spring-mvc.xml

<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="zh_CN" />
</bean>

<context:component-scan base-package="test" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

spring-mybatis.xml

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>


TestController

@Controller
@RequestMapping("/testPath")
public class TestController {

@RequestMapping(value = "/testAction")
public String doTest(HttpServletRequest request, HttpServletResponse response, String param) {
System.err.println(request.getParameter("param"));
ComboPooledDataSource dataSource = ContextLoader.getCurrentWebApplicationContext().getBean("dataSource", ComboPooledDataSource.class);
System.err.println(dataSource.getJdbcUrl());
return "/index";
}

}

System.err.println(dataSource.getJdbcUrl());
输出的是
${jdbc.url}

web.xml中改为

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-base.xml,classpath:spring-mybatis.xml</param-value>
</context-param>

<servlet>
<servlet-name>spring mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

再输出就没问题了(把spring-base.xml放到context-param中)
jdbc:mysql://localhost:3306/mydb
求大神帮忙
所有分数献上
...全文
184 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
baij1230 2017-01-14
  • 打赏
  • 举报
回复
楼主没有加上spring-base.xml
什么都不能 2017-01-13
  • 打赏
  • 举报
回复
你的web.xml里是不是还有个spring初始化的listener?<context-param>是给listener用的,没有配置占位符解析器,吧spring-base.xml 放到conext-param里
  • 打赏
  • 举报
回复
问题是啥,看的有点晕
bcsflilong 2017-01-13
  • 打赏
  • 举报
回复
帮你顶一下吧 说实话我没看完太多了

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧