Spring 是目前主流的 Java Web 開發(fā)框架,是 Java 世界最為成功的框架。該框架是一個輕量級的開源框架,具有很高的凝聚力和吸引力。
Spring 框架不局限于服務(wù)器端的開發(fā)。從簡單性、可測試性和松耦合的角度而言,任何 Java 應(yīng)用都可以從 Spring 中受益。Spring 框架還是一個超級粘合平臺,除了自己提供功能外,還提供粘合其他技術(shù)和框架的能力。
OS:macOS High Sierra (Version: 10.13.6)
Java, Maven,IDEA,MariaDB等安裝配置過程,見?IDEA創(chuàng)建Maven Quickstart項目
1. 在 IDEA上創(chuàng)建 Maven Quickstart 項目
?? New Project -> Project Type: Maven -> Project SDK: 1.8 -> select maven-archtype-quickstart: Next
?????? Name: MavenQuickstartSpring
?????? GroupId: com.example
?????? ArtifactId: MavenQuickstartSpring
?? -> Finish
????生成的項目目錄結(jié)構(gòu),參考?IDEA創(chuàng)建Maven Quickstart項目
2.? 導(dǎo)入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中項目列表 -> 點擊鼠標(biāo)右鍵 -> 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;
/*
? ? // 構(gòu)造函數(shù)注入
? ? 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, 點擊鼠標(biāo)右鍵, 選擇 Run "App.main()"
????2) Edit Configurations
????????Click "+" add new configuration -> Select "Application"
????????????Name: MavenQuickstartSpring
????????????Main class: com.example.App
????????-> Apply / OK
????????Click Run "MavenQuickstartSpring"