首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

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

  • 25-02-21 23:22
  • 3552
  • 8872
blog.csdn.net

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

JavaEESpring核心

文章目录

          • 1、简单工厂模式
          • 2、简单工厂模式的演示案例:
          • 3、bug
          • 4、出现bug的位置:
          • 5、解决bug方法:
          • 6、实现控制反转功能,SpringIOC的小Demo演示案例:
          • 7、项目结构:
          • 8、新建或从前章项目中复制过来resources目录,并增加配置文件applicationContext.xml,配置文件头如下:
          • 9、创建并编写HelloWorld.java类
          • 10、继续编写applicationContext.xml配置
          • 11、测试
          • 12、本章思维导图:

1、简单工厂模式
  • 产品的标准 interface(接口) Pet(宠物)
  • 产品 Dog(狗狗) Cat(猫)
  • 工厂 Factorys
  • 测试(客户端/服务器) test junit(单元测试)
2、简单工厂模式的演示案例:
  • 制定工厂生产产品的标准:
public interface Pet{
	public void getEat();
	public void getSport();
}
  • 1
  • 2
  • 3
  • 4
  • 工厂:
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;
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 产品
    • Dog
public class Dog implements Pet{//ServiceImpl(业务实现类)  DaoImpl(dao实现类)
	public void getEat(){
		System.out.println("狗吃骨头");
	
	}
	public void getSport(){
		System.out.println("狗喜欢运动");
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • Cat
public class Cat implements Pet{
	public void getEat(){
		System.out.println("猫吃鱼");
	
	}
	public void getSport(){
		System.out.println("猫很懒,不喜欢运动");
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 测试类
/**
 * @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();		
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
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,配置文件头如下:

<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">
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
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!");
	}	
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
10、继续编写applicationContext.xml配置

<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="hello" class="com.javaxyz.spring.HelloWorld">
		
		<property name="strName">
			<value>Hellovalue>
		property>
	bean>
beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
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();
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
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

文章知识点与官方知识档案匹配,可进一步学习相关知识
Java技能树首页概览150366 人正在系统学习中
注:本文转载自blog.csdn.net的青云交的文章"https://blog.csdn.net/atgfg/article/details/101079591"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2491) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top