---
title: "Struts2+Spring+Hibernate In Action（二）"
author: "kyo100900"
date: "2007-11-01T00:30:30Z"
tags: []
categories: ["企业架构"]
slug: "struts2-spring-hibernate-in-action"
draft: false
lastmod: "2007-11-01T00:30:30Z"
id: 137150
url: "https://superleo.iteye.com/blog/137150"
---

当所有的Service配置好了，可以当他们统统配置在Spring这个IoC容器中，供Struts2使用。

以下是本程序中Spring的配置代码中的一部分：

xml 代码

001. xml version="1.0" encoding="UTF-8"?&gt;
002. "http://www.springframework.org/dtd/spring-beans-2.0.dtd"&gt;
003. &lt;beans&gt;
004. &lt;bean id="propertyConfigurer"
005. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
006. &lt;property name="locations"&gt;
007. &lt;list&gt;
008. &lt;value&gt;classpath:leo.confvalue&gt;
009. list&gt;
010. property&gt;
011. &lt;property name="fileEncoding" value="utf-8" /&gt;
012. &lt;property name="ignoreResourceNotFound" value="true" /&gt;
013. bean&gt;
014. &lt;bean id="dataSource"
015. class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
016. &lt;property name="driverClassName" value="${jdbc.driver}" /&gt;
017. &lt;property name="url" value="${jdbc.url}" /&gt;
018. &lt;property name="username" value="${jdbc.username}" /&gt;
019. &lt;property name="password" value="${jdbc.password}" /&gt;
020. bean&gt;
021. &lt;bean id="sessionFactory"
022. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
023. &lt;property name="configurationClass"&gt;
024. &lt;value&gt;org.hibernate.cfg.AnnotationConfigurationvalue&gt;
025. property&gt;
026. &lt;property name="hibernateProperties"&gt;
027. &lt;value&gt;
028. hibernate.dialect=org.hibernate.dialect.MySQLDialect
029. hibernate.show\_sql=true hibernate.format\_sql=true
030. value&gt;
031. property&gt;
032. &lt;property name="dataSource" ref="dataSource" /&gt;
033. &lt;property name="annotatedClasses"&gt;
034. &lt;list&gt;
035. &lt;value&gt;com.leo.po.Uservalue&gt;
036. &lt;value&gt;com.leo.po.Articlevalue&gt;
037. &lt;value&gt;com.leo.po.Mailvalue&gt;
038. &lt;value&gt;com.leo.po.Logvalue&gt;
039. list&gt;
040. property&gt;
041. bean&gt;
042. &lt;bean id="hibernateTemplate"
043. class="org.springframework.orm.hibernate3.HibernateTemplate"&gt;
044. &lt;property name="sessionFactory"&gt;
045. &lt;ref local="sessionFactory" /&gt;
046. property&gt;
047. bean&gt;
048. &lt;bean id="transactionManager"
049. class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
050. &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
051. bean&gt;
052. &lt;bean
053. class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /&gt;
054. &lt;bean
055. class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor"&gt;
056. &lt;property name="transactionInterceptor"
057. ref="transactionInterceptor" /&gt;
058. bean&gt;
059. &lt;bean id="transactionInterceptor"
060. class="org.springframework.transaction.interceptor.TransactionInterceptor"&gt;
061. &lt;property name="transactionManager" ref="transactionManager" /&gt;
062. &lt;property name="transactionAttributeSource"&gt;
063. &lt;bean
064. class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" /&gt;
065. property&gt;
066. bean&gt;
067. &lt;bean id="userService"
068. class="com.leo.service.impl.UserServiceImpl"&gt;
069. &lt;constructor-arg&gt;
070. &lt;value&gt;com.leo.po.Uservalue&gt;
071. constructor-arg&gt;
072. &lt;property name="hibernateTemplate" ref="hibernateTemplate" /&gt;
073. bean&gt;
074. &lt;bean id="articleService"
075. class="com.leo.service.impl.ArticleServiceImpl"&gt;
076. &lt;constructor-arg&gt;
077. &lt;value&gt;com.leo.po.Articlevalue&gt;
078. constructor-arg&gt;
079. &lt;property name="hibernateTemplate" ref="hibernateTemplate" /&gt;
080. &lt;property name="mailService" ref="mailService" /&gt;
081. &lt;property name="logService" ref="logService" /&gt;
082. &lt;property name="javaMailSenderUtil" ref="javaMailSenderUtil" /&gt;
083. bean&gt;
084. &lt;bean id="mailService"
085. class="com.leo.service.impl.MailServiceImpl"&gt;
086. &lt;constructor-arg&gt;
087. &lt;value&gt;com.leo.po.Mailvalue&gt;
088. constructor-arg&gt;
089. &lt;property name="hibernateTemplate" ref="hibernateTemplate" /&gt;
090. bean&gt;
091. &lt;bean id="logService" class="com.leo.service.impl.LogServiceImpl"&gt;
092. &lt;constructor-arg&gt;
093. &lt;value&gt;com.leo.po.Logvalue&gt;
094. constructor-arg&gt;
095. &lt;property name="hibernateTemplate" ref="hibernateTemplate" /&gt;
096. bean&gt;
097. &lt;bean id="mailSender"
098. class="org.springframework.mail.javamail.JavaMailSenderImpl"&gt;
099. &lt;property name="host" value="${mail.smtp}" /&gt;
100. &lt;property name="username" value="${mail.username}" /&gt;
101. &lt;property name="password" value="${mail.password}" /&gt;
102. &lt;property name="javaMailProperties"&gt;
103. &lt;props&gt;
104. &lt;prop key="mail.smtp.auth"&gt;trueprop&gt;
105. props&gt;
106. property&gt;
107. bean&gt;
108. &lt;bean id="mailMessage"
109. class="org.springframework.mail.SimpleMailMessage"&gt;
110. &lt;property name="to" value="superleo\_cn@hotmail.com" /&gt;
111. &lt;property name="from" value="${mail.username}" /&gt;
112. &lt;property name="subject" value="testdfsfsdf" /&gt;
113. bean&gt;
114. &lt;bean id="javaMailSenderUtil"
115. class="com.leo.util.JavaMailSenderUtil"&gt;
116. &lt;property name="mailSender" ref="mailSender" /&gt;
117. bean&gt;
118. beans&gt;

在classpath下的leo.conf是程序的基本参数设置，包括你的MySQL数据用户，密码，还有你电子邮件SMTP和账号密码的设置，我的配置是这样的，大家使用的时候，需要修改这个配置文件，以免出现无法正常使用和正常收发信。

java 代码

01. # JDBC配置
02. jdbc.driver=com.mysql.jdbc.Driver
03. jdbc.url=jdbc:mysql://localhost/ssh\_mail?useUnicode=true&characterEncoding=utf8&jdbcCompliantTruncation=false
04. jdbc.username=root
05. jdbc.password=123
06. jdbc.maxActive=25
07. jdbc.maxIdle=5
08. jdbc.maxWait=10000
09. # 电子邮件配置
10. mail.smtp=smtp.163.com
11. mail.username=kyo100900@163.com
12. mail.password=123456

配置好了后，如果不放心，在test文件夹下，有一个测试类，叫UserServiceTest.java。 大家可以修改里面的一个ID参数，然后运行就可知道你的Spring是否在Service层配置正常。