3、Spring之入门案例

莱莱  金牌会员 | 2023-7-27 02:30:03 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 910|帖子 910|积分 2730

3.1、创建module

3.1.1、右击project,创建新module


3.1.2、选择maven


3.1.3、设置module名称和路径



3.1.4、module初始状态


3.1.5、配置打包方式和依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <beans xmlns="http://www.springframework.org/schema/beans"
  5.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  7.    
  8.     <bean id="helloWord" ></bean>
  9. </beans> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  14.    
  15.     <bean id="helloWord" ></bean>
  16. </beans> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  17.     <modelVersion>4.0.0</modelVersion>
  18.     <groupId>org.rain</groupId>
  19.     <artifactId>spring_helloword</artifactId>
  20.     <version>1.0-SNAPSHOT</version>
  21.     <packaging>jar</packaging>
  22.     <dependencies>
  23. <?xml version="1.0" encoding="UTF-8"?>
  24. <beans xmlns="http://www.springframework.org/schema/beans"
  25.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  26.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  27.    
  28.     <bean id="helloWord" ></bean>
  29. </beans>
  30. <?xml version="1.0" encoding="UTF-8"?>
  31. <beans xmlns="http://www.springframework.org/schema/beans"
  32.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  33.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  34.    
  35.     <bean id="helloWord" ></bean>
  36. </beans><dependency>
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  41.    
  42.     <bean id="helloWord" ></bean>
  43. </beans>    <groupId>org.springframework</groupId>
  44. <?xml version="1.0" encoding="UTF-8"?>
  45. <beans xmlns="http://www.springframework.org/schema/beans"
  46.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  47.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  48.    
  49.     <bean id="helloWord" ></bean>
  50. </beans>    <artifactId>spring-context</artifactId>
  51. <?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.    
  56.     <bean id="helloWord" ></bean>
  57. </beans>    <version>5.3.1</version>
  58. <?xml version="1.0" encoding="UTF-8"?>
  59. <beans xmlns="http://www.springframework.org/schema/beans"
  60.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  61.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  62.    
  63.     <bean id="helloWord" ></bean>
  64. </beans></dependency>
  65. <?xml version="1.0" encoding="UTF-8"?>
  66. <beans xmlns="http://www.springframework.org/schema/beans"
  67.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  68.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  69.    
  70.     <bean id="helloWord" ></bean>
  71. </beans>
  72. <?xml version="1.0" encoding="UTF-8"?>
  73. <beans xmlns="http://www.springframework.org/schema/beans"
  74.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  75.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  76.    
  77.     <bean id="helloWord" ></bean>
  78. </beans><dependency>
  79. <?xml version="1.0" encoding="UTF-8"?>
  80. <beans xmlns="http://www.springframework.org/schema/beans"
  81.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  82.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  83.    
  84.     <bean id="helloWord" ></bean>
  85. </beans>    <groupId>junit</groupId>
  86. <?xml version="1.0" encoding="UTF-8"?>
  87. <beans xmlns="http://www.springframework.org/schema/beans"
  88.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  89.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  90.    
  91.     <bean id="helloWord" ></bean>
  92. </beans>    <artifactId>junit</artifactId>
  93. <?xml version="1.0" encoding="UTF-8"?>
  94. <beans xmlns="http://www.springframework.org/schema/beans"
  95.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  96.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  97.    
  98.     <bean id="helloWord" ></bean>
  99. </beans>    <version>4.12</version>
  100. <?xml version="1.0" encoding="UTF-8"?>
  101. <beans xmlns="http://www.springframework.org/schema/beans"
  102.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  103.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  104.    
  105.     <bean id="helloWord" ></bean>
  106. </beans>    <scope>test</scope>
  107. <?xml version="1.0" encoding="UTF-8"?>
  108. <beans xmlns="http://www.springframework.org/schema/beans"
  109.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  110.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  111.    
  112.     <bean id="helloWord" ></bean>
  113. </beans></dependency>
  114.     </dependencies>
  115. </project>
复制代码
3.2、示例

3.2.1、创建组件类

  1. package org.rain.spring.component;
  2. /**
  3. * @author liaojy
  4. * @date 2023/7/25 - 19:17
  5. */
  6. public class HelloWord {
  7.     public void sayHello(){
  8. <?xml version="1.0" encoding="UTF-8"?>
  9. <beans xmlns="http://www.springframework.org/schema/beans"
  10.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  11.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  12.    
  13.     <bean id="helloWord" ></bean>
  14. </beans>System.out.println("Hello,Spring!");
  15.     }
  16. }
复制代码
3.2.2、创建Sprig配置文件


++++++++++++++++++++++++++++++++++++分割线++++++++++++++++++++++++++++++++++++

注意:因为会通过自定义代码指定Spring配置文件,所以Spring配置文件名可以是任意的;
但当整合ssm后,就不能通过通过自定义代码指定Spring配置文件,因此文件名有硬性要求。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.    
  6.     <bean id="helloWord" ></bean>
  7. </beans>
复制代码
3.2.3、创建测试类


如图所示,获取到了IOC容器和容器中对应的bean组件,并成功调用了该bean组件的方法
  1. package org.rain.spring.test;import org.junit.Test;import org.rain.spring.component.HelloWord;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @author liaojy * @date 2023/7/25 - 19:39 */public class HelloWordTest {    @Test    public void testHelloWord(){<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.    
  6.     <bean id="helloWord" ></bean>
  7. </beans>//获取IOC容器<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.    
  12.     <bean id="helloWord" ></bean>
  13. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.    
  18.     <bean id="helloWord" ></bean>
  19. </beans>//获取IOC容器中的bean<?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  23.    
  24.     <bean id="helloWord" ></bean>
  25. </beans>HelloWord helloWord = (HelloWord)applicationContext.getBean("helloWord");<?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  29.    
  30.     <bean id="helloWord" ></bean>
  31. </beans>helloWord.sayHello();    }}
复制代码
3.3、获取bean的三种方式

3.3.1、根据id获取

由于 id 属性是 bean 的唯一标识,所以根据 bean 标签的 id 属性可以精确获取到一个组件对象;但也存在类型转换问题,具体见上一小节。
3.3.2、根据类型获取(最常用)


如图所示,根据类型获取bean,则不存在类型转换问题
  1.     @Test    public void testHelloWord(){<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.    
  6.     <bean id="helloWord" ></bean>
  7. </beans>//获取IOC容器<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.    
  12.     <bean id="helloWord" ></bean>
  13. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.    
  18.     <bean id="helloWord" ></bean>
  19. </beans>//获取IOC容器中的bean<?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  23.    
  24.     <bean id="helloWord" ></bean>
  25. </beans>HelloWord helloWord = applicationContext.getBean(HelloWord.class);<?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  29.    
  30.     <bean id="helloWord" ></bean>
  31. </beans>helloWord.sayHello();    }
复制代码
注意:如下图所示,当根据类型获取bean时,要求IOC容器中指定类型的bean有且只能有一个;否则会报错
  1. org.springframework.beans.factory.NoUniqueBeanDefinitionException:
  2. No qualifying bean of type 'org.rain.spring.component.HelloWord' available:
  3. expected single matching bean but found 2: helloWord,helloWordtwo
复制代码

+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++

3.3.3、根据id和类型获取

  1.     @Test    public void testHelloWord(){<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.    
  6.     <bean id="helloWord" ></bean>
  7. </beans>//获取IOC容器<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.    
  12.     <bean id="helloWord" ></bean>
  13. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.    
  18.     <bean id="helloWord" ></bean>
  19. </beans>//获取IOC容器中的bean<?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  23.    
  24.     <bean id="helloWord" ></bean>
  25. </beans>HelloWord helloWord = applicationContext.getBean("helloWord",HelloWord.class);<?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  29.    
  30.     <bean id="helloWord" ></bean>
  31. </beans>helloWord.sayHello();    }
复制代码
3.3.4、重要扩展

如果组件类实现了接口,则根据接口类型可以获取 bean,前提是IOC容器中实现该接口的组件类型的bean有且只能有一个

+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++

+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++

+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++
  1.     @Test    public void testHelloWord(){<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.    
  6.     <bean id="helloWord" ></bean>
  7. </beans>//获取IOC容器<?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  11.    
  12.     <bean id="helloWord" ></bean>
  13. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.    
  18.     <bean id="helloWord" ></bean>
  19. </beans>//根据接口类型获取IOC容器中的bean<?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  23.    
  24.     <bean id="helloWord" ></bean>
  25. </beans>HelloWord helloWord = (HelloWord) applicationContext.getBean(Hello.class);<?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  29.    
  30.     <bean id="helloWord" ></bean>
  31. </beans>helloWord.sayHello();    }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

莱莱

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表