当前位置: 代码迷 >> Java Web开发 >> 【生手学习spring】
  详细解决方案

【生手学习spring】

热度:182   发布时间:2016-04-16 21:43:04.0
【新手学习spring】
帮帮忙看看呗
package aopannotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class test {
     public static void main(String args[]){
      ApplicationContext as = new FileSystemXmlApplicationContext("applicationContext.xml");
         IHello ih= (IHello) as.getBean("he");
         ih.sayHello("房客");
     }
}

测试类
package aopannotation;

public class Hello implements IHello{

public void sayHello(String str) {
      System.out.println("你好"+str);

}
   
}

package aopannotation;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


@Aspect
public class aspectBean {
    @Pointcut("execution(*hello.*(..))")
    public void log(){}
       @Before(value="log()")
    public void startLog(){
        System.out.println("开始记录!");
       }
       @After(value="log()")
    public void endLog(){
     System.out.println("结束记录!");
    }   

}

接口IHello
/**
 * 
 */
package aopannotation;

/**
 * @author Administrator
 *
 */
public interface IHello {
    public void sayHello(String str);
}

applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>

<!--
  - Application context definition for JPetStore's business layer.
  - Contains bean references to the transaction manager and to the DAOs in
  - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
  -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
   
   <aop:aspectj-autoproxy/>
   <bean id="apbean" class="aopannotation.aspectBean" />
   <bean id="he" class="aopnnotation.Hello" />
   
</beans>


错误信息
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreExceptionIOException parsing XML document from file [E:\protected\myeclipsestart\spring\applicationContext.xml]; nested exception is java.io.FileNotFoundException: applicationContext.xml (系统找不到指定的文件。)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:349)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:113)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:80)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:422)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
at aopannotation.test.main(test.java:8)
Caused by: java.io.FileNotFoundException: applicationContext.xml (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at org.springframework.core.io.FileSystemResource.getInputStream(FileSystemResource.java:108)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
... 13 more

------解决思路----------------------
你的applicationContent.xml问价放的位置
和你
ApplicationContext as = new FileSystemXmlApplicationContext("applicationContext.xml")
位置不匹配

E:\protected\myeclipsestart\spring\applicationContext.xml]
找不到
  相关解决方案