spring component-scan模式在JBOSS 5下的解决方案

转载于:http://blog.sina.com.cn/s/blog_6151984a0100ko2y.html

 

  • 问题
1.环境
spring 2.5.6
struts 2.1.6
hibernate 3.3.2
JBOSS 4.2.2 GA
JBOSS 5.0.0 GA
2.spring 配置文件开启component-scan    
spring-hibernate-c3p0.xml:

<context:component-scan base-package="com.machome"/>
 
3.业务类加@Service标注,自动被扫描进spring bean pool
@Service("stuService")
@Transactional
public class StuService_hibernate implements Stu_hibernate_intf {
    @Resource                       
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
    public List<Stu_hibernate> findAll(){
        return sessionFactory.getCurrentSession().createQuery("from Stu_hibernate").list();
    }
4.Action类加@Controller标注,自动被扫描进spring bean pool,并采用@Resource注入前面扫描生成的业务bean
@Controller("stuList")      
@Scope("prototype")           
public class StuList extends ActionSupport {
    private static final long serialVersionUID = 1L;
   
    @Resource(name="stuService")
    private Stu_hibernate_intf stuService;       


    public void setStuService(Stu_hibernate_intf stuService) {
        this.stuService = stuService;
    }

    @Override
    public String execute() throws Exception {
        List<Stu_hibernate> sList = stuService.findAll();
        ActionContext.getContext().put("testList",sList);
        return SUCCESS;
     
}
5.struts2 配置
struts.xml
    <package name="example" namespace="/example" extends="com.machome.struts2.core"  >
       
        <action name="stuList" class="stuList"> <!-- class name为spring bean name -->               
            <result type="dispatcher">/jsp/stu_list.jsp</result>
    </action>
6.前端jsp文件
/jsp/stu_list.jsp :
  <body>
    <%
        List<Stu_hibernate> testList=(List<Stu_hibernate>)request.getAttribute("testList");
         for(Stu_hibernate temp:testList)
             out.println(temp.getStu_id()+":"+temp.getStu_name()+"<br />");
     %>   

  </body>
7. 项目web.xml配置    
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- ##################### 初始化spring容器  ###########-->   
    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:spring-hibernate-c3p0.xml</param-value>
    </context-param>
    <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
   
<!-- ###################### struts2 过滤器  ############ -->   
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
<!-- ################################################# -->

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
 
8.访问Action
http://localhost:8080/A_ssh2_basic/example/stuList
9.结果:
1.JBOSS 4.2.2 GA测试通过

2.JBOSS 5.0.0 GA,5.1.0 GA , 出错:
Unable to instantiate Action, stuList,  defined for 'stuList' in namespace '/example'stuList from BaseClassLoa
root cause    
java.lang.ClassNotFoundException: stuList from BaseClassLoader@114e72e{VFSClassLoaderPolicy@1557f9d{name=vfsfile:/E:/jboss-5.0.0.GA/server/default/deploy/A_ssh2_basic.war/



  • 原因:
JBOSS 5.0后采用 VFS系统,解析XML文件时对spring支持的不好.
(JBOSS 项目组认为是spring对它支持的不好,这种事鸡同鸭讲,永远没有共同语言,JBOSS等着spring 3.0后解决这个问题,spring等着JBOSS 5.1以后解决这个问题,结果两家都没完全解决,都是出了补丁了事)
具体情况见: http://jira.springframework.org/browse/SPR-5120

  • 解决方法:
1.下载jboss-spring-int-vfs.jar,加入build path
(在此处下载: http://jira.springframework.org/browse/SPR-5120)
  该jar包内包含两个类:
  VFSClassPathXmlApplicationContext
  VFSXmlWebApplicationContext
2.web.xml添加下列语句
<!-- ##################### 初始化spring容器  ###########-->   
    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:spring-hibernate-c3p0.xml</param-value>
    </context-param>
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.jboss.spring.vfs.context.VFSXmlWebApplicationContext
        </param-value>
    </context-param>
    <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
   
<!-- ###################### struts2 过滤器  ############ -->   
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
3.重新再JBOSS 5.0.0 GA下访问 http://localhost:8080/A_ssh2_basic/example/stuList,通过
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值