Configure maven and spring

Source: Internet
Author: User
Tags maven central

Configure maven and spring

This blog is mainly about maven and spring configuration management. maven is used to manage spring dependency packages.

This article is original, reproduced please indicate to: http://blog.csdn.net/j903829182/article/details/38150795

For configuration of maven and struts2, see: http://blog.csdn.net/j903829182/article/details/38128777

For configuration of maven and hibernate, refer to: http://blog.csdn.net/j903829182/article/details/38147659

1. Create a web project using maven under the doscommand in cmd. For details, refer to the previous two blogs.

2. Import the created project into myeclipse, the specific import method, here no longer long-winded, not clear can see the http://blog.csdn.net/j903829182/article/details/38128777

3. Configure the jdk version of the project, add the directory, modify the properties of the project, For details, please refer to the http://blog.csdn.net/j903829182/article/details/38128777

4. After the above configuration is complete, the preliminary work of using maven to manage web projects will be completed. Next, configure spring package management.

5. Configure the spring dependency package:

Open the pom. xml file and configure the spring package.

   
  
   
4.0.0
    
  
   
Com. wj. demo. maven
  Maven4  
  
   
War
    
  
   
1.0-SNAPSHOT
    
  
   
Maven4 Maven Webapp
    
  
   
Http://maven.apache.org
    
      
       
   
    
UTF-8
       
      
      
         
    
     
Junit
    Junit      
    
     
4.10
          
    
     
Test
        
               
         
         
    
     
Org. springframework
    Spring-core      
          
    
     
4.0.0.M2
        
           
         
    
     
Org. springframework
    Spring-beans      
    
     
4.0.0.M2
        
           
         
    
     
Org. springframework
    Spring-context      
    
     
4.0.0.M2
        
           
         
    
     
Org. springframework
    Spring-jdbc      
    
     
4.0.0.M2
        
               
         
    
     
Org. springframework
    Spring-expression      
    
     
4.0.0.M2
        
           
         
    
     
Org. springframework
    Spring-web      
    
     
4.0.0.M2
        
           
         
    
     
Org. springframework
    Spring-webmvc      
    
     
4.0.0.M2
        
           
         
    
     
Org. springframework
    Spring-tx      
    
     
4.0.0.M2
        
               
         
    
     
Org. apache
    Commons-logging      
    
     
1.1.3
        
             
    
      
   
    
Maven4
     
  
 


Note: the package I configured here is relatively new. The maven central repository may not be updated yet. If you write the package as per me, it will lead to errors, so here I configure my local package. You can also configure your local package as follows:

Whether it's a new package or an old package, **** The content remains unchanged. You do not need to change the name of the organization according to the old configuration;

****Do not change the content here. According to the old package configuration package name, where should I change it ??? What needs to be changed is
  
   
***
  Version number. You change it to the version of your local package. After you have configured all the packages, save the package and mavne will automatically download the package you need, if the package is successfully downloaded, no error is reported. Otherwise, an error message is displayed. This indicates that the package download has failed. What should I do?
First, check the error message and find the error package in the warehouse. In this case, you can download the failed package and delete all the items in the error package directory, copy your local package.
  
   
***
  The same is true (the path where the package is stored, generally the repository is the root directory and the Organization Name (the package is generally a jar package, so you have the software to decompress the jar package. You can double-click it and click it all the way, what I see is the name of the organization. I tried a few and it is true that this is the case) to create a folder for the folder name, and finally create a file 2 folder named after the package name (excluding the version number, finally, create a folder named by version number, and store the corresponding version package in the last version number ).
Next, after the package is ready, right-click the Project ---> select Maven4Myeclipse ----> select Update Project, wait for updates, and then you can clean your Project. If there are still errors, if the update package is not downloaded, add your local package to the repository one by one according to the above steps.
Finally, if no error is reported, your package has been added successfully.
6. Add the applicationContext. xml file to the src/main/resources directory.
In the applicationContext. xml file, you can find the content in applicationContext. xml through spring APIs in the spring package you downloaded. The configuration information in spring4.0.0 is as follows:
   
   
    
      
      
      
    
   


7. Now you can test the project. First, go to the directory where the project is located in the dos interface of cmd to compile and test the project. Enter the following two commands:

1. mvn compile

2. mvn test-compile

If you do not enter the preceding two commands, you may not be able to find applicationContext. xml!

8. write java code test files are all placed under the src/main/java directory, and create a package name under src/main/java, write classes in the package, the details will not be written, paste the Code:

1. Write an interface:

package com.face;public interface Human {public void eat();public void walk();}


2. Write two classes that implement the interface:

Package com. imp; import com. face. human; public class American implements Human {public void eat () {// TODO Auto-generated method stubSystem. out. println ("Americans eat western food. I don't like western food !!! ");} Public void walk () {// TODO Auto-generated method stubSystem. out. println (" Americans often take a bus, and lack of exercise is not good for their health !!! ");}}



Package com. imp; import com. face. human; public class Chinese implements Human {public void eat () {// TODO Auto-generated method stubSystem. out. println !!! ");} Public void walk () {// TODO Auto-generated method stubSystem. out. println (" Chinese are walking fast, Chinese are in good health !!! ");}}


3. Write a factory

package com.factory;import com.face.Human;import com.imp.American;import com.imp.Chinese;public class Factory {public Human getHuman(String name){if("Chinese".equals(name)){return new Chinese();}else if("American".equals(name)){return new American();}else{return null;}}}

4. Write a test class

package com.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import com.face.Human;public class TestMain1 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub/*Human human=null;human=new Factory().getHuman("Chinese");human.eat();human.walk();human=new Factory().getHuman("American");human.eat();human.walk();*/ApplicationContext context =    new FileSystemXmlApplicationContext("src/main/resources/applicationContext.xml");/*ApplicationContext context =    new ClassPathXmlApplicationContext(new String[] {"services.xml", "applicationContext.xml"});*/Human human=null;human=(Human) context.getBean("chinese");human.eat();human.walk();human=(Human) context.getBean("american");human.eat();human.walk();}}



9. Now we can run the TestMain1 class as we compile and run a java program.

If Exception occurs in thread "main" java. lang. NoClassDefFoundError: org/apache/commons/logging/LogFactory
Caused by: java. lang. ClassNotFoundException: org. apache. commons. logging. LogFactory

It means that the less a package, solution: import a commons-logging.jar package, package import and the above and then pom. xml inside the configuration package is the same


If the following error occurs: Exception in thread "main" org. springframework. beans. factory. BeanDefinitionStoreException:
IOException parsing XML document from file [G: \ maven \ maven4 \ src \ applicationContext. xml]; nested exception is java. io. FileNotFoundException:
Src \ applicationContext. xml (the specified file cannot be found .)
Caused by: java. io. FileNotFoundException: applicationContext. xml (the system cannot find the specified file .)


Solution: First, check whether it has been done.

1. mvn compile 2.mvn test-compile operation. If not, perform this operation. If an error persists, follow the prompts in the directory G: \ maven \ maven4 \ src \ applicationContext. in xml], applicationContext does not exist. xml. Therefore, check whether the parameter of the FileSystemXmlApplicationContext constructor in the main method is "src/main/resources/applicationContext. xml ", if not, it should be changed to this. As follows:

ApplicationContext context =
New FileSystemXmlApplicationContext ("src/main/resources/applicationContext. xml ");



10. If everything is OK, the console will output the following information.

22:01:07 org. springframework. context. support. FileSystemXmlApplicationContext prepareRefresh
Information: Refreshing org. springframework. context. support. FileSystemXmlApplicationContext @ 742808b3: startup date [Sat Jul 26 22:01:07 CST 2014]; root of context hierarchy
22:01:07 org. springframework. beans. factory. xml. XmlBeanDefinitionReader loadBeanDefinitions
Information: Loading XML bean definitions from file [G: \ maven \ maven4 \ src \ main \ resources \ applicationContext. xml]
Chinese people will eat a lot of food in China !!!
Chinese are walking fast and Chinese are in good health !!!
Western food for Americans. I don't like western food !!!
Americans often take a bus and lack of exercise is not good for their health !!!


11. directory structure of the project:





Programming, this process is quite enjoyable. I hope that my learning process will be helpful to you and you may not understand it or feel that I have a problem. We can communicate, learn, and make progress together.
































































Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.