博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA Eclipse使用Maven构建web项目详解(SSM框架)
阅读量:7247 次
发布时间:2019-06-29

本文共 4108 字,大约阅读时间需要 13 分钟。

tips: 启动项目后,welcome-file的链接即为测试用例

  1. 部署maven web项目
    • pom.xml添加webapp依赖:
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet
jsp-api
2.0
provided
javax.servlet
jstl
1.2
provided
  • 配置jdk版本,在build->plugins节点中添加:
org.apache.maven.plugins
maven-compiler-plugin
1.7
1.7
  1. 整合spring
  2. 整合log4j
  3. 配置dataSource

com.alibaba
druid
1.0.7

“`

* [编程方式取得Spring上下文的Properties]

driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8
  1. 整合mybatis
    • 在此,应完成的有:

      1. 事务管理
      2. 扫描mapper包

  2. 整合mybatis-generator
    • mybatis-spring的版本换成1.3.0,否则会报错:
      java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L
  3. 用junit测试一下
    • Junit测试用例见test下的cn.jxnu.mapper.UserMapperTest
  4. 整合springMVC

    一、[编程方式取得Spring上下文的Properties]

    在Spring初始化时,可以使用Properties配置器把properties文件装载到Spring的上下文中。

xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation=“http://www.springframework.org/schema/context                  http://www.springframework.org/schema/context/spring-context-3.0.xsd”      

这样在Spring的配置文件中可以用表达式来获得load进来的properties内容,例如:

有时候我们在程序中也需要用到这些配置,那么如何取值,显然不能使用${}方式的。

这时要决定用什么方式来获取properties了,最方便的当然是直接读取文件,此处省略。
如果程序一定要用通过Spring加载的properties,那么我们首先要得到Context了。

1、FileSystemXmlApplicationContext——从指定的目录中加载:

ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");

2、ClassPathXmlApplicationContext——从classpath路径加载:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

3、参照, 通过web.xml来获取Context。

4、在servlet中获取。

ServletContext servletContext = servlet.getServletContext();     WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

然后可以通过相应的bean去访问需要的properties(spring配置文件中${}方式设置到bean里)的值,这里不记录。

用PropertyPlaceholderConfigurer在加载上下文的时候暴露properties

hello.properties

表明PropertyPlaceholderConfigurer是承担properties读取任务的类。

下面的类继承PropertyPlaceholderConfigurer,通过重写processProperties方法把properties暴露出去了。

import java.util.HashMap;  import java.util.Map;  import java.util.Properties;  import org.springframework.beans.BeansException;  import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;  import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;  public class CustomizedPropertyConfigurer extends PropertyPlaceholderConfigurer {
private static Map
ctxPropertiesMap; @Override protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)throws BeansException { super.processProperties(beanFactory, props); //load properties to ctxPropertiesMap ctxPropertiesMap = new HashMap
(); for (Object key : props.keySet()) { String keyStr = key.toString(); String value = props.getProperty(keyStr); ctxPropertiesMap.put(keyStr, value); } } //static method for accessing context properties public static Object getContextProperty(String name) { return ctxPropertiesMap.get(name); } }

这样此类即完成了PropertyPlaceholderConfigurer的任务,同时又提供了上下文properties访问的功能。

于是在Spring配置文件中把PropertyPlaceholderConfigurer改成CustomizedPropertyConfigurer

最后在程序中我们便可以使用CustomizedPropertyConfigurer.getContextProperty()来取得上下文中的properties的值了。

转载地址:http://pwbbm.baihongyu.com/

你可能感兴趣的文章
货拉拉完成 3 亿美元 D 轮融资,2018 年全年业务量增长将近 200% ...
查看>>
Ceph Storage Cluster(CEPH存储集群) Configuration配置
查看>>
洛谷 P3369 BZOJ 3224 【模板】普通平衡树(Treap/SBT)
查看>>
logging模块配置笔记
查看>>
NLP 语料分类不均衡的解决办法
查看>>
【云周刊】第187期:阿里推出 PolarFS 分布式文件系统:将存储与计算分开,提升云数据库性能...
查看>>
1134. Vertex Cover (25)
查看>>
神经网络的激活函数总结
查看>>
[Guava源码日报](10)Iterables
查看>>
Google Jib 即将迎来正式版
查看>>
python高级特性-迭代
查看>>
Android属性设置android:noHistory="true"
查看>>
Jackson对泛型的序列化和反序列化方法汇总
查看>>
玩完自动驾驶,现代又钻研起了机器骨骼和智能房间
查看>>
如何自学人工智能?
查看>>
Linux系统使用普通命令删除不掉的文件处理方法
查看>>
canon iPF 系列保养墨盒清零方法
查看>>
Emulating Neural Synapses through AI
查看>>
Oracle in与exists语句
查看>>
字体大宝库:20款免费的情人节字体
查看>>