Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux集群 > Architecture > » 正文

SSH下使用Spring注解自动注入bean

来源: djkin 分享至:

这里在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 plaincopy to clipboardprint?

  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. <!-- 配置sessionFactory -->
  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.Oracle10gDialect</prop>
  23. <prop key="hibernate.show_sql">true</prop>
  24. </props>
  25. </property>
  26. <property name="packagesToScan">
  27. <list>
  28. <value>com.entity</value>
  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. <!-- 使用annotation自动注入bean,并启动相关处理注解的进程 -->
  39. <context:component-scan base-package="com">
  40. <context:include-filter type="regex" expression="com/.dao.*"/>
  41. <!-- 正则表达式必须格式正确,否则无效。以下是无效的示例
  42. <context:exclude-filter type="regex" expression="/.service/..*"/>
  43. <context:exclude-filter type="regex" expression="com/.service*"/>
  44. <context:exclude-filter type="regex" expression=".service*"/>
  45. -->
  46. <!-- 正确格式:从base-package开始
  47. <context:exclude-filter type="regex" expression="com/.service.*"/>
  48. <context:exclude-filter type="regex" expression="com/.service/..*"/>
  49. -->
  50. </context:component-scan>
  51.  
  52. </beans>

 

web.xml

 

view plaincopy to clipboardprint?

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app id="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. <display-name>mytest</display-name>
  4. <!-- 指定spring配置文件的位置 -->
  5. <context-param>
  6. <param-name>contextConfigLocation</param-name>
  7. <param-value>classpath*:/applicationContext.xml</param-value>
  8. </context-param>
  9.  
  10. <!-- Struts 2 -->
  11. <filter>
  12. <filter-name>struts2</filter-name>
  13. <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  14. <init-param>
  15. <param-name>actionPackages</param-name>
  16. <param-value>com.action</param-value>
  17. </init-param>
  18. </filter>
  19.  
  20. <filter-mapping>
  21. <filter-name>struts2</filter-name>
  22. <url-pattern>/*</url-pattern>
  23. </filter-mapping>
  24.  
  25. <!-- 自动加载applicationContext -->
  26. <listener>
  27. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  28. </listener>
  29. </web-app>

 

使用Hibernate JPA定义User类:

 

view plaincopy to clipboardprint?

  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. }

 

Dao层:

 

view plaincopy to clipboardprint?

  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. }

 

Service层:

 

view plaincopy to clipboardprint?

  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. }

 

Action:

 

view plaincopy to clipboardprint?

  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. }

 

调试信息如下:

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


Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史