Using maven to create spring project registration bean, running successfully

1: Project creation and configuration

1.

2 if your maven warehouse is not under the default.. / user / directory, modify the settings settings, and then pom.xml Add spring dependency in. Note that you need to re-enter it even if it is not reported as red after adding

 

 

 

3. Create a test class. I write a javabean Book class casually here. class Main is used to test whether the javabean is registered successfully.

4. Create the spring configuration file applicationContext.xml And register the JavaBean book class above. Note here that if you don't add the spring dependency well, you can't find the profile option. As shown in Figure 1:

After the spring context dependency is added successfully, the following screen is displayed:

 

The content of the created spring configuration file is:

Next, register the bean Book class you just wrote. Note that the path of class should be complete. Starting from the next level of java directory, here I am bean.Book , (with my catalog level)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="book" class="bean.Book"/>
    
</beans>

 

5. Load the configuration file.

Content in Main:

package util;

import bean.Book;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        Book book=(Book) ctx.getBean("book");
        System.out.println(book);

    }
}

Of course, in addition to ClassPathXmlApplicationContext (to find the configuration file under classpath), you can also use FileSystemXmlApplicationContext to load from the operating system path. As follows:

package util;

import bean.Book;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
//Load from operating system path
        FileSystemXmlApplicationContext ftx=new FileSystemXmlApplicationContext("E:\\mvnPro\\project\\src\\main\\resources\\applicationContext.xml");
        Book book=(Book)ftx.getBean("book");
        System.out.println(book);


    }
}

If successful, the toString method rewritten by the Book class will be printed, but the following situations may be encountered:

Error:java : error: release 5 is not supported

Error:java : source option 5 is no longer supported. Please use version 6 or later.

You can modify the following aspects according to your corresponding jdk version:

 

 

And then Pom.xml Add your own jdk attributes to

  
   <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>14</maven.compiler.source>
    <maven.compiler.target>14</maven.compiler.target>
</properties>

Finally, run successfully prints the book information... (toString method not overridden)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Keywords: xml Spring Maven Java

Added by Hellusius on Mon, 15 Jun 2020 08:56:53 +0300