web 项目集成福昕

这是Project Student的一部分。 其他帖子包括带有Jersey的 Web服务 客户端,带有Jersey的 Web服务服务器 , 业务层 , 具有Spring数据的持久性和分片集成测试数据 。

早先,我们成功地针对持久性/业务层(使用嵌入式H2数据库)和REST服务器/客户端层(使用Jetty服务器)运行了集成测试。 现在是时候将所有内容编织在一起了。

幸运的是,我们已经准备好所有代码并进行了测试。 现在我们要做的就是创建一些配置文件魔术。

局限性

  • 用户身份验证 –尚未进行身份验证的工作。
  • 加密 -尚未对通信进行加密。

容器管理的数据源和JNDI

容器管理的数据源对许多开发人员而言口碑不好,我不确定为什么。 可能与容器管理的持久性(CMP)混淆?

无论如何,容器管理的数据源背后的想法很简单。 您无需弄清楚如何在已部署的系统中维护数据库连接参数-无需修改已部署的Web应用程序(不安全)或从文件系统读取文件(您可能无法访问)等您只需将问题交给维护Web服务器/应用服务器的人员并通过JNDI检索值。

Tomcat和Jetty需要在XML文件中进行手动配置。 像JBoss和GlassFish这样的更高级的应用服务器,使您可以通过漂亮的GUI配置数据源。 其实并不重要,因为您只需要执行一次。 Tomcat 7和Jetty的说明 。

Tomcat的关键点是服务器库位于$ CATALINA_HOME / lib下 ,并且可能不在您期望的位置。 例如,在Ubuntu中,它是/ usr / share / tomcat7 / lib ,而不是/ var / lib / tomcat7 / lib 。 其次,如果未从.war文件中提取META-INF / context.xml文件,则必须将其放在conf / Catalina / localhost / student-ws-webapp.xml (或任何已命名为.war文件的文件)下)。 后者会覆盖前者,因此通常将.war文件设置为在开发环境中运行,然后在测试和生产环境中覆盖配置。

META-INF / context.xml (Tomcat)

<?xml version="1.0" encoding="UTF-8"?>
<Context><Resource name="jdbc/studentDS"auth="Container"type="javax.sql.DataSource"driverClassName="org.postgresql.Driver"url="jdbc:postgresql:student"username="student"password="student"maxActive="20"maxIdle="10"maxWait="-1"factory="org.apache.commons.dbcp.BasicDataSourceFactory" /></Context>

值得注意的是,通过JNDI(作为java.lang.String)传递加密密钥很简单。 对于修改已部署的Web应用程序或访问服务器的文件系统的需求,这具有与前面讨论的相同的好处。

(由于您希望您的实际加密密钥既需要JNDI密钥又需要基于文件系统的盐,因此实现起来要复杂一些,但这在webapp的初始部署过程中很容易处理。)

JPA配置

我们的persistence.xml文件非常少。 我们通常需要两个持久性单元,一个用于JTA事务(生产),一个用于非JTA事务(开发和测试)。

META-INF / persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"version="1.0"><persistence-unit name="studentPU-local"transaction-type="RESOURCE_LOCAL"><provider>org.hibernate.ejb.HibernatePersistence</provider><non-jta-data-source>jdbc/studentDS</non-jta-data-source></persistence-unit>
</persistence>

网页配置

web.xml文件与集成测试中使用的文件几乎相同。 关键区别在于,它为容器提供的数据源获取资源引用。

WEB-INF / web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name>Project Student Webservice</display-name><context-param><param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></context-param><context-param><param-name>contextConfigLocation</param-name><param-value>com.invariantproperties.sandbox.student.config.PersistenceJpaConfigcom.invariantproperties.sandbox.student.config.BusinessApplicationContextcom.invariantproperties.sandbox.student.webservice.config.RestApplicationContext</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>REST dispatcher</servlet-name><servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class><init-param><param-name>spring.profiles.active</param-name><param-value>test</param-value></init-param></servlet><servlet-mapping><servlet-name>REST dispatcher</servlet-name><url-pattern>/rest/*</url-pattern></servlet-mapping><resource-ref><description>Student Datasource</description><res-ref-name>jdbc/studentDS</res-ref-name><res-type>javax.sql.DataSource</res-type><res-auth>Container</res-auth></resource-ref>
</web-app>

弹簧配置

由于有两个更改,因此持久层的Spring配置与以前大不相同。 从风格上讲,由于不再需要处理嵌入式数据库,因此可以使用标准配置文件而不是配置类。

更为重要的更改是,我们已将所有数据源配置移至容器,并且可以从我们的spring配置中删除它。 我们需要指向正确的数据源和持久性单元,仅此而已!

applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jee="http://www.springframework.org/schema/jee"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpahttp://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-3.0.xsd"><context:property-placeholder location="WEB-INF/database.properties" /><context:annotation-config /><!-- we use container-based datasource --><jee:jndi-lookup id="dataSource" jndi-name="${persistence.unit.dataSource}"expected-type="javax.sql.DataSource" /><bean name="entityManagerFactory"class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"><property name="dataSource" ref="dataSource" /><property name="persistenceUnitName" value="${persistence.unit.name}" /><property name="packagesToScan" value="${entitymanager.packages.to.scan}" /><property name="jpaVendorAdapter"><bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /></property></bean><bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" /><bean name="exceptionTranslation"class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /><tx:annotation-driven transaction-manager="transactionManager"proxy-target-class="false" /></beans>

我们的属性文件仅包含数据源的名称,持久性单元名称以及包含JPA批注的要扫描的软件包列表。

WEB-INF / database.properties

# jpa configuration
entitymanager.packages.to.scan=com.invariantproperties.sandbox.student.domain
persistence.unit.dataSource=java:comp/env/jdbc/studentDS
persistence.unit.name=studentPU-local

数据库架构和安全性

最后,集成测试可以使用Hibernate自动创建功能,但是我们应该始终在开发和生产环境中显式维护我们的架构。 除了避免自动化工具以意想不到的方式发挥作用带来的潜在问题之外,这还使我们能够维护基础架构的价值。

在投入生产之前,编写和测试升级和降级脚本非常重要。 如果出现问题,我们总是需要一种可以正常恢复的方法。

更重要的是,我们的数据库架构应始终由Webapp之外的其他用户拥有。 例如,Web应用程序可以将“学生用户”用于“学生所有者”创建的表。 这将防止使用SQL注入的攻击者删除或修改表。

--
-- for security this must run as student-owner, not student-user!
----
-- create an idempotent stored procedure that creates the initial database schema.
--
create or replace function create_schema_0_0_2() returns void as $$
declareschema_version_rec record;schema_count int;
begincreate table if not exists schema_version (schema_version varchar(20) not null);select count(*) into schema_count from schema_version;case schema_countwhen 0 then-- we just created tableinsert into schema_version(schema_version) values('0.0.2');when 1 then-- this is 'create' so we only need to make sure it's current version-- normally we accept either current version or immediately prior version.select * into strict schema_version_rec from schema_version;if schema_version_rec.schema_version  '0.0.2' thenraise notice 'Unwilling to run updates - check prior version';exit;end if;      elseraise notice 'Bad database - more than one schema versions defined!';exit;end case;-- create tables!create table if not exists test_run (test_run_pkey int primary key,uuid varchar(40) unique not null,creation_date timestamp not null,name varchar(80) not null,test_date timestamp not null,username varchar(40) not null);create table if not exists classroom (classroom_pkey int primary key,uuid varchar(40) unique not null,creation_date timestamp not null,test_run_pkey int references test_run(test_run_pkey),name varchar(80) not null);create table if not exists course (course_pkey int primary key,uuid varchar(40) unique not null,creation_date timestamp not null,test_run_pkey int references test_run(test_run_pkey),name varchar(80) not null);create table if not exists instructor (instructor_pkey int primary key,uuid varchar(40) unique not null,creation_date timestamp not null,test_run_pkey int references test_run(test_run_pkey),name varchar(80) not null,email varchar(200) unique not null);create table if not exists section (section_pkey int primary key,uuid varchar(40) unique not null,creation_date timestamp not null,test_run_pkey int references test_run(test_run_pkey),name varchar(80) not null);create table if not exists student (student_pkey int primary key,uuid varchar(40) unique not null,creation_date timestamp not null,test_run_pkey int references test_run(test_run_pkey),name varchar(80) not null,email varchar(200) unique not null);create table if not exists term (term_pkey int primary key,uuid varchar(40) unique not null,creation_date timestamp not null,test_run_pkey int references test_run(test_run_pkey),name varchar(80) not null);-- correction: need to define this!create sequence hibernate_sequence;-- make sure nobody can truncate our tablesrevoke truncate on classroom, course, instructor, section, student, term, test_run from public;-- grant CRUD privileges to student-user.grant select, insert, update, delete on classroom, course, instructor, section, student, term, test_run to student;grant usage on hibernate_sequence to student;return;
end;
$$ language plpgsql;-- create database schema
select create_schema_0_0_2() is null;-- clean up
drop function create_schema_0_0_2();

整合测试

我们可以重复使用来自Web服务服务的集成测试,但是自1以来,我还没有将其复制到该项目中。我讨厌复制代码,最好将集成测试放入服务器和webapp都使用的单独项目中,以及2)配置Jetty来提供JNDI值很容易,但是由于某种原因,记录在案的类抛出了异常,并且花很多时间进行研究还不够重要(此时)。

源代码

  • 可从http://code.google.com/p/invariant-properties-blog/source/browse/student获取源代码。

更正

提供的模式忽略了创建新对象所需的“ hibernate_sequence”。 它必须由“学生”用户定义并可读。

参考: 项目学生:我们的JCG合作伙伴 Bear Giles的Web服务集成 ,位于Invariant Properties博客上。

翻译自: https://www.javacodegeeks.com/2014/01/project-student-webservice-integration.html

web 项目集成福昕

web 项目集成福昕_项目学生:Web服务集成相关推荐

  1. web项目怎么打包上线_高级前端web工程师简历范文,【工作经历+项目经验+自我评价】怎么写...

    高级前端web工程师简历范文,工作经历+项目经验+自我评价怎么写 [网盘下载]100+清新大气简历模板下载: https://zhuanlan.zhihu.com/p/115911695 https: ...

  2. 项目集跟进计划_项目延期,项目经理应该如何补救?

    大部分项目经理都面临过项目延期的情况,特别是在软件开发领域,项目延期情况尤为严重.项目管理者的真正挑战,不是发现问题和记录问题,而是预见问题.控制问题和解决问题. 首先项目经理一般受过良好培训,又经历 ...

  3. 第一次当项目经理压力大_项目经理不想被甩锅,你要这样做进度管理

    小李刚出任项目经理,承接了一个中型软件项目.上任时公司高层再三叮咛他一定要尊重客户,充分满足客户需求. 项目开始比较顺利,但进入到后期,客户频繁的需求变更带来很多额外工作.小李动员大家加班,保持了项目 ...

  4. web前端开发最佳实践_学习前端Web开发的最佳方法

    web前端开发最佳实践 为什么要进行网站开发? (Why web development?) Web development is a field that is not going anywhere ...

  5. pythonweb项目源码下载_最新Python WEB开发在线教育项目之谷粒教育 软件源码齐全...

    [课程内容]项目准备 01.根据模板页面抽象app 02.app当中模型类(表)的抽象(1) 03.app当中模型类(表)的抽象(2) 04.项目的创建和配置 05.创建其余app配置子路由,创建自主 ...

  6. tomcat 项目发布失败原因_项目启动tomcat失败的几种可能原因和解决方法

    总结一下tomcat启动问题,也给自己做个笔记 , 逐渐补充完善. 1.java配置路径有问题,请配置好jdk路径,具体参考java路径的配置吧. 2.项目未添加tomcat驱动, (一般提示The ...

  7. tomcat 启动项目 页面文字乱码_项目通过tomcat部署到服务器,请求数据页面中文乱码问题...

    问题描述:1.将项目部署到服务器之后从页面接收的中文乱码 2.数据库中原有的数据都能正常显示 产生原因:没有对Tomcat服务器和mysql进行配置更改 解决流程: 一. 修改Tomcat配置 更改两 ...

  8. web后端开发学习路线_学习后端Web开发的最佳方法

    web后端开发学习路线 My previous article described how you can get into frontend development. It also discuss ...

  9. 基片集成波导原理_第5讲基片集成波导.ppt

    第5讲基片集成波导 * * 我们对加工的基片集成波导双工器进行了测试,在完成对毫米波转接头插损的测试以后,得到在30.0GHz时这些毫米波转接头和基片集成波导―微带线转换器的插损为-0.75dB/个. ...

最新文章

  1. 如何利用 C# 爬取「京东 - 计算机与互联网图书销量榜」!
  2. 人工智能的产业落地经验!
  3. MYSQL千万级数据量的优化方法积累
  4. 从源码分析DEARGUI之动态特效
  5. [bzoj 4887] [Tjoi2017]可乐
  6. 全面升级 —— Apache RocketMQ 5.0 SDK 的新面貌
  7. sink xxx does not exist
  8. linux做定时数据库备份,Linux实现定时备份数据库
  9. 绿茶影视内容站群-明王优化版吸粉视频站群-轻量级CMS站群
  10. Java报警110_Java入门基础SL110
  11. 计算机学院科技活动策划,计算机学院科技创新活动策划书(7页)-原创力文档...
  12. MySQL(my.ini)配置文件详解
  13. python利用字典破解WIFI密码
  14. 设计模式观察者模式_观察者设计模式教程
  15. python之selenium和xpath简单知晓国服魔兽世界正式服人口普查
  16. 实战:Traefik 高级配置2-2022.1.18
  17. idea java 阿里巴巴_AS 阿里巴巴Java开发规约 CheckStyle-IDEA
  18. 便携式PC用射频器件的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  19. android crash存储位置,在Android手机上保存Crash Log
  20. 第四章:循环神经网络

热门文章

  1. 【dfs】无穷迷宫(jzoj 3924)
  2. 终于,把十大经典排序算法汇总了!(Java实现版)
  3. Spring 事务原理和使用
  4. 浅谈流处理算法 (1) – 蓄水池采样
  5. Java IO: ByteArray和Filter
  6. Java:对double值进行四舍五入,保留两位小数的几种方法
  7. DOMException: Failed to execute ‘appendChild‘ on ‘Node‘: This node type does
  8. 表扬几位积极的同学!
  9. JS中闭包的应用自定义JS模块2
  10. 2020蓝桥杯省赛---java---A---2(既分数组)