- 浏览: 104143 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
-
wxynxyo:
非常感谢,解决了一个问题
Spring MVC 类型转换 @InitBinder使用 转 -
hxsmile:
很清晰明了
MyEclipse下XFire开发Webservice实例 -
yaoyy:
...
MyEclipse下XFire开发Webservice实例 -
hyl523:
好,明白了,多谢!
MyEclipse下XFire开发Webservice实例
SSH下使用Spring注解自动注入bean
Spring注解的使用方法详见:http://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/,这里在SSH框架下做一个例子。
首先导入相关包:spring-beans-3.0.4.RELEASE.jar(org.springframework.beans.factory.annotation.Autowired用来注入bean)、spring-context-3.0.4.RELEASE.jar(org.springframework.stereotype.Componet 、Service、Repository等用来定义bean)。
其次需要添加相关配置:applicationContext.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <description>Spring公共配置</description>
- <!--配置数据源-->
- <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
- <propertyname="driverClassName"value="oracle.jdbc.driver.OracleDriver"/>
- <propertyname="url"value="jdbc:oracle:thin:@127.0.0.1:1521:cui"/>
- <propertyname="username"value="cui"/>
- <propertyname="password"value="cui"/>
- </bean>
- <!--配置sessionFactory-->
- <beanid="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <propertyname="dataSource"ref="dataSource"/>
- <propertyname="hibernateProperties">
- <props>
- <propkey="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
- <propkey="hibernate.show_sql">true</prop>
- </props>
- </property>
- <propertyname="packagesToScan">
- <list>
- <value>com.entity</value>
- </list>
- </property>
- </bean>
- <!--事务管理-->
- <beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <propertyname="sessionFactory"ref="sessionFactory"></property>
- </bean>
- <!--使用annotation自动注入bean,并启动相关处理注解的进程-->
- <context:component-scanbase-package="com">
- <context:include-filtertype="regex"expression="com/.dao.*"/>
- <!--正则表达式必须格式正确,否则无效。以下是无效的示例
- <context:exclude-filtertype="regex"expression="/.service/..*"/>
- <context:exclude-filtertype="regex"expression="com/.service*"/>
- <context:exclude-filtertype="regex"expression=".service*"/>
- -->
- <!--正确格式:从base-package开始
- <context:exclude-filtertype="regex"expression="com/.service.*"/>
- <context:exclude-filtertype="regex"expression="com/.service/..*"/>
- -->
- </context:component-scan>
- </beans>
web.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <web-appid="WebApp_ID"version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <display-name>mytest</display-name>
- <!--指定spring配置文件的位置-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:/applicationContext.xml</param-value>
- </context-param>
- <!--Struts2-->
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- <init-param>
- <param-name>actionPackages</param-name>
- <param-value>com.action</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!--自动加载applicationContext-->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- </web-app>
使用Hibernate JPA定义User类:
- packagecom.entity;
- importjavax.persistence.Column;
- importjavax.persistence.Entity;
- importjavax.persistence.GeneratedValue;
- importjavax.persistence.GenerationType;
- importjavax.persistence.Id;
- importjavax.persistence.Table;
- @Entity
- @Table(name="s_user")
- publicclassUser{
- privateLongid;
- privateStringusername;
- privateStringpassword;
- @Id
- @GeneratedValue(strategy=GenerationType.AUTO)
- publicLonggetId(){
- returnid;
- }
- publicvoidsetId(Longid){
- this.id=id;
- }
- @Column(name="name")
- publicStringgetUsername(){
- returnusername;
- }
- publicvoidsetUsername(Stringusername){
- this.username=username;
- }
- @Column(name="pwd")
- publicStringgetPassword(){
- returnpassword;
- }
- publicvoidsetPassword(Stringpassword){
- this.password=password;
- }
- }
Dao层:
- packagecom.dao;
- importcom.entity.User;
- publicinterfaceUserDao{
- publicvoidsave(Useruser);
- }
- packagecom.dao.Impl;
- importorg.apache.commons.logging.Log;
- importorg.apache.commons.logging.LogFactory;
- importorg.hibernate.SessionFactory;
- importorg.springframework.beans.factory.annotation.Autowired;
- importorg.springframework.orm.hibernate3.HibernateTemplate;
- importorg.springframework.stereotype.Repository;
- importcom.dao.UserDao;
- importcom.entity.User;
- @Repository("userDao")
- publicclassUserDaoImplimplementsUserDao{
- privateHibernateTemplatetemplate;
- privateLoglog=LogFactory.getLog(UserDaoImpl.class);
- //使用构造子注入自动注入sessionFactory
- @Autowired
- publicUserDaoImpl(SessionFactorysessionFactory){
- this.template=newHibernateTemplate(sessionFactory);
- }
- publicvoidsave(Useruser){
- template.save(user);
- log.debug("saveuser:"+user.getUsername());
- }
- }
Service层:
- packagecom.service;
- importcom.entity.User;
- publicinterfaceUserManager{
- publicvoidadd(Useruser);
- }
- packagecom.service.Impl;
- importorg.apache.commons.logging.Log;
- importorg.apache.commons.logging.LogFactory;
- importorg.springframework.beans.factory.annotation.Autowired;
- importorg.springframework.stereotype.Service;
- importcom.dao.UserDao;
- importcom.entity.User;
- importcom.service.UserManager;
- @Service("userManager")
- publicclassUserManagerImplimplementsUserManager{
- //自动注入userDao,也可以使用@Resource
- @Autowired
- privateUserDaouserDao;
- privateLoglog=LogFactory.getLog(UserManagerImpl.class);
- publicvoidadd(Useruser){
- userDao.save(user);
- log.debug("addUser:"+user.getUsername());
- }
- }
Action:
- packagecom.action.convention;
- importorg.apache.struts2.convention.annotation.Result;
- importorg.springframework.beans.factory.annotation.Autowired;
- importcom.entity.User;
- importcom.opensymphony.xwork2.ActionSupport;
- importcom.service.UserManager;
- @Result(name="success",location="hello.jsp")
- publicclassUserActionextendsActionSupport{
- privatestaticfinallongserialVersionUID=1L;
- @Autowired
- privateUserManageruserManager;
- publicStringexecute(){
- Useruser=newUser();
- user.setUsername("cuihaiyang");
- user.setPassword("abcd");
- userManager.add(user);
- returnSUCCESS;
- }
- }
调试信息如下:
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into s_user (pwd, name, id) values (?, ?, ?)
2011-03-10 19:44:25,296 [http-8080-1] DEBUG [com.dao.Impl.UserDaoImpl] - save user:cuihaiyang
2011-03-10 19:44:25,296 [http-8080-1] DEBUG [com.service.Impl.UserManagerImpl] - add User:cuihaiyang
Spring注解的使用方法详见:http://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/,这里在SSH框架下做一个例子。
首先导入相关包:spring-beans-3.0.4.RELEASE.jar(org.springframework.beans.factory.annotation.Autowired用来注入bean)、spring-context-3.0.4.RELEASE.jar(org.springframework.stereotype.Componet 、Service、Repository等用来定义bean)。
其次需要添加相关配置:applicationContext.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <description>Spring公共配置</description>
- <!--配置数据源-->
- <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
- <propertyname="driverClassName"value="oracle.jdbc.driver.OracleDriver"/>
- <propertyname="url"value="jdbc:oracle:thin:@127.0.0.1:1521:cui"/>
- <propertyname="username"value="cui"/>
- <propertyname="password"value="cui"/>
- </bean>
- <!--配置sessionFactory-->
- <beanid="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <propertyname="dataSource"ref="dataSource"/>
- <propertyname="hibernateProperties">
- <props>
- <propkey="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
- <propkey="hibernate.show_sql">true</prop>
- </props>
- </property>
- <propertyname="packagesToScan">
- <list>
- <value>com.entity</value>
- </list>
- </property>
- </bean>
- <!--事务管理-->
- <beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <propertyname="sessionFactory"ref="sessionFactory"></property>
- </bean>
- <!--使用annotation自动注入bean,并启动相关处理注解的进程-->
- <context:component-scanbase-package="com">
- <context:include-filtertype="regex"expression="com/.dao.*"/>
- <!--正则表达式必须格式正确,否则无效。以下是无效的示例
- <context:exclude-filtertype="regex"expression="/.service/..*"/>
- <context:exclude-filtertype="regex"expression="com/.service*"/>
- <context:exclude-filtertype="regex"expression=".service*"/>
- -->
- <!--正确格式:从base-package开始
- <context:exclude-filtertype="regex"expression="com/.service.*"/>
- <context:exclude-filtertype="regex"expression="com/.service/..*"/>
- -->
- </context:component-scan>
- </beans>
web.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <web-appid="WebApp_ID"version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <display-name>mytest</display-name>
- <!--指定spring配置文件的位置-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:/applicationContext.xml</param-value>
- </context-param>
- <!--Struts2-->
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- <init-param>
- <param-name>actionPackages</param-name>
- <param-value>com.action</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!--自动加载applicationContext-->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- </web-app>
使用Hibernate JPA定义User类:
- packagecom.entity;
- importjavax.persistence.Column;
- importjavax.persistence.Entity;
- importjavax.persistence.GeneratedValue;
- importjavax.persistence.GenerationType;
- importjavax.persistence.Id;
- importjavax.persistence.Table;
- @Entity
- @Table(name="s_user")
- publicclassUser{
- privateLongid;
- privateStringusername;
- privateStringpassword;
- @Id
- @GeneratedValue(strategy=GenerationType.AUTO)
- publicLonggetId(){
- returnid;
- }
- publicvoidsetId(Longid){
- this.id=id;
- }
- @Column(name="name")
- publicStringgetUsername(){
- returnusername;
- }
- publicvoidsetUsername(Stringusername){
- this.username=username;
- }
- @Column(name="pwd")
- publicStringgetPassword(){
- returnpassword;
- }
- publicvoidsetPassword(Stringpassword){
- this.password=password;
- }
- }
Dao层:
- packagecom.dao;
- importcom.entity.User;
- publicinterfaceUserDao{
- publicvoidsave(Useruser);
- }
- packagecom.dao.Impl;
- importorg.apache.commons.logging.Log;
- importorg.apache.commons.logging.LogFactory;
- importorg.hibernate.SessionFactory;
- importorg.springframework.beans.factory.annotation.Autowired;
- importorg.springframework.orm.hibernate3.HibernateTemplate;
- importorg.springframework.stereotype.Repository;
- importcom.dao.UserDao;
- importcom.entity.User;
- @Repository("userDao")
- publicclassUserDaoImplimplementsUserDao{
- privateHibernateTemplatetemplate;
- privateLoglog=LogFactory.getLog(UserDaoImpl.class);
- //使用构造子注入自动注入sessionFactory
- @Autowired
- publicUserDaoImpl(SessionFactorysessionFactory){
- this.template=newHibernateTemplate(sessionFactory);
- }
- publicvoidsave(Useruser){
- template.save(user);
- log.debug("saveuser:"+user.getUsername());
- }
- }
Service层:
- packagecom.service;
- importcom.entity.User;
- publicinterfaceUserManager{
- publicvoidadd(Useruser);
- }
- packagecom.service.Impl;
- importorg.apache.commons.logging.Log;
- importorg.apache.commons.logging.LogFactory;
- importorg.springframework.beans.factory.annotation.Autowired;
- importorg.springframework.stereotype.Service;
- importcom.dao.UserDao;
- importcom.entity.User;
- importcom.service.UserManager;
- @Service("userManager")
- publicclassUserManagerImplimplementsUserManager{
- //自动注入userDao,也可以使用@Resource
- @Autowired
- privateUserDaouserDao;
- privateLoglog=LogFactory.getLog(UserManagerImpl.class);
- publicvoidadd(Useruser){
- userDao.save(user);
- log.debug("addUser:"+user.getUsername());
- }
- }
Action:
- packagecom.action.convention;
- importorg.apache.struts2.convention.annotation.Result;
- importorg.springframework.beans.factory.annotation.Autowired;
- importcom.entity.User;
- importcom.opensymphony.xwork2.ActionSupport;
- importcom.service.UserManager;
- @Result(name="success",location="hello.jsp")
- publicclassUserActionextendsActionSupport{
- privatestaticfinallongserialVersionUID=1L;
- @Autowired
- privateUserManageruserManager;
- publicStringexecute(){
- Useruser=newUser();
- user.setUsername("cuihaiyang");
- user.setPassword("abcd");
- userManager.add(user);
- returnSUCCESS;
- }
- }
调试信息如下:
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into s_user (pwd, name, id) values (?, ?, ?)
2011-03-10 19:44:25,296 [http-8080-1] DEBUG [com.dao.Impl.UserDaoImpl] - save user:cuihaiyang
2011-03-10 19:44:25,296 [http-8080-1] DEBUG [com.service.Impl.UserManagerImpl] - add User:cuihaiyang
相关推荐
ssh2注解配置,全部是注解配置,struts2和hibernate3和spring2.5全部是注解配置,,访问路径为http://localhost:8080/mytest/student/findAll.action
1 首先整合spring和hibernate,这次我们在spring 中配置bean使用注解的方式 ,hibernate实体映射关系也使用注解的方式,配置完毕后用简单方法测试下hibernate是否整合成功。 a 加入支持:添加 spring核心包、...
一个小型maven管理的ssh框架开发的人员管理网站,能基本对与ssh框架有一个基本了解,内容也不多,框架条理都分好了,易于学习。...默认字符集为UTF-8,数据库直接按照bean创建就行,注解形式无法自动创建表
【第十二章】零配置 之 12.2 注解实现Bean依赖注入 ——跟我学spring3 【第十二章】零配置 之 12.3 注解实现Bean定义 ——跟我学spring3 【第十二章】零配置 之 12.4 基于Java类定义Bean配置元数据 ——跟我学spring...
一、 开始使用annotation配置Spring 16 二、 @Autowired、@Qualifier 16 (一) @Autowired 16 (二) @Qualifier 17 三、 @Resource(重要、推荐) 17 (一) JSR-250 17 (二) @Resource 17 四、 @Componet 18 五、 @Scope...
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <value>com.org.core.entity</value> ${hibernate....
SSH2(Struts2 + Spring + Hibernate),标准SSH2框架,采用了基于注解方式管理bean和事务,使用poi进行excel批量导出
测试spring事务管理 搭建了ssh框架的web工程 本工程用到的数据库表很简单 user(id, name) 可自行创建 本例所有的事务放在service层进行管理,方法中间抛出... 第5种方式:全注解 详见spring-core-transaction-5.xml
图书类型:图书类别,类别名称,可借阅天数 图书:图书条形码,图书名称,图书所在类别,...采用技术: SSH2(Struts2 Spring Hibernate),标准SSH2框架,采用了基于注解方式管理bean和事务,使用poi进行excel批量导出
一、 开始使用annotation配置Spring 16 二、 @Autowired、@Qualifier 16 (一) @Autowired 16 (二) @Qualifier 17 三、 @Resource(重要、推荐) 17 (一) JSR-250 17 (二) @Resource 17 四、 @Componet 18 五、 @Scope...
采用技术: SSH2(Struts2 + Spring + Hibernate),标准SSH2框架,采用了基于注解方式管理bean和事务,使用poi进行excel批量导出 图书类型:图书类别,类别名称,可借阅天数 图书:图书条形码,图书名称,图书所在...
涵盖使用Spring Boot 进行Java EE 开发的绝大数应用场景,包含:Web 开发、数据访问、安全控制、批处理、异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 第一部分 点睛Spring 4.x 第1 章 Spring ...
采用技术: SSH2(Struts2 + Spring + Hibernate),标准SSH2框架,采用了基于注解方式管理bean和事务,使用poi进行excel批量导出 图书类型:图书类别,类别名称,可借阅天数 图书:图书条形码,图书名称,图书所在...
图书类型:图书类别,类别名称,可借阅天数 图书:图书条形码,图书名称,图书所在类别...采用技术: SSH2(Struts2 + Spring + Hibernate),标准SSH2框架,采用了基于注解方式管理Bean和事务,使用Poi进行Excel批量导出
8.有全文检索工具类包含索引的创建、查询等,自动分页Bean,Excel读取/导出等功能、汉字转拼音,MD5加密,加密解密,图片转字符、字符转图片,IP检查,拦截器权限拦截,Struts2Utils类,Spring3Utils类,轻松搞定...
pdf文档包括如下知识: 1.spring介绍 2.spring搭建 3.spring中的概念 4.配置文件详解 5.Bean的创建方式 6.注入方式 7.复杂属性注入 8.在WEB环境中使用Spring容器 9.注解代替xml配置 10. spring AOP开发 等等。
SSH整合+struts convention插件小示例 全程使用注解扫包,无struts action的配置文件,...Spring自动扫包来获取bean 数据库内容太简单,不再上传。 只是一个小实例。 请用MyEclipse导入项目。 我的版本是MyEclipse 2013
一个spring2.5+hibernate3.3+struts1.3的整合的完整项目,该项目使用spring容器创建sessionFactory,管理struts ActionBean的创建,其中使用注解的方式创建实体Bean以及依赖注入和事务
涵盖使用Spring Boot 进行Java EE 开发的绝大数应用场景,包含:Web 开发、数据访问、安全控制、批处理、异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 第一部分 点睛Spring 4.x 第1 章 Spring ...
一个简单的ssh框架整合,内含实现以下描述所有的基础包,此框架实现hibernate,struts在spring中的整合,另外开启了spring的注解功能,在实例化spring容器时,可对指定包可以扫描Bean的存在,实现所谓的零配置。