创建第一个Spring程序,Java常用笔试题

org.springframework

spring-context

5.0.3.RELEASE

junit

junit

4.13

test

刷新

在这里插入图片描述

检查依赖

在这里插入图片描述

定义接口与实体类


接口

public interface SomeService {

void doSome();

}

实现类

public class SomeServiceImpl implements SomeService {

//无参构造器

public SomeServiceImpl() {

System.out.println(“SomeServiceImpl无参数构造方法”);

}

@Override

public void doSome() {

System.out.println("==业务方法doSome()=");

}

}

创建 Spring 配置文件


src/main/resources/目录现创建一个 xml 文件,文件名可以随意,但

Spring 建议的名称为 applicationContext.xml

注意:spring 配置中需要加入约束文件才能正常使用,约束文件是 xsd 扩展名。

IDEA工具默认提供Spring模板

选中resources包>New>XML Confiquration File >Spring Config

在这里插入图片描述

创建成功

在这里插入图片描述

默认模板说明

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.org/schema/beans”

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”>

spring标准的配置文件:

1)根标签是 beans

  1. beans 后面的是约束文件说明

3)beans里面是bean声明。

4)什么是bean: bean就是java对象, spring容器管理的java对象,叫做bean

注册bean对象

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.org/schema/beans”

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

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”>

  • bean标签:用于定义一个实例对象。一个实例对应一个 bean 元素。

  • id:该属性是 Bean 实例的唯一标识,程序通过 id 属性访问 Bean,Bean

与 Bean 间的依赖关系也是通过 id 属性关联的。

  • class:指定该 Bean 所属的类,注意这里只能是类,不能是接口。

定义测试类


测试类定义在根目录下的test包下的java包里面

这里我们创建一个test1(名称任意)测试类

在这里插入图片描述

spring代理SomeServiceImpl对象

在这里插入图片描述

测试spring容器中代理someServiceImpl对象

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.SomeService;

public class test1 {

@Test

public void test1(){

//指定spring配置文件的位置和名称

String resources = “applicationContext.xml”;

//创建spring容器对象

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(resources);

//从spring容器中获取对象,使用id

SomeService someService = (SomeService)applicationContext.getBean(“someService”);

//执行对象的业务方法

someService.doSome();

}

}

成功运行spring代理的someServiceImpl对象

在这里插入图片描述

使用 spring 创建非自定义类对象


简单来说就是spring可以创建自定义的对象,也可以创建非自定义的对象。

上面spring代理创建的对象就是我们自定义的对象,我们可以通过spring代理非自定义的对象,例如java下的日期类(Date):

spring 配置文件加入 java.util.Date 定义:

如图

在这里插入图片描述

test1 测试类中:

调用 getBean(“myDate”); 获取日期类对象。

在这里插入图片描述

容器接口和实现类


ApplicationContext 接口(容器)

ApplicationContext 用于加载 Spring 的配置文件,在程序中充当“容

器”的角色。其实现类有两个:

使用Ctrl+H可以查看类的继承关系

在这里插入图片描述

ClassPathXmlApplicationContext和FileSystemXmlApplicationContext的区别

ClassPathXmlApplicationContext[只能读放在web-info/classes目录下的配置文件]

  • 默认就是指项目的classpath路径下面

  • 如果要使用绝对路径,需要加上file:前缀表示这是绝对路径

使用ClassPathXmlApplicationContext指定spring配置文件位置

默认就是指项目的classpath路径下面

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“applicationContext.xml”);

加上file表示使用绝对路径指定spring配置文件位置

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“file:G:/IDEA/S3/Spring/demo1/src/main/resources/applicationContext.xml”);

FileSystemXmlApplicationContext:

  • 默认是项目工作路径,即项目的根目录

  • 有file表示的是文件绝对路径

  • 如果要使用classpath路径,需要前缀classpath

使用FileSystemXmlApplicationContext指定spring配置文件位置

默认是项目工作路径,即项目的根目录,而我们的配置文件在根目录下的src/main/resources下面

ApplicationContext applicationContext = new FileSystemXmlApplicationContext(“src/main/resources/applicationContext.xml”);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值