Idea搭建Spring和入门案例讲解

待我称王封你为后i 2022-03-22 04:44 303阅读 0赞

Spring入门

Spring是什么?

Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架(不仅可以用来开发se也可以用来开发ee),它解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。

idea搭建Spring

1.创建一个工程
在这里插入图片描述
2.新建xml配置文件
在这里插入图片描述
这样就用idea完成了创建步骤!接下来就是对Spring的讲解和入门案例分析

Spring的IoC(:Inverse Of Control)和DI(Dependency Injection)

IoC:控制反转,将对象的创建权反转给Spring。
DI:依赖注入,在有IoC环境的前提下,Spring管理这个类时将类的依赖的属性注入(设置)进来。
依赖关系:比如定义一个A类,B类使用的时候要传入一个A类,这就是B依赖A。
先创建一个bean
package test;

public class TestBean {

  1. //姓名
  2. private String name;
  3. //年龄
  4. private int age;
  5. public int getAge() {
  6. return age;
  7. }
  8. public void setAge(int age) {
  9. this.age = age;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. /**
  18. * 自我介绍
  19. */
  20. public void intrduce(){
  21. System.out.println("您好,我叫"+this.name+"今年"+this.age+"岁!");
  22. }
  23. /**
  24. *初始化
  25. */
  26. public void init(){
  27. System.out.println("创建!");
  28. }
  29. /**
  30. *销毁
  31. */
  32. public void destroy(){
  33. System.out.println("销毁!");
  34. }

}

1.将控制权给Spring(第一行)如下图:
2.DI依赖注入,注入依赖值
3.执行方法
在这里插入图片描述在这里插入图片描述

Spring的工厂类

1.BeanFactory(老版本):在调用getBean时加载类的实例

2.ApplicationContext(新版本):在配置xml时加载,是BeanFactory的子类。
实现类:
(1)ClassPathXmlApplicationContext:加载类路径下的配置文件
(2)FileSystemXmlApplicationContext:加载磁盘的配置文件

Spring的配置

Bean的配置
1.bean标签里id和name的区别
(1)id:不能重复,不能出现特殊字符
(2)name:可以重复
2.class是生成的类的全路径
3.生命周期的配置
(1)初始化:init-method ,bean在初始化时执行的方法
(2)销毁:destroy-method, bean在销毁时执行的方法(必须是单例创建,解释在下文的作用范围配置,工厂的关闭方法)

  1. public class test01 {
  2. public static void main(String[] args) {
  3. //创建Spring工厂
  4. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  5. TestBean testBean = (TestBean) applicationContext.getBean("IntrduceDemo");
  6. testBean.intrduce();
  7. ((ClassPathXmlApplicationContext) applicationContext).close();
  8. }
  9. }

在这里插入图片描述
4.bean作用范围的配置scope
(1)单例:singleton,默认,每次从工厂创建的都是同一个对象,因此地址也相同

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  4. <bean id="IntrduceDemo" class="test.TestBean" init-method="init" destroy-method="destroy" scope="prototype">
  5. <property name="name" value="魏澳"/>
  6. <property name="age" value="20"/>
  7. </bean>
  8. </beans>
  9. //单例 初始化两个对象,分别打印地址
  10. public class test01 {
  11. public static void main(String[] args) {
  12. //创建Spring工厂
  13. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  14. TestBean testBean = (TestBean) applicationContext.getBean("IntrduceDemo");
  15. ApplicationContext applicationContext1 = new ClassPathXmlApplicationContext("applicationContext.xml");
  16. TestBean testBean1 = (TestBean) applicationContext1.getBean("IntrduceDemo");
  17. System.out.println("testBean:"+testBean);
  18. System.out.println("testBean:"+testBean1);
  19. testBean.intrduce();
  20. ((ClassPathXmlApplicationContext) applicationContext).close();
  21. }
  22. }

在这里插入图片描述
显然是一个创建一个销毁,并且地址相同。
(2)多例:prototype,从工厂中创建不同的对象

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  4. <bean id="IntrduceDemo" class="test.TestBean" init-method="init" destroy-method="destroy" scope="prototype">
  5. <property name="name" value="魏澳"/>
  6. <property name="age" value="20"/>
  7. </bean>
  8. </beans>

在这里插入图片描述
还是上个代码块,但是有了两个创建了,没有销毁,所以说销毁是针对于单例模式的(和上文呼应)。

  1. 今天的内容主要是讲的是一些入门的基本知识,下一次就接着往后写续集了,我也是新手一枚,如果有不对或者不懂的地方可以一起讨论!!

发表评论

表情:
评论列表 (有 0 条评论,303人围观)

还没有评论,来说两句吧...

相关阅读