- 浏览: 107582 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
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
相关推荐
Spring注解如`@Component`、`@Service`、`@Repository`和`@Controller`用于标记bean,使得Spring容器能够自动检测和管理这些bean。此外,`@Autowired`注解用于自动装配bean的依赖,而`@Transactional`注解则可以声明...
SSH(Struts2、Spring、Hibernate)...总的来说,这个项目提供了一个使用SSH框架和Spring注解实现的登录功能实例,通过分析和学习,你可以更好地理解SSH框架的工作原理以及如何在实际项目中应用Spring注解来简化开发。
在实际开发中,SSH框架的使用往往结合IDE(如Eclipse或IntelliJ IDEA)进行,这些工具通常有对SSH支持的插件,能帮助开发者快速生成和管理注解配置。同时,持续集成工具(如Jenkins)和版本控制系统(如Git)也是...
- `服务类和服务接口`:使用Spring注解进行配置的服务层类和接口。 - `DAO类`:使用Hibernate和Spring注解进行数据访问操作的类。 - `测试类`:用于验证注解配置是否正确的测试代码。 - `web.xml`:Web应用的部署...
3. **配置DAO**:使用Spring的`@Repository`注解标记DAO接口,通常会配合`@Autowired`注入SessionFactory或EntityManager。 4. **服务层**:使用`@Service`注解标记服务类,这里会包含业务逻辑,同样可以使用`@...
综上所述,SSH高质量整合利用Spring的注解简化配置,通过Hibernate的注解实现ORM映射,同时利用Hibernate的自动建表功能,极大地提高了开发效率。项目中的标签注入则优化了视图层的开发,使得整个Web应用的开发流程...
在上面的例子中,`@Service`注解标记了UserService是一个业务层组件,而`@Autowired`注解告诉Spring容器自动寻找与userRepository类型匹配的bean进行注入。Spring会通过类型匹配、名称匹配或者使用@Qualifier注解...
在Spring中,我们可以通过注解来声明bean、依赖注入、事务管理等。比如,`@Component` 定义了一个bean,`@Autowired` 自动装配bean的依赖,`@Transactional` 实现了事务管理。 **Hibernate** 是一个强大的ORM...
例如,可以通过`@Resource`或`@Autowired`注解在类成员变量上,让Spring自动寻找匹配类型的bean进行注入。另外,`@Component`、`@Service`、`@Repository`和`@Controller`等注解用于定义不同类型的bean,`@Scope`...
总结起来,这个场景展示了如何通过Spring注解配置创建和管理中间层服务,以及如何在不依赖Struts2默认配置的情况下,使Struts2能够使用这些服务。通过这种方式,我们可以充分利用Spring的注解驱动和依赖注入能力,...
`@Autowired`注解可以自动将Bean注入到需要的字段或方法中。例如: ```java @Service public class UserService { private UserRepository userRepository; @Autowired public UserService(UserRepository ...
在SSH2中,Spring主要负责控制层和业务层的管理,通过注解配置可以避免大量的XML配置文件,例如`@Autowired`用于自动装配bean,`@Service`、`@Repository`和`@Controller`用于标记服务、数据访问对象和控制器类。...
学习这个实例,你可以了解到如何在实际项目中利用Spring注解来提高代码可读性、减少配置,并了解SSH2架构下Spring如何与其他组件协同工作。同时,通过阅读文档,你可以深入理解每个注解的用法和背后的设计理念,...
"ssh"文件夹可能包含了示例项目的源代码,包括Struts2的Action类、Hibernate的实体类、Spring的配置文件等,这些都是深入理解SSH框架和注解使用的宝贵学习资料。 总之,SSH框架结合了Struts2的请求处理、Hibernate...
SSH2整合全注解实例是基于Struts2、Spring和Hibernate这三个开源框架的集成应用,它们共同构建了一个强大的Web应用程序开发平台。SSH2是Java Web开发中的经典组合,提供了模型-视图-控制器(MVC)的设计模式,使得...
2. **Spring注解**: - `@Component`:标记一个类为Spring管理的Bean,如Service、DAO等。 - `@Autowired`:自动装配Bean,根据类型或名称注入依赖。 - `@Qualifier`:配合@Autowired,当有多个相同类型的Bean时...
在本项目中,Spring通过注解实现组件扫描,自动装配Bean,如`@Autowired`用于自动注入依赖,`@Service`、`@Repository`和`@Controller`定义不同层的组件角色。此外,Spring的AOP机制用于事务管理,如`@Transactional...
- `@Autowired`:这个注解用于自动装配Bean,Spring会根据类型或名称将依赖注入到目标Bean中。 - `@Component`,`@Service`,`@Repository`:这些是组件扫描的注解,它们标识了一个类作为Spring的Bean,分别对应于...
2. **Spring**:Spring框架是注解驱动的王者,它提供了大量的注解来实现依赖注入(DI)、AOP、事务管理等功能。比如,@Autowired自动装配bean,@Service、@Repository、@Controller用于组件扫描,@Transactional进行...
SSH框架,全称为Spring、Struts和Hibernate的组合,是Java Web开发中广泛使用的三大开源框架。它们分别负责应用的依赖注入(Spring)、MVC(Model-View-Controller)架构管理(Struts)以及持久层操作(Hibernate)...