Create a simple Spring application

Environmental installation has been completed, the next to create a simple Spring application.

Create a Spring application steps:

  1. Create a maven project
  2. Add spring library dependencies
  3. Creating Bean class
  4. Add xml file Bean's assembly
  5. Create Main Class
  6. Run the application

1. Create a maven project

Open the Eclipse, if it is not set up a development environment, reference may Spring development environment to build (the Eclipse) , menu selection: File > New > Maven Projectpop-up dialog, the following operation of FIG.

image

Click the Nextpop-up dialog, as shown operation

image

Click Finishto complete the project creation, the project is structured as follows:

image

Project directory Description:

  • src - the source directory
    • main - Code
      • java - Java code directory
      • resources - resources directory profiles, etc.
    • test - test code
  • target - the compiled output directory

pom.xml file in the root directory of the project is dependent maven package configuration file.

2. Add the spring library dependencies

Make use of the spring module added to the project. Modifications pom.xml, introduction of the following modules:

  • spring-core
  • spring-beans
  • spring-context

The complete pom.xmlcontents of the file:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.qikegu.demo</groupId>
    <artifactId>spring-helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

    </dependencies>

</project>

Next, add the code, it will add the following files:

  • Customer.java - bean class
  • applicationContext.xml - bean configuration files
  • Hello.java - master class

The final project structure shown below:

image

It will be described later in detail.

3. Create Bean class

Add CustomerBean class. Project root directory right pop-up menu, select: New -> Filespecify the directory .../src/main/java/com/qikegu/demo, add Customer.java file.

Customer.javaCode:

package com.qikegu.demo;

public class Customer { 

    String name; 
    
    public String getName() { 
        return name; 
    }
    
    public void setName(String name) { 
        this.name = name; 
    }
    
    public void displayInfo() { 
        System.out.println("Hello: "+ name); 
    } 
}

This is a simple bean class, and contains a property getter and setter methods, another displayInfo()method will print the name of the customer.

4. Add the Bean xml file assembly

In the resources directory, add file xml assembly of Bean:

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

    <bean id="customerBean" class="com.qikegu.demo.Customer"> 
        <property name="name" value="奇客谷"></property> 
    </bean>
</beans>
  • <bean>Marked as specified class definition bean.
  • <property>Tag is a child element of the bean, for setting the Customerattribute class, the property value is assigned by the IoC container to the Customerclass instance.

5. Create Main Class

Add a master class file Hello.java, as follows:

package com.qikegu.demo;

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

public class Hello { 
    public static void main(String[] args) { 
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Customer customerBean = (Customer) context.getBean("customerBean"); 
        customerBean.displayInfo(); 
        
        ((ClassPathXmlApplicationContext) context).close();
    } 
}

6. Run the application

Right-click the Hello.javapop-up menu, select Run As > Java Applicationthe output:

Hello: 奇客谷

Tutorial Series

  1. Spring Framework Introduction
  2. Spring Framework module
  3. Spring development environment to build (Eclipse)
  4. Create a simple Spring application
  5. Spring Inversion of Control container (Inversion of Control - IOC)
  6. Appreciated dependency injection (DI - Dependency Injection)
  7. Bean XML configuration (1) - through XML configuration loaded Bean
  8. Bean XML configuration (2) - Bean scope and lifecycle callback method configuration
  9. Bean XML configuration (3) - configuration dependency injection
  10. Bean XML configuration (4) - Automatic Assembly
  11. Bean annotation (Annotation) Configuration (1) - Load Bean via annotations
  12. Bean annotation (Annotation) Configuration (2) - Bean scope and lifecycle callback method configuration
  13. Bean Annotations (Annotation) configuration (3) - configuration dependency injection
  14. Bean Java Configuration
  15. Spring Aspect Oriented Programming (AOP)
  16. Spring event (1) - built-in event
  17. Spring event (2) - Custom Event

Guess you like

Origin www.cnblogs.com/jinbuqi/p/10959096.html