Spring框架-Java學習路線課程第一課:Spring核心

本博客地址 | 小站坐坐 | GitHub

JavaEESpring核心

1、簡單工廠模式
  • 產品的標準 interface(接口) Pet(寵物)
  • 產品 Dog(狗狗) Cat(貓)
  • 工廠 Factorys
  • 測試(客戶端/服務器) test junit(單元測試)
2、簡單工廠模式的演示案例:
  • 制定工廠生產產品的標準:
public interface Pet{
	public void getEat();
	public void getSport();
}
  • 工廠:
public class Factorys{
	//常量,用來交互
	public static final String DOG="dog";
	public static final String CAT="cat";
	//方法:帶入參數,返回我們要的產品對象
	public Pet getPet(String name){
		if("DOG".equals(name)){
			return new Dog();
		}else if("CAT".equals(name)){
			return new Cat();
		}else{
			return null;
		}
	}
}
  • 產品
    • Dog
public class Dog implements Pet{//ServiceImpl(業務實現類)  DaoImpl(dao實現類)
	public void getEat(){
		System.out.println("狗喫骨頭");
	
	}
	public void getSport(){
		System.out.println("狗喜歡運動");
	}
}
  • Cat
public class Cat implements Pet{
	public void getEat(){
		System.out.println("貓喫魚");
	
	}
	public void getSport(){
		System.out.println("貓很懶,不喜歡運動");
	}
}
  • 測試類
/**
 * @Author:DongGaoYun
 * @Description:
 * @Date 2019-9-20 下午3:23:14
 * @Version 1.0
 * @Company: www.springhome.org
 */
package com.javaxyz.test;

import static org.junit.Assert.*;


import org.apache.taglibs.standard.lang.jstl.test.beans.Factory;
import org.junit.Test;

import com.javaxyz.entity.Dog;
import com.javaxyz.factorys.Factorys;
import com.javaxyz.interfaces.Pet;

/**
 * @ClassName:FactoryTest.java
 * @Description:描述信息
 * @Author:DongGaoYun 
 * @URL: www.gyun.org
 * @Email:[email protected]
 * @QQ:1050968899
 * @WeiXin:QingYunJiao
 * @Date:2019-9-20 下午3:23:14
 * @Version:1.0
 */
@SuppressWarnings("all")
public class FactoryTest {

	@Test
	public void test() {
		//創建對象
		Factorys factory=new Factorys();
		Pet pet = null;
//		pet = factory.getPet("dog");
		pet = factory.getPet("cat");
		//調用方法
		pet.getEat();
		pet.getSport();		
	}
}
3、bug
java.lang.NullPointerException
	at com.javaxyz.test.FactoryTest.test(FactoryTest.java:37)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
4、出現bug的位置:
  • 原因:
    • DOG和CAT常量名加上了雙引號,如圖所示:
      在這裏插入圖片描述
      修改爲:如圖
      在這裏插入圖片描述
5、解決bug方法:
  • 細心查看一行一行關鍵代碼,找出錯誤點
  • debug模式查找錯誤
6、實現控制反轉功能,SpringIOC的小Demo演示案例:
  • 需求:輸出完整的Hello,World!

  • 需求分析後,實現控制反轉需有以下幾步驟:

    • 新建項目
    • 放入jar
    • resources目錄增加配置文件applicationContext.xml
    • resources原有的配置文件
      • database.properties
      • log4j.properties
    • 在src下的新建包名後,編寫HelloWorld.java類
    • 繼續編寫applicationContext.xml配置
    • 編寫測試代碼
    • 測試完美,成功輸出完整的Hello,World!,項目結束。
7、項目結構:

在這裏插入圖片描述

8、新建或從前章項目中複製過來resources目錄,並增加配置文件applicationContext.xml,配置文件頭如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
9、創建並編寫HelloWorld.java類
/**
 * @Author:DongGaoYun
 * @Description:
 * @Date 2019-9-20 下午4:27:00
 * @Version 1.0
 * @Company: www.springhome.org
 */
package com.javaxyz.spring;

/**
 * @ClassName:HelloWorld.java
 * @Description:SpringIOC的依賴注入
 * @Author:DongGaoYun 
 * @URL: www.gyun.org
 * @Email:[email protected]
 * @QQ:1050968899
 * @WeiXin:QingYunJiao
 * @Date:2019-9-20 下午4:27:00
 * @Version:1.0
 */
public class HelloWorld {
	//定義一個變量
	private String strName;

	/**
	 * 獲得值
	 * @return the strName
	 */
	public String getStrName() {
		return strName;
	}

	/**
	 * 設置(注入值)
	 * @param strName the strName to set
	 */
	public void setStrName(String strName) {
		this.strName = strName;
	} 
	//打印完整的HelloWorld
	public void print(){
		System.out.println(strName+",World!");
	}	
}

10、繼續編寫applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<!-- 創建bean對象 id是唯一的,可以方便引用,class是完全限定名 -->
	<bean id="hello" class="com.javaxyz.spring.HelloWorld">
		<!-- 給實例的屬性賦值 -->
		<property name="strName">
			<value>Hello</value>
		</property>
	</bean>
</beans>
11、測試
/**
 * @Author:DongGaoYun
 * @Description:
 * @Date 2019-9-20 下午3:23:14
 * @Version 1.0
 * @Company: www.springhome.org
 */
package com.javaxyz.test;

import static org.junit.Assert.*;

import org.apache.taglibs.standard.lang.jstl.test.beans.Factory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.javaxyz.entity.Dog;
import com.javaxyz.factorys.Factorys;
import com.javaxyz.interfaces.Pet;
import com.javaxyz.spring.HelloWorld;

/**
 * @ClassName:FactoryTest.java
 * @Description:測試SpringIOC注入
 * @Author:DongGaoYun
 * @URL: www.gyun.org
 * @Email:[email protected]
 * @QQ:1050968899
 * @WeiXin:QingYunJiao
 * @Date:2019-9-20 下午3:23:14
 * @Version:1.0
 */
@SuppressWarnings("all")
public class FactoryTest {

	@Test
	public void test2() {
		// 讀取配置文件
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		HelloWorld hello = (HelloWorld) context.getBean("hello");
		hello.print();
	}
}
12、本章思維導圖:

在這裏插入圖片描述

(待補充,請繼續關注!)


——————— 精 選 文 章 ———————
  1. Java入門-Java學習路線課程第一課:初識JAVA
  2. Java入門-Java學習路線課程第二課:變量與數據類型
  3. Java入門-Java學習路線課程第三課:選擇結構
  4. Java入門-Java學習路線課程第四課:循環結構
  5. Java入門-Java學習路線課程第五課:一維數組
  6. Java入門-Java學習路線課程第六課:二維數組
  7. Java入門-Java學習路線課程第七課:類和對象
  8. Java入門-Java學習路線課程第八課:方法和方法重載
  9. java學習:在給學生演示用Myeclipse10.7.1工具生成War時,意外報錯:SECURITY: INTEGRITY CHECK ERROR
  10. 使用jquery發送Ajax請求的幾種異步刷新方式
  11. idea Springboot啓動時內嵌tomcat報錯- An incompatible version [1.1.33] of the APR based Apache Tomcat Native

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章