Spring 是目前主流的 Java Web 开发框架,是 Java 世界最为成功的框架。该框架是一个轻量级的开源框架,具有很高的凝聚力和吸引力。
Spring 框架不局限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何 Java 应用都可以从 Spring 中受益。Spring 框架还是一个超级粘合平台,除了自己提供功能外,还提供粘合其他技术和框架的能力。
OS:macOS High Sierra (Version: 10.13.6)
Java, Maven,IDEA,MariaDB等安装配置过程,见 IDEA创建Maven Quickstart项目
1. 在 IDEA上创建 Maven Quickstart 项目
New Project -> Project Type: Maven -> Project SDK: 1.8 -> select maven-archtype-quickstart: Next
Name: MavenQuickstartSpring
GroupId: com.example
ArtifactId: MavenQuickstartSpring
-> Finish
生成的项目目录结构,参考 IDEA创建Maven Quickstart项目
2. 导入Spring 依赖包
访问 http://www.mvnrepository.com/,查询 Spring
修改 pom.xml
<project ... >
...
<dependencies>
...
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.9</version>
</dependency>
...
</dependencies>
...
</project>
在IDE中项目列表 -> 点击鼠标右键 -> Maven -> Reload Project
3. 添加 src/main/resources/spring-beans.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-init-method="init"
default-destroy-method="destroy">
</beans>
4. 基于 spring-beans.xml 的 Beans
1)添加 src/main/java/com/example/hello/HelloWorld.java
package com.example.hello;
public class HelloWorld {
private String message1;
private String message2;
public void getMessage1() {
System.out.println("HelloWorld -> message1 = " + message1);
}
public void setMessage1(String message) {
this.message1 = message;
}
public void getMessage2() {
System.out.println("HelloWorld -> message2 = " + message2);
}
public void setMessage2(String message) {
this.message2 = message;
}
public void init() {
System.out.println("HelloWorld -> init()");
}
public void destroy() {
System.out.println("HelloWorld -> destroy()");
}
}
2) 添加 src/main/java/com/example/hello/HelloChina.java
package com.example.hello;
public class HelloChina {
private String message1;
private String message2;
private String message3;
private String emptyValue;
private String nullValue;
public void setMessage1(String message) {
this.message1 = message;
}
public void setMessage2(String message) {
this.message2 = message;
}
public void setMessage3(String message) {
this.message3 = message;
}
public void getMessage1() {
System.out.println("HelloChina -> message1 = " + message1);
}
public void getMessage2() {
System.out.println("HelloChina -> message2 = " + message2);
}
public void getMessage3() {
System.out.println("HelloChina -> message3 = " + message3);
}
public void getEmptyValue() {
System.out.println("HelloChina -> emptyValue = " + emptyValue);
}
public void setEmptyValue(String emptyValue) {
this.emptyValue = emptyValue;
}
public void getNullValue() {
System.out.println("HelloChina -> nullValue = " + nullValue);
}
public void setNullValue(String nullValue) {
this.nullValue = nullValue;
}
public void init() {
System.out.println("HelloChina -> init()");
}
public void destroy() {
System.out.println("HelloChina -> destroy()");
}
}
3) 添加 src/main/java/com/example/hello/InitHelloWorld.java
package com.example.hello;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
public class InitHelloWorld implements BeanPostProcessor, Ordered {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("InitHelloWorld -> postProcessBeforeInitialization(): " + beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("InitHelloWorld -> postProcessAfterInitialization(): " + beanName);
return bean;
}
@Override
public int getOrder() {
return 3;
}
}
4) 添加 src/main/java/com/example/hello/InitHelloWorld2.java
package com.example.hello;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
public class InitHelloWorld2 implements BeanPostProcessor, Ordered {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("InitHelloWorld2 -> postProcessBeforeInitialization(): " + beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("InitHelloWorld2 -> postProcessAfterInitialization(): " + beanName);
return bean;
}
@Override
public int getOrder() {
return 0;
}
}
5) 修改 src/main/resources/spring-beans.xml
<beans ...>
...
<bean id="helloWorld" class="com.example.hello.HelloWorld">
<property name="message1" value="Hello World 1" />
<property name="message2" value="Hello World 2" />
</bean>
<bean id="helloChina" class="com.example.hello.HelloChina" parent="helloWorld">
<property name="message3" value="Hello China 3" />
<property name="emptyValue" value="" />
<property name="nullValue"><null/></property>
</bean>
<!-- 后置处理器 -->
<bean class="com.example.hello.InitHelloWorld" />
<bean class="com.example.hello.InitHelloWorld2" />
...
</beans>
5. 基于注解 @Component, @Resource 的 Beans
1) 添加 src/main/java/com/example/Man.java
package com.example;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Value;
@Component("man")
public class Man {
@Value("${name}")
private String name;
@Value("${age}")
private int age;
public Man() {
System.out.println("Man -> Man()");
}
public Man(String name, int age) {
System.out.println("Man -> Man(name, age)");
this.name = name;
this.age = age;
}
public void show() {
System.out.println("Main -> show(): name = " + name + ", age = " + age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2) 添加 src/main/java/com/example/Person.java
package com.example;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component("person")
public class Person {
@Resource(name="man")
private Man man;
/*
// 构造函数注入
public Person(Man man) {
System.out.println("Person -> Person(man)");
this.man = man;
}
*/
public void man() {
man.show();
}
public Man getMan() {
return man;
}
public void setMan(Man man) {
System.out.println("Person -> setMan()");
this.man = man;
}
}
3) 添加 src/main/resources/config.properties
name=tester
age=25
4) 修改 src/main/resources/spring-beans.xml
<beans ...>
...
<!-- 使用context命名空间,通知spring扫描指定目录,进行注解的解析 -->
<context:component-scan base-package="com.example" />
<context:property-placeholder location="classpath:config.properties"/>
...
</beans>
6. 添加 src/main/java/com/example/App.java
package com.example;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ksmstock.hello.HelloWorld;
import com.ksmstock.hello.HelloChina;
/**
* Hello world!
*
*/
public class App {
public static void main( String[] args ) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-beans.xml");
HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
objA.getMessage1();
objA.getMessage2();
HelloChina objB = (HelloChina) context.getBean("helloChina");
objB.getMessage1();
objB.getMessage2();
objB.getMessage3();
objB.getEmptyValue();
objB.getNullValue();
Person person = (Person) context.getBean("person");
person.man();
context.registerShutdownHook();
}
}
7. 编译运行
1) Run App.main()
打开 App.java, 点击鼠标右键, 选择 Run "App.main()"
2) Edit Configurations
Click "+" add new configuration -> Select "Application"
Name: MavenQuickstartSpring
Main class: com.example.App
-> Apply / OK
Click Run "MavenQuickstartSpring"