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

 

view plain copy to clipboard print ?
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  6.   
  7.     <description>Spring公共配置 description>  
  8.   
  9.       
  10.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  11.         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>  
  12.         <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:cui"/>  
  13.         <property name="username" value="cui"/>  
  14.         <property name="password" value="cui"/>  
  15.     bean>  
  16.       
  17.       
  18.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  19.         <property name="dataSource" ref="dataSource"/>  
  20.         <property name="hibernateProperties">  
  21.             <props>  
  22.                 <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialectprop>  
  23.                 <prop key="hibernate.show_sql">trueprop>  
  24.             props>  
  25.         property>  
  26.         <property name="packagesToScan">  
  27.             <list>  
  28.                 <value>com.entityvalue>  
  29.             list>  
  30.         property>  
  31.     bean>  
  32.       
  33.       
  34.     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  35.         <property name="sessionFactory" ref="sessionFactory">property>  
  36.     bean>  
  37.       
  38.       
  39.     <context:component-scan base-package="com">  
  40.         <context:include-filter type="regex" expression="com/.dao.*"/>  
  41.          org.hibernate.dialect.Oracle10gDialect true com.entity

     

    web.xml

     

    view plain copy to clipboard print ?
    1. "1.0" encoding="UTF-8"?>  
    2. "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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
    3.     mytest  
    4.       
    5.       
    6.         contextConfigLocation  
    7.         classpath*:/applicationContext.xml  
    8.       
    9.       
    10.       
    11.       
    12.         struts2  
    13.         class>org.apache.struts2.dispatcher.FilterDispatcherclass>  
    14.           
    15.             actionPackages  
    16.             com.action  
    17.           
    18.       
    19.   
    20.       
    21.         struts2  
    22.         /*  
    23.       
    24.       
    25.       
    26.       
    27.         class>org.springframework.web.context.ContextLoaderListenerclass>  
    28.       
    29.   

    mytest contextConfigLocation classpath*:/applicationContext.xml struts2 org.apache.struts2.dispatcher.FilterDispatcher actionPackages com.action struts2 /* org.springframework.web.context.ContextLoaderListener

     

    使用Hibernate JPA定义User类:

     

    view plain copy to clipboard print ?
    1. package com.entity;  
    2.   
    3. import javax.persistence.Column;  
    4. import javax.persistence.Entity;  
    5. import javax.persistence.GeneratedValue;  
    6. import javax.persistence.GenerationType;  
    7. import javax.persistence.Id;  
    8. import javax.persistence.Table;  
    9.   
    10. @Entity  
    11. @Table(name = "s_user")  
    12. public class User {  
    13.   
    14.     private Long id;  
    15.     private String username;  
    16.     private String password;  
    17.   
    18.     @Id  
    19.     @GeneratedValue(strategy = GenerationType.AUTO)  
    20.     public Long getId() {  
    21.         return id;  
    22.     }  
    23.   
    24.     public void setId(Long id) {  
    25.         this.id = id;  
    26.     }  
    27.   
    28.     @Column(name = "name")  
    29.     public String getUsername() {  
    30.         return username;  
    31.     }  
    32.   
    33.     public void setUsername(String username) {  
    34.         this.username = username;  
    35.     }  
    36.   
    37.     @Column(name = "pwd")  
    38.     public String getPassword() {  
    39.         return password;  
    40.     }  
    41.   
    42.     public void setPassword(String password) {  
    43.         this.password = password;  
    44.     }  
    45.   
    46. }  

    package com.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "s_user") public class User { private Long id; private String username; private String password; @Id @GeneratedValue(strategy = GenerationType.AUTO) public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name = "name") public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Column(name = "pwd") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

     

    Dao层:

     

    view plain copy to clipboard print ?
    1. package com.dao;  
    2.   
    3. import com.entity.User;  
    4.   
    5. public interface UserDao {  
    6.     public void save(User user);  
    7. }  
    8.   
    9.   
    10. package com.dao.Impl;  
    11.   
    12. import org.apache.commons.logging.Log;  
    13. import org.apache.commons.logging.LogFactory;  
    14. import org.hibernate.SessionFactory;  
    15. import org.springframework.beans.factory.annotation.Autowired;  
    16. import org.springframework.orm.hibernate3.HibernateTemplate;  
    17. import org.springframework.stereotype.Repository;  
    18.   
    19. import com.dao.UserDao;  
    20. import com.entity.User;  
    21.   
    22. @Repository("userDao")  
    23. public class UserDaoImpl implements UserDao {  
    24.   
    25.     private HibernateTemplate template;  
    26.     private Log log = LogFactory.getLog(UserDaoImpl.class);  
    27.   
    28.     //使用构造子注入自动注入sessionFactory   
    29.     @Autowired  
    30.     public UserDaoImpl(SessionFactory sessionFactory) {  
    31.         this.template = new HibernateTemplate(sessionFactory);  
    32.     }  
    33.   
    34.     public void save(User user) {  
    35.         template.save(user);  
    36.         log.debug("save user:" + user.getUsername());  
    37.     }  
    38. }  

    package com.dao; import com.entity.User; public interface UserDao { public void save(User user); } package com.dao.Impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Repository; import com.dao.UserDao; import com.entity.User; @Repository("userDao") public class UserDaoImpl implements UserDao { private HibernateTemplate template; private Log log = LogFactory.getLog(UserDaoImpl.class); //使用构造子注入自动注入sessionFactory @Autowired public UserDaoImpl(SessionFactory sessionFactory) { this.template = new HibernateTemplate(sessionFactory); } public void save(User user) { template.save(user); log.debug("save user:" + user.getUsername()); } }

     

    Service层:

     

    view plain copy to clipboard print ?
    1. package com.service;  
    2. import com.entity.User;  
    3.   
    4. public interface UserManager {  
    5.     public void add(User user);  
    6. }  
    7.   
    8.   
    9. package com.service.Impl;  
    10.   
    11. import org.apache.commons.logging.Log;  
    12. import org.apache.commons.logging.LogFactory;  
    13. import org.springframework.beans.factory.annotation.Autowired;  
    14. import org.springframework.stereotype.Service;  
    15.   
    16. import com.dao.UserDao;  
    17. import com.entity.User;  
    18. import com.service.UserManager;  
    19.   
    20. @Service("userManager")  
    21. public class UserManagerImpl implements UserManager {  
    22.   
    23.     //自动注入userDao,也可以使用@Resource   
    24.     @Autowired  
    25.     private UserDao userDao;  
    26.     private Log log = LogFactory.getLog(UserManagerImpl.class);  
    27.   
    28.     public void add(User user) {  
    29.         userDao.save(user);  
    30.         log.debug("add User:" + user.getUsername());  
    31.     }  
    32. }  

    package com.service; import com.entity.User; public interface UserManager { public void add(User user); } package com.service.Impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.dao.UserDao; import com.entity.User; import com.service.UserManager; @Service("userManager") public class UserManagerImpl implements UserManager { //自动注入userDao,也可以使用@Resource @Autowired private UserDao userDao; private Log log = LogFactory.getLog(UserManagerImpl.class); public void add(User user) { userDao.save(user); log.debug("add User:" + user.getUsername()); } }

     

    Action:

     

    view plain copy to clipboard print ?
    1. package com.action.convention;  
    2.   
    3. import org.apache.struts2.convention.annotation.Result;  
    4. import org.springframework.beans.factory.annotation.Autowired;  
    5.   
    6. import com.entity.User;  
    7. import com.opensymphony.xwork2.ActionSupport;  
    8. import com.service.UserManager;  
    9.   
    10. @Result(name = "success", location = "hello.jsp")  
    11. public class UserAction extends ActionSupport {  
    12.   
    13.     private static final long serialVersionUID = 1L;  
    14.   
    15.     @Autowired  
    16.     private UserManager userManager;  
    17.   
    18.     public String execute() {  
    19.         User user = new User();  
    20.         user.setUsername("cuihaiyang");  
    21.         user.setPassword("abcd");  
    22.   
    23.         userManager.add(user);  
    24.   
    25.         return SUCCESS;  
    26.     }  
    27. }  

    package com.action.convention; import org.apache.struts2.convention.annotation.Result; import org.springframework.beans.factory.annotation.Autowired; import com.entity.User; import com.opensymphony.xwork2.ActionSupport; import com.service.UserManager; @Result(name = "success", location = "hello.jsp") public class UserAction extends ActionSupport { private static final long serialVersionUID = 1L; @Autowired private UserManager userManager; public String execute() { User user = new User(); user.setUsername("cuihaiyang"); user.setPassword("abcd"); userManager.add(user); return SUCCESS; } }

     

    调试信息如下:

    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

     

    转自http://blog.csdn.net/cuihaiyang/article/details/6238257#

你可能感兴趣的:(学习记录)