简体   繁体   English

找不到Spring XML上下文文件

[英]Spring xml context file not found

I'm trying to learn Spring Framework following this tutorial: 我正在尝试按照本教程学习Spring Framework:

http://www.tutorialspoint.com/spring/spring_hello_world_example.htm

I have create a new Maven project with: 我用以下方法创建了一个新的Maven项目:

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>3.2.5.RELEASE</version>
 </dependency>

Is not a Web application. 不是Web应用程序。 I want to create a simple java application and my main method is: 我想创建一个简单的Java应用程序,我的主要方法是:

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();

If i'm not wrong ClassPathXmlApplicationContext constructor looks for the file on the src/main/resources folder. 如果我没有记错的话,ClassPathXmlApplicationContext构造函数将在src / main / resources文件夹中查找文件。 But when i execute the application i got the exception "File not found beans.xml". 但是,当我执行该应用程序时,出现了异常“找不到文件bean.xml”。 I tried with the full route to the beans.xml file src/main/resources/beans.xml and tons of routes. 我尝试使用到bean.xml文件src / main / resources / beans.xml的完整路由以及大量路由。

When i use this code to check the existence of my file it says that exists. 当我使用此代码检查文件是否存在时,它说存在。

File f = new File("src/main/resources/beans.xml");
System.out.println("Exist test: " + f.exists());

I don't get it. 我不明白 Any help please? 有什么帮助吗?

Another thing that i wanted to ask is if i need to use xml to get Dependency Injection. 我想问的另一件事是我是否需要使用xml来获取依赖注入。 Can i work with spring framework without using XML definitions? 我可以在不使用XML定义的情况下使用spring框架吗?

I have seen Annotations like @Autowired and more but i can't get it working also. 我已经看到了诸如@Autowired之类的Annotations,但我也无法使其正常运行。 I wonder if i need to use a combination of xml+annotations. 我想知道是否需要使用xml +注释的组合。

If you place the file in the root directory of your classpath (and that is what you do) use 如果将文件放在类路径的根目录中(这就是您要做的),请使用

ApplicationContext context = new ClassPathXmlApplicationContext("/beans.xml");

to reference it ("/" at the beginning). 引用它(开头是“ /”)。

You don't need XML configuration. 您不需要XML配置。 Have a look at the Spring Framwork Reference Documentation . 看看Spring Framwork参考文档 Chapter 5.9 Annotation-based container configuration and 5.12 Java-based container configuration explain Java Configuration in Detail. 5.9基于注释的容器配置5.12基于Java的容器配置详细介绍了Java配置。

ClassPathXmlApplicationContext doesn't look for the file in the /src/main/resources. ClassPathXmlApplicationContext不在/ src / main / resources中查找文件。 It tries to find it in the classpath (the "root" of your jar file). 它试图在类路径(jar文件的“根”)中找到它。

But everything you put in the src/main/resources will be put in the "root" (in the classpath so) by maven when you build your application. 但是,在构建应用程序时,您放置在src / main / resources中的所有内容都会被maven放置在“ root”(在类路径中)中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM