1. 程式人生 > >Spring複習筆記-1

Spring複習筆記-1

需要研究定性一個小工程,所以需要複習一下spring,目前已經是spring5.0以上,java10以上,思維也已經變化了很多。最開始看了一些新特性基本是加強了註解,減少了配置檔案,但個人需要巨集觀檢視物件的依賴關係所以暫時還要看傳統的配置檔案方式,這樣的方式我更認為老而彌堅。

選擇的是Eclipse集合

首先新建三個maven工程:

work工程是父工程,管理依賴其實不用如下這麼多,因為根據依賴傳遞只需要配置spring-context即可匯入基本依賴包,但為了記住基本依賴所以全部加入,只是為了複習定性,工程版本管理沒有藉助原始碼管理工具,所以採用了最土的辦法,只寫了一個<user.pistachio.version>0.0.1-SNAPSHOT</user.pistachio.version>來定義版本,那麼子工程也可以使用這個版本號,這樣做會有警告,說maven今後不會支援,其實我用的就是最新的maven3.6,這樣的提示不是一天兩天了,

<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">
	<properties>
		<user.pistachio.version>0.0.1-SNAPSHOT</user.pistachio.version>
		<spring.version>5.1.2.RELEASE</spring.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.springstudy</groupId>
	<artifactId>work</artifactId>
	<version>${user.pistachio.version}</version>
	<packaging>pom</packaging>
	<modules>
		<module>../springguides</module>
		<module>../framework</module>
	</modules>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-core</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aop</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-expression</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>com.springstudy</groupId>
				<artifactId>framework</artifactId>
				<version>${user.pistachio.version}</version>
			</dependency>

		</dependencies>
	</dependencyManagement>
</project>

兩個子工程

<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">
	<parent>
		<groupId>com.springstudy</groupId>
		<artifactId>work</artifactId>
		<version>${user.pistachio.version}</version>
		<relativePath>../work</relativePath>
	</parent>
	<modelVersion>4.0.0</modelVersion>
	<artifactId>springguides</artifactId>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
		</dependency>
		<dependency>
		  <groupId>com.springstudy</groupId>
		  <artifactId>framework</artifactId>		  
		</dependency>
	</dependencies>
</project>
<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>
  <parent>
    <groupId>com.springstudy</groupId>
    <artifactId>work</artifactId>
    <version>${user.pistachio.version}</version>
    <relativePath>../work</relativePath>
  </parent>
  <artifactId>framework</artifactId>
</project>

先在guides工程裡寫一個基本的登入行為類

package com.springstudy;

import com.springstudy.framework.Action;
import com.springstudy.framework.BasicMessage;


public class LoginAction implements Action {
	private String name;
	private String password;
	private String pageUrl;
	private int loginCnt;
	private double netCoins; 

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPageUrl() {
		return pageUrl;
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}

	public int getLoginCnt() {
		return loginCnt;
	}

	public void setLoginCnt(int loginCnt) {
		this.loginCnt = loginCnt;
	}

	public double getNetCoins() {
		return netCoins;
	}

	public void setNetCoins(double netCoins) {
		this.netCoins = netCoins;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	
	public LoginAction(String name, String pageUrl, int loginCnt) {
		super();
		this.name = name;
		this.pageUrl = pageUrl;
		this.loginCnt = loginCnt;
		System.out.println("[String,String,int建構函式初始化]"+this.toString());
	}
	

	public LoginAction(String name, String pageUrl, double netCoins) {
		super();
		this.name = name;
		this.pageUrl = pageUrl;
		this.netCoins = netCoins;
		System.out.println("[String,String,double建構函式初始化]"+this.toString());
	}

	@Override
	public String toString() {
		return "LoginAction [name=" + name + ", password=" + password + ", pageUrl=" + pageUrl + ", loginCnt="
				+ loginCnt + ", netCoins=" + netCoins + "]"+", HashCode [="+this.hashCode()+"]";
	}

	public LoginAction() {
		System.out.println("無引數構造器開始初始化"+this.toString());
	}

	@Override
	public BasicMessage execute(){
		BasicMessage bm=BasicMessage.NONE;
		if(name!=null && name.equals("admin")) {
			bm=BasicMessage.SUCCESS;
			return bm;
		}
		return bm;
	}

}

上層framework工程:Action和基本訊息列舉

package com.springstudy.framework;

public interface Action {
    public BasicMessage execute();
}
package com.springstudy.framework;

public enum BasicMessage {
	SUCCESS("成功", 1), NONE("無訊息", 0),ERROR("錯誤", -1);  
    private String name;  
    private int index;  
    private BasicMessage(String name, int index) {  
        this.name = name;  
        this.index = index;  
    }  
    public static String getName(int index) {  
        for (BasicMessage c : BasicMessage.values()) {  
            if (c.getIndex() == index) {  
                return c.name;  
            }  
        }  
        return null;  
    }  
    public String getName() {  
        return name;  
    }  
    public int getIndex() {  
        return index;  
    }  
}

寫上一個main類來寫入口函式:

package com.springstudy;

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

import com.springstudy.framework.Action;

public class Main {

	public static void main(String[] args) {
		// 傳統方式,誰用物件就要建立它
		LoginAction la = new LoginAction();
		la.setName("admin");

		System.out.println(la.execute().getName());

		// Ioc控制容器負責建立物件,管理物件關係和物件生命週期,DI方式主要為約定實現方式,set,構造器,工廠注入三種方法。
		// 控制反轉是把建立任務交給容器,這樣更符合自然,物件本來就存在,我們只是需要它一起協作來完成任務,更符合人類任務協作。
		// 依賴注入,依賴誰注入?對於spring來說是依賴容器注入屬性。
		@SuppressWarnings("resource")
		ApplicationContext ctx = new ClassPathXmlApplicationContext("App.xml");
		Action springlact = ctx.getBean("loginAction", Action.class);
		System.out.println(springlact.execute().getName());
		//擺脫了對LoginAction的直接依賴,轉變為對spring容器和資料(配置bean的id字面值)的依賴
		ctx.getBean("loginAction1", Action.class);
		ctx.getBean("loginAction2", Action.class);
		ctx.getBean("loginAction3", Action.class);
		ctx.getBean("loginAction4", Action.class);

	}

}

然後是spring配置檔案App.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.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- set注入方式 -->
	<bean id="loginAction" class="com.springstudy.LoginAction">
		<property name="name" value="spring"></property>
	</bean>
	<!--採用LoginAction(String name, String pageUrl, int loginCnt)建構函式 -->
	<bean id="loginAction1" class="com.springstudy.LoginAction">
		<constructor-arg value="Tom"></constructor-arg>
		<constructor-arg value="http://sc.skcompany"></constructor-arg>
		<constructor-arg value="1"></constructor-arg>
	</bean>
	<!--採用LoginAction(String name, String pageUrl, double netCoins)建構函式 -->
	<bean id="loginAction2" class="com.springstudy.LoginAction">
		<constructor-arg index="0" value="Juidy"></constructor-arg>
		<constructor-arg index="1" value="http://sc.skcompany"></constructor-arg>
		<constructor-arg index="2" value="1.5"></constructor-arg>
	</bean>
	<!--採用LoginAction(String name, String pageUrl, double netCoins)建構函式 ,通過指定型別,這樣字面值2不會使用int引數型別的建構函式 -->
	<bean id="loginAction3" class="com.springstudy.LoginAction">
		<constructor-arg index="0" value="Juidy"></constructor-arg>
		<constructor-arg index="1" value="http://sc.skcompany"></constructor-arg>
		<constructor-arg index="2" value="2" type="double"></constructor-arg>
	</bean>
	<!-- name方式,如果有特殊字元用CDATA方式,通過以上方式得出結論,如果含有多個建構函式的類,如果有型別歧義,那麼不如使用如下方式或是set注入方式 -->
	<bean id="loginAction4" class="com.springstudy.LoginAction">
		<constructor-arg name="name" value="James"></constructor-arg>
		<constructor-arg name="pageUrl">
			<value><![CDATA[http://sc.skcompany]]></value>
		</constructor-arg>
		<constructor-arg name="netCoins" value="2"></constructor-arg>
	</bean>
</beans>

這一次複習了,bean的set和構造方法注入屬性方式,構造方法歧義的最佳思路,使用CDATA封裝非法字元,確定了Ioc和DI的行為,概念上不能混為一談,可能今後有更好的實現Ioc行為方式。