Spring框架-Java学习路线课程第一课:Spring核心

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/atgfg/article/details/101079591

本博客地址 | 小站坐坐 | 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

猜你喜欢

转载自blog.csdn.net/atgfg/article/details/101079591