后端开发基础-Spring框架学习-001——基础概念

作用域

. 默认情况下,对于一个bean,spring容器只会

创建一个实例。

. spring容器在启动之后,会扫描整个配置文件,

然后将单例的bean先创建好。

延迟加载

. spring容器在启动之后,会扫描整个配置文件,

然后将单例的bean先创建好。

. 如果希望只有当getBean时才创建,可以延迟

加载。

IOC(Inversion Of Controll 控制反转)

===============================

IOC是什么?


对象之间的依赖关系交给容器来管理。

DI是什么(Dependency injection 依赖注入)?


容器通过调用对象的构造器或者set方法

来建立对象之间的依赖关系。

注:

IOC是目标,DI是手段。

DI的几种方式


set方法(重点)

. 容器调用对象的set方法来建立对象之间的

依赖关系。

. 有无参构造器

. 有set方法

构造器

. 容器调用对象的构造器来建立对象之间的

依赖关系。

. 有带参构造器

generated by haroopad


案例演示:

工程案例目录结构:

需要使用到的Jar:

pom.xml

4.0.0

com.study

springcase-day01

0.0.1-SNAPSHOT

war

org.springframework

spring-webmvc

3.2.8.RELEASE

junit

junit

4.12

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

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

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

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

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

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

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

xmlns:jpa=“http://www.springframework.org/schema/data/jpa”

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd

http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

<bean id=“eb1”

class=“container.instantiation.ExampleBean”/>

<bean id=“cal1” class=“java.util.Calendar”

factory-method=“getInstance”/>

<bean id=“cal2”

class=“java.util.GregorianCalendar”/>

<bean id=“time1” factory-bean=“cal2”

factory-method=“getTime”/>

StartSping.java

package container.first;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StartSping {

public static void main(String[] args) {

//启动spring容器(配置文件名不区分大小写)

ApplicationContext ac = new ClassPathXmlApplicationContext(“applicationcontext.xml”);

System.out.println(ac);

}

}

  1. 演示启动spring容器

运行程序后,后台执行结果:

演示 创建对象的方式:

ExampleBean.java

package container.instantiation;

public class ExampleBean {

public ExampleBean() {

System.out.println(“ExamleBean的无参构造器…”);

}

}

TestCase.java

package test;

import java.util.Calendar;

import java.util.Date;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import container.instantiation.ExampleBean;

public class TestCase {

@Test

//测试容器创建对象的第一种方式(调用无参构造器)

public void test1(){

//启动容器

ApplicationContext ac = new ClassPathXmlApplicationContext(“applicationcontext.xml”);

System.out.println(ac);

//向容器申请获得一个对象

ExampleBean eb1 = ac.getBean(“eb1”, ExampleBean.class);

System.out.println(eb1);

Date date1 = ac.getBean(“date1”, Date.class);

System.out.println(date1);

}

@Test

//测试容器创建对象的第二种方式(静态工厂方法)

public void test2(){

//启动容器

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

System.out.println(ac);

Calendar cal = ac.getBean(“cal1”, Calendar.class);

System.out.println(cal);

}

@Test

//测试容器创建对象的第三种方式(实例工厂方法)

public void test3(){

//启动容器

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

System.out.println(ac);

Date date = ac.getBean(“time1”, Date.class);

System.out.println(date);

}

}

依次运行test1,test2,test3,后台运行结果:

A.

B.

C.

生命周期的管理、作用域、延迟加载****演示

MessageBean.java

package container.life;

public class MessageBean {

public MessageBean(){

System.out.println(“MessageBean的无参构造器…”);

}

public void init(){

System.out.println(“init方法…”);

}

public void sendMsg(){

System.out.println(“发送消息…”);

}

public void destroy(){

System.out.println(“destroy方法…”);

}

}

SomeBean.java

package container.life;

public class SomeBean {

public SomeBean(){

System.out.println(“SomeBean的无参构造器…”);

}

}

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

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

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

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

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

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

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

xmlns:jpa=“http://www.springframework.org/schema/data/jpa”

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd

http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

<bean id=“mb1”

class=“container.life.MessageBean”

init-method=“init”

destroy-method=“destroy”

scope=“prototype”>

<bean id=“sb1” class=“container.life.SomeBean”

scope=“prototype”>

<bean id=“sb2” class=“container.life.SomeBean”

lazy-init=“true”>

TestCae2.java

package test;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import container.life.MessageBean;

import container.life.SomeBean;

public class TestCae2 {

@Test

//测试生命周期相关的几个

public void test1(){

//关闭容器的方法在ApplicationContext

//接口当中没有提供,需要使用其子接口

//AbstractApplicationContext

AbstractApplicationContext ac = new ClassPathXmlApplicationContext(“app2.xml”);

MessageBean mb = ac.getBean(“mb1”,MessageBean.class);

mb.sendMsg();

System.out.println(mb);

ac.close();

}

@Test

//测试作用域

public void test2(){

ApplicationContext ac = new ClassPathXmlApplicationContext(“app2.xml”);

SomeBean sb1 = ac.getBean(“sb1”, SomeBean.class);

最后

小编精心为大家准备了一手资料

以上Java高级架构资料、源码、笔记、视频。Dubbo、Redis、设计模式、Netty、zookeeper、Spring cloud、分布式、高并发等架构技术

【附】架构书籍

  1. BAT面试的20道高频数据库问题解析
  2. Java面试宝典
  3. Netty实战
  4. 算法

BATJ面试要点及Java架构师进阶资料

//接口当中没有提供,需要使用其子接口

//AbstractApplicationContext

AbstractApplicationContext ac = new ClassPathXmlApplicationContext(“app2.xml”);

MessageBean mb = ac.getBean(“mb1”,MessageBean.class);

mb.sendMsg();

System.out.println(mb);

ac.close();

}

@Test

//测试作用域

public void test2(){

ApplicationContext ac = new ClassPathXmlApplicationContext(“app2.xml”);

SomeBean sb1 = ac.getBean(“sb1”, SomeBean.class);

最后

小编精心为大家准备了一手资料

[外链图片转存中…(img-z1OXiq6h-1719191791791)]

[外链图片转存中…(img-TfJtz792-1719191791792)]

以上Java高级架构资料、源码、笔记、视频。Dubbo、Redis、设计模式、Netty、zookeeper、Spring cloud、分布式、高并发等架构技术

【附】架构书籍

  1. BAT面试的20道高频数据库问题解析
  2. Java面试宝典
  3. Netty实战
  4. 算法

[外链图片转存中…(img-ckYraBVS-1719191791792)]

BATJ面试要点及Java架构师进阶资料

[外链图片转存中…(img-Svtd48ei-1719191791793)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值