本帖最后由 student_2008 于 2013-11-29 13:02:20 编辑

解决方案 »

  1.   

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    public class Main {
    private static ApplicationContext context = null;
    static final String[] path={"classpath:applicationContext.xml"};
        static final String[] classpath={"applicationContext.xml"};
        
        private static void init(){
            try {
                context = new FileSystemXmlApplicationContext(path);
            } catch (Exception e) {
                try {
                    context = new FileSystemXmlApplicationContext(classpath);
                } catch (Exception e2) {
                    try {
                        context = new ClassPathXmlApplicationContext(classpath);
                    } catch (Exception e3) {
                        e3.printStackTrace();
                    }
                }
            }
        }
        
    public static void main(String[] args) {
    init();
    System.out.println(context==null);
    ((Serv)context.getBean(Serv.class)).t();
    }
    }
    import org.springframework.stereotype.Component;@Component
    public class Serv {
    public void t(){
    System.out.println("sssssss");
    }
    }
    application.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xsi:schemaLocation="  
      http://www.springframework.org/schema/aop    
       http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
            http://www.springframework.org/schema/beans   
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/context   
            http://www.springframework.org/schema/context/spring-context-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/jdbc   
            http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  
            http://www.springframework.org/schema/data/jpa   
            http://www.springframework.org/schema/data/jpa/spring-jpa.xsd  
            http://www.springframework.org/schema/jee   
            http://www.springframework.org/schema/jee/spring-jee-3.0.xsd  
            ">
    <context:annotation-config />
    <aop:aspectj-autoproxy/>
    <context:component-scan base-package="a" >
    </context:component-scan>


    <!-- <bean id="serv" class="a.Serv"></bean> -->


    </beans>
      

  2.   

    我试过了。没有问题啊。可以运行<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
        xmlns:aop="http://www.springframework.org/schema/aop"  
        xsi:schemaLocation="  
             http://www.springframework.org/schema/aop    
              http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
            http://www.springframework.org/schema/beans   
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/context   
            http://www.springframework.org/schema/context/spring-context-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/jdbc   
            http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  
            http://www.springframework.org/schema/data/jpa   
            http://www.springframework.org/schema/data/jpa/spring-jpa.xsd  
            http://www.springframework.org/schema/jee   
            http://www.springframework.org/schema/jee/spring-jee-3.0.xsd  
            ">
     
     
        <context:annotation-config />
        <aop:aspectj-autoproxy/>
        <context:component-scan base-package="com.wanmei.spring.test" >
        </context:component-scan>
         
         
        <!-- <bean id="serv" class="a.Serv"></bean> -->
         
         
    </beans>
    package com.wanmei.spring.test;import org.springframework.stereotype.Component;@Component
    public class Serv {
        public void t(){
            System.out.println("sssssss");
        }
    }
    package com.wanmei.spring.test;import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;public class Bootstrap { /**
     * @param args
     */
    public static void main(String[] args) {

    String configLocation = "E:/workspace_java/test/src/com/wanmei/spring/test/applicationContext.xml";
    ApplicationContext context = new FileSystemXmlApplicationContext(configLocation);
    context.getBean(Serv.class).t();
    }}
      

  3.   

    我是用eclipse导出的,export->runnable->package required
    你是这种么
      

  4.   

    嗯。我照着你说的样子做了测试。确实,存在你说的问题。具体问题我还没有找到。
    不过,正如你说的:在eclipse中运行是正常的,eclipse导出之后就有问题了。应该是在eclipse运行时存在某个“eclipse的环境因素”,程序可以正常运行。经过eclipse导出之后,这个“环境因素”丢失了,就无法运行了。或者是eclipse的导出存在问题,也会导致这样的问题。
    我也没有找到问题。如果你很着急的话,还是用spring的xml里面进行bean的配置吧,不采用“注释”的方式。
    如果你的问题解决了,请在这里跟帖。我也很想知道为什么。谢谢!