Environmental installation has been completed, the next to create a simple Spring application.
Create a Spring application steps:
- Create a maven project
- Add spring library dependencies
- Creating Bean class
- Add xml file Bean's assembly
- Create Main Class
- 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 Project
pop-up dialog, the following operation of FIG.
Click the Next
pop-up dialog, as shown operation
Click Finish
to complete the project creation, the project is structured as follows:
Project directory Description:
- src - the source directory
- main - Code
- java - Java code directory
- resources - resources directory profiles, etc.
- test - test code
- main - 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.xml
contents 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:
It will be described later in detail.
3. Create Bean class
Add Customer
Bean class. Project root directory right pop-up menu, select: New -> File
specify the directory .../src/main/java/com/qikegu/demo
, add Customer.java file.
Customer.java
Code:
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 theCustomer
attribute class, the property value is assigned by the IoC container to theCustomer
class 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.java
pop-up menu, select Run As > Java Application
the output:
Hello: 奇客谷
Tutorial Series
- Spring Framework Introduction
- Spring Framework module
- Spring development environment to build (Eclipse)
- Create a simple Spring application
- Spring Inversion of Control container (Inversion of Control - IOC)
- Appreciated dependency injection (DI - Dependency Injection)
- Bean XML configuration (1) - through XML configuration loaded Bean
- Bean XML configuration (2) - Bean scope and lifecycle callback method configuration
- Bean XML configuration (3) - configuration dependency injection
- Bean XML configuration (4) - Automatic Assembly
- Bean annotation (Annotation) Configuration (1) - Load Bean via annotations
- Bean annotation (Annotation) Configuration (2) - Bean scope and lifecycle callback method configuration
- Bean Annotations (Annotation) configuration (3) - configuration dependency injection
- Bean Java Configuration
- Spring Aspect Oriented Programming (AOP)
- Spring event (1) - built-in event
- Spring event (2) - Custom Event