免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1180 | 回复: 0
打印 上一主题 下一主题

Struts2+Spring+Hibernate In Action(二) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-23 14:12 |只看该作者 |倒序浏览

当所有的Service配置好了,可以当他们统统配置在Spring这个IoC容器中,供Struts2使用。
以下是本程序中Spring的配置代码中的一部分:
xml 代码
xml version="1.0" encoding="UTF-8"?>   
              "http://www.springframework.org/dtd/spring-beans-2.0.dtd">   
beans>   
   
   
    bean id="propertyConfigurer"   
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   
        property name="locations">   
            list>   
                value>classpath:leo.confvalue>   
            list>   
        property>   
        property name="fileEncoding" value="utf-8" />   
        property name="ignoreResourceNotFound" value="true" />   
    bean>   
   
   
    bean id="dataSource"   
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">   
        property name="driverClassName" value="${jdbc.driver}" />   
        property name="url" value="${jdbc.url}" />   
        property name="username" value="${jdbc.username}" />   
        property name="password" value="${jdbc.password}" />   
    bean>   
   
    bean id="sessionFactory"   
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">   
        property name="configurationClass">   
            value>org.hibernate.cfg.AnnotationConfigurationvalue>   
        property>   
   
        property name="hibernateProperties">   
            value>   
                hibernate.dialect=org.hibernate.dialect.MySQLDialect   
                hibernate.show_sql=true hibernate.format_sql=true   
            value>   
        property>   
   
        property name="dataSource" ref="dataSource" />   
   
        property name="annotatedClasses">   
            list>   
                value>com.leo.po.Uservalue>   
                value>com.leo.po.Articlevalue>   
                value>com.leo.po.Mailvalue>   
                value>com.leo.po.Logvalue>   
            list>   
        property>   
    bean>   
   
   
    bean id="hibernateTemplate"   
        class="org.springframework.orm.hibernate3.HibernateTemplate">   
        property name="sessionFactory">   
            ref local="sessionFactory" />   
        property>   
    bean>   
   
   
   
    bean id="transactionManager"   
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
        property name="sessionFactory" ref="sessionFactory" />   
    bean>   
   
   
    bean   
        class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />   
   
    bean   
        class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">   
        property name="transactionInterceptor"   
            ref="transactionInterceptor" />   
    bean>   
   
    bean id="transactionInterceptor"   
        class="org.springframework.transaction.interceptor.TransactionInterceptor">   
        property name="transactionManager" ref="transactionManager" />   
   
        property name="transactionAttributeSource">   
            bean   
                class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" />   
        property>   
    bean>   
   
   
    bean id="userService"   
        class="com.leo.service.impl.UserServiceImpl">   
        constructor-arg>   
            value>com.leo.po.Uservalue>   
        constructor-arg>   
        property name="hibernateTemplate" ref="hibernateTemplate" />   
    bean>   
   
    bean id="articleService"   
        class="com.leo.service.impl.ArticleServiceImpl">   
        constructor-arg>   
            value>com.leo.po.Articlevalue>   
        constructor-arg>   
        property name="hibernateTemplate" ref="hibernateTemplate" />   
        property name="mailService" ref="mailService" />   
        property name="logService" ref="logService" />   
        property name="javaMailSenderUtil" ref="javaMailSenderUtil" />   
    bean>   
   
    bean id="mailService"   
        class="com.leo.service.impl.MailServiceImpl">   
        constructor-arg>   
            value>com.leo.po.Mailvalue>   
        constructor-arg>   
        property name="hibernateTemplate" ref="hibernateTemplate" />   
    bean>   
   
    bean id="logService" class="com.leo.service.impl.LogServiceImpl">   
        constructor-arg>   
            value>com.leo.po.Logvalue>   
        constructor-arg>   
        property name="hibernateTemplate" ref="hibernateTemplate" />   
    bean>   
   
   
   
   
        
    bean id="mailSender"   
        class="org.springframework.mail.javamail.JavaMailSenderImpl">   
        property name="host" value="${mail.smtp}" />   
        property name="username" value="${mail.username}" />   
        property name="password" value="${mail.password}" />   
        property name="javaMailProperties">   
            props>   
                prop key="mail.smtp.auth">trueprop>   
            props>   
        property>   
    bean>   
   
    bean id="mailMessage"   
        class="org.springframework.mail.SimpleMailMessage">   
        property name="to" value="superleo_cn@hotmail.com" />   
        property name="from" value="${mail.username}" />   
        property name="subject" value="testdfsfsdf" />   
    bean>   
   
   
    bean id="javaMailSenderUtil"   
        class="com.leo.util.JavaMailSenderUtil">   
        property name="mailSender" ref="mailSender" />   
    bean>   
   
beans>   
在classpath下的leo.conf是程序的基本参数设置,包括你的MySQL数据用户,密码,还有你电子邮件SMTP和账号密码的设置,我的配置是这样的,大家使用的时候,需要修改这个配置文件,以免出现无法正常使用和正常收发信。
java 代码
# JDBC配置   
jdbc.driver=com.mysql.jdbc.Driver   
jdbc.url=jdbc:mysql://localhost/ssh_mail?useUnicode=true&characterEncoding=utf8&jdbcCompliantTruncation=false   
jdbc.username=root   
jdbc.password=123   
   
jdbc.maxActive=25   
jdbc.maxIdle=5   
jdbc.maxWait=10000   
   
   
# 电子邮件配置   
mail.smtp=smtp.163.com   
mail.username=kyo100900@163.com   
mail.password=123456
配置好了后,如果不放心,在test文件夹下,有一个测试类,叫UserServiceTest.java。 大家可以修改里面的一个ID参数,然后运行就可知道你的Spring是否在Service层配置正常。


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/55983/showart_1333319.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP