Spring MVC + JPA + Hibernate + GlassFish V3 + MySQL 实现J2EE应用的常用配置模板详解

作者:chaijunkun------感谢原创,感谢作者!!

首先是web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
	<!--设置spring在本应用程序中的配置加载监听器 -->
	<!--监听器会在应用程序启动时自动实例化 -->
	<!--ContextLoaderListener实例化后自动寻找WEB-INF下的applicationContext.xml配置文件 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!--注意这里,如果你的项目中有多个spring上下文DI配置文件 不要写成classpath:applicationContext*.xml的形式 -->
		<!--一定要写成/WEB-INF/classes/...的形式,resin和glassfish在这里貌似不认这样的写法 -->
		<!--我最近在迁移代码的时候就出现了DAO层和Service层无法注入到Controller的问题,罪魁祸首就因为它 -->
		<param-value>/WEB-INF/classes/applicationContext*.xml</param-value>
	</context-param>

	<!--解决spring乱码问题 -->
	<filter>
		<filter-name>encoding</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!--全局Servlet调度配置 -->
	<servlet>
		<!--若设置 servlet-name为[name] -->
		<!--则DispatcherServlet在实例化后会自动加载[name]-servlet.xml -->
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!--显式声明Servlet配置文件路径及其名称 -->
			<param-value>/WEB-INF/classes/spring-servlet.xml</param-value>
		</init-param>
		<!--随服务器一同启动 -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<!--错误页转向配置 -->
	<error-page>
		<error-code>404</error-code>
		<location>/error.html</location>
	</error-page>

	<!--首页文件名设置 -->
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<!--设置持久化单元引用 -->
	<!--此配置仅当Servlet容器支持Servlet 2.5时有效 -->
	<!--此时读取JPA配置文件 文件路径:类路径/META-INF/persistence.xml -->
	<persistence-unit-ref>
		<!--此处的引用名称一般习惯性写法为:persistence/持久化单元名称 -->
		<!--此值被applicationContext.xml使用 -->
		<persistence-unit-ref-name>persistence/stu_infoPU</persistence-unit-ref-name>
		<!--此处的persistence-unit-name值与persistence.xml中persistence-unit的name属性对应 -->
		<persistence-unit-name>stu_infoPU</persistence-unit-name>
	</persistence-unit-ref>
</web-app>

2013年7月12日补充:最近手里有个项目要从tomcat服务器转为用Glassfish服务器。但是仿照本文配置的过程中一直提示错误。
 org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [一个Service接口] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER, lookup=)}
这说明Controller层已经实例化了,只是DAO层和Service层没有实例化。既然这个项目以前在Tomcat能跑起来,说明是配置的问题。虽然查阅了很多资料但都没什么帮助,后来无意间注意到了web.xml中关于ContextLoaderListener的配置,<context-param>中指定的applicationContext文件是以classpath:applicationContext*.xml这样描述的,我改成了/WEB-INF/classes/applicationContext*.xml之后就能正常注入并启动了。为了保险起见,我吧Controller层的初始化配置文件classpath:spring-servlet.xml也弄成了/WEB-INF/classes/spring-servlet.xml。这下就万无一失了。

 

其次是类路径下的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="stu_infoPU" transaction-type="JTA">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<!--设置从应用服务器的JNDI树种查找哪一个具体的JDBC资源 -->
		<jta-data-source>jdbc/stu_info</jta-data-source>
		<properties>
			<property name="dialect" value="org.hibernate.dialect.MySQLDialect"></property>
			<property name="hibernate.show_sql" value="true"></property>
		</properties>
	</persistence-unit>
</persistence>


然后是applicationContext.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/jee
           http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	<!--此配置文件中的所有配置要优先于spring-servlet.xml的配置加载 -->
	<context:component-scan base-package="service,util" />
	<!--启用Spring的任务调度编程模式 使其识别@Scheduled注解 -->
	<task:executor id="executor" pool-size="5" />
	<task:scheduler id="scheduler" pool-size="10" />
	<task:annotation-driven executor="executor" scheduler="scheduler" />
	<!--与持久化相关的配置 -->
	<!--jndi-name用于指示从web.xml中读取哪一个持久化单元 -->
	<!--使用web.xml中的persistence-unit-ref-name所指定的值 -->
	<!--查找链: -->
	<!--通过jdni-name在web.xml中查找persistence-unit-ref节点的子节点 -->
	<!--查找有无与jdni-name所指定名称匹配的persistence-unit-ref-name -->
	<!--根据匹配到的persistence-unit-ref-name得到persistence-unit-name -->
	<!--根据persistence-unit-name在类路径/META-INF/persistence.xml中查找与之匹配的persistence-unit -->
	<!--加载在persistence.xml中相关的配置 并实例化实体管理器工厂类 -->
	<jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/stu_infoPU" />
	<!--开启从应用服务器的JNDI树中自动获取事务性DataSource -->
	<tx:jta-transaction-manager />
	<!--激活@Transactional注解驱动的事务行为 -->
	<!--此处设置的transaction-manager为transactionManager -->
	<tx:annotation-driven transaction-manager="transactionManager" />
</beans>



最后是spring-servlet.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
	<!--针对控制器返回视图字符串的修饰 最终视图路径为 [prefix][returnValue][suffix] -->
	<!--例如prefix="/WEB-INF/" returnValue="admin/login" suffix=".jsp" -->
	<!--最终的URL为 /WEB-INF/admin/login.jsp -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/pages/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<!--此处配置将 controller包和util包及其子包内部 -->
	<!--包含注解 @Service @Component @Controller @Repository的类全部自动注入 -->
	<context:component-scan base-package="controller" />

	<!--为spring设置注解映射路径支持 @RequestMapping -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

	<!--设置spring拦截器 -->
	<mvc:interceptors>
		<!--若mvc:mapping的path相同则按照配置先后形成拦截器链 -->
		<mvc:interceptor>
			<mvc:mapping path="/*" />
			<bean class="interceptor.TestInterceptor1" />
		</mvc:interceptor>
		<mvc:interceptor>
			<mvc:mapping path="/*" />
			<bean class="interceptor.TestInterceptor2" />
		</mvc:interceptor>
	</mvc:interceptors>
</beans>


接下来就是配置GlassFish了。

我将GlassFish安装在了D盘。下面是删除默认域domain1及创建自定义域stu_info的过程

 


C:\Documents and Settings\chaijunkun>d:

D:\>cd glassfish3

D:\glassfish3>cd glassfish

D:\glassfish3\glassfish>cd bin

D:\glassfish3\glassfish\bin>asadmin
使用“exit”以退出, 使用“help”以获得联机帮助。
asadmin> delete-domain domain1
已删除域 domain1。
已成功执行命令 delete-domain。
asadmin> create-domain stu_info
请输入管理员用户名 [按 Enter 键将接受默认值 "admin"/无密码]>【按回车继续】
使用 Admin 的默认端口 4848。
使用 HTTP Instance 的默认端口 8080。
使用 JMS 的默认端口 7676。
使用 IIOP 的默认端口 3700。
使用 HTTP_SSL 的默认端口 8181。
使用 IIOP_SSL 的默认端口 3820。
使用 IIOP_MUTUALAUTH 的默认端口 3920。
使用 JMX_ADMIN 的默认端口 8686。
使用 OSGI_SHELL 的默认端口 6666。
使用 JAVA_DEBUGGER 的默认端口 9009。
无法找到使用在 [D:\glassfish3\glassfish\lib\templates\locales\zh_CN\index.html]
中指定的语言环境 [zh_CN] 的文件。使用默认的 (en_US) index.html。
自签名 X.509 服务器证书的标识名为:
[CN=MicrosoftUser,OU=GlassFish,O=Oracle Corporation,L=Santa Clara,ST=Ca
lifornia,C=US]
自签名 X.509 服务器证书的标识名为:
[CN=MicrosoftUser,OU=GlassFish,O=Oracle Corporation,L=Santa Clara,ST=Ca
lifornia,C=US]
找不到域初始化程序,将绕开自定义步骤
已创建域 stu_info。
域 stu_info 管理员端口为 7982。
域 stu_info 允许管理员以用户 "admin"(无密码)身份登录。
已成功执行命令 create-domain。

建立好域之后不急着开启域,由于要是用MySQL数据库,需要先将MySQL数据库的Connector拷贝到域的库目录下。

我用的MySQL connector是mysql-connector-java-5.1.17-bin.jar,MySQL的官方提供了下载。

由于我将GlassFish V3安装到了D:\glassfish3,所以需要将该文件拷贝到D:\glassfish3\glassfish\domains\stu_info\lib\目录下。

拷贝完成后,该connector的完整文件名应为:D:\glassfish3\glassfish\domains\stu_info\lib\mysql-connector-java-5.1.17-bin.jar

下面回到命令行启动新建立的stu_info域

asadmin> start-domain stu_info
正在等待 stu_info 启动 ...............
成功启动了 domain : stu_info
domain  位置: D:\glassfish3\glassfish\domains\stu_info
日志文件: D:\glassfish3\glassfish\domains\stu_info\logs\server.log
管理端口: 4848
已成功执行命令 start-domain。
asadmin>

此时打开浏览器,输入:http://127.0.0.1:4848

进入GlassFish V3的GUI管理系统

 

左侧“日常任务”导航栏中找到JDBC配置区域,由于是新建的域,只存在默认的JDBC资源。需要新建一个适合应用的JDBC资源。

 

点击JDBC连接池

 

 

点击右侧新建按钮,填入如下信息

 

 

点击“下一步”

 

 

选择数据源类名称,勾选Ping启用选项。向下拖动,还有其他属性。这里只保留关键的属性,很简单,就不一一解释了

 

 

点击右下角的“完成”。如果没问题,会在页面上方提示“Ping成功”

 

接下来添加JDBC数据源

 

点击JDBC资源,在右侧点击“新建”按钮

 

 

按照约定俗成的习惯,这里的JNDI名称以jdbc开头,加“/”后接JDBC连接池名称。池选择刚刚新建的stu_info连接池,

点击“确定”按钮

 

 

新的JDBC数据源就添加好了。

接下来就是用MyEclipse将写好的Web应用部署到GlassFish上了。部署好后再开启stu_info域。在左侧“应用程序”中会看到所部署的Web应用。

 


配置好这些以后,如果Web应用程序没有问题的话,访问的路径为http://127.0.0.1:8080/stu_info/。URL中的stu_info是由应用程序名称决定的。

如果想去掉这个名称,在浏览器中直接输入http://127.0.0.1:8080/就能访问Web应用程序则应进行如下配置:

点击日昌任务下配置节点,选择server-config 配置项。找到虚拟服务器,选中名称为server的虚拟服务器



在右侧出现虚拟服务器配置界面:



其中默认Web模块是空的。在下拉列表中选择刚刚部署的Web应用程序,点击“应用”,即可生效。此时即可直接访问:

http://127.0.0.1:8080

 


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值