Research Idea Java Application not found in xml configuration file problem

Research Idea Java Application not found in xml configuration file problem
 
problem:
When building a Java Application project with Idea, a Spring application framework, but Spring xml configuration file can not be found. Examination showed that the problem is not the code. I spent a good long time before we can solve.
 
Problems arise, I did a summary of the study load various resource files (.xml, .properties, etc.) Idea in. To illustrate the problem, following the establishment of a Spring test project, the goal is to run a print "Hello World!" In the console, focusing on resource allocation method to see the Idea. And a different configuration will not effect caused.
 
surroundings:
IntelliJ IDEA 5.1.2 (I think it is best to use version 7.0 now has just come out)
J2SDK 1.5
Spring framework 1.2.8
WIN2003
 
 
Build a test project:
 
Dependent packages
commons-logging.jar
spring-beans.jar
spring-context.jar
spring-core.jar
 
Source:
 
1, Bean class HelloBean:
public class HelloBean {
    private String helloWord;
   
    public void setHelloWord(String helloWord) {
        this.helloWord = helloWord;
    }
    public String getHelloWord() {
        return helloWord;
    }
}
 
2, 主 类 Spring Demo:
public class SpringDemo {
//    public static void main(String[] args) {
//        Resource rs =
//                new FileSystemResource("beans-config.xml");
//        BeanFactory factory =
//                new XmlBeanFactory(rs);
//
//        HelloBean hello =
//                (HelloBean) factory.getBean("helloBean");
//        System.out.println(hello.getHelloWord());
//    }
    public static void main(String args[]){
//        ApplicationContext context = new FileSystemXmlApplicationContext("beans-config.xml");
        ApplicationContext context = new ClassPathXmlApplicationContext("beans-config.xml");
        HelloBean hello = (HelloBean)context.getBean("helloBean");
        System.out.println(hello.getHelloWord());
    }
}
 
3, Spring configuration file beans-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
  "
[url]http://www.springframework.org/dtd/spring-beans.dtd[/url] ">
<beans>
    <bean id="helloBean"
          class="lavasoft.springtest.HelloBean">
        <property name="helloWord">
            <value>Hello World!</value>
        </property>
    </bean>
</beans>
 
 
Idea configuration:
Here is the directory structure Idea project:
 
 
Jar configuration and resource files:
 
 
Directory type setting chart:
 
 
After the configuration according to FIG., The results are as follows:
 
Research result:
There are three types of directories, it has been given in the figure caption.
Here mainly to see the res directory:
1, this directory usually named res, to represent resources.
2, if the directory is set to "Sources" type, after the project has been compiled, the files in the directory will resorce intact copy of the file to the directory generated compiled in this directory for the classes, and the Idea of the project panel We can see there is res directory.
3, this directory is set to a normal folder type (shallow ××× paper bag), then after the project is built, the files in the directory are not copied to resorce classes directory, and seen in Idea engineering panel not see this directory .
4, res files in the directory to find the program you want to be, you need to configure res directory to the Idea in your classpath. Referring to FIG second.
5, this configuration method is suitable for other types of configuration files such as .properties file, the principle is to load these files into calsspath, so that you can debug in Idea in the run.

Guess you like

Origin www.cnblogs.com/liudianjia/p/12525855.html