Spring框架入门操作

迷南。 2022-04-15 04:20 398阅读 0赞

学了几个框架之后,学习Spring框架的时候就很简单了,这篇博客先说一下spring框架的操作步骤,他的IOC、DI、AOP在以后的文章中再分享。

如何创建一个spring工程,请大家耐心操作。

1.导入jar包

在一个maven工程中的pom.xml文件中导入

  1. spring-core spring-beans spring-context commons-logging
  2. <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
  3. <dependency>
  4. <groupId>org.springframework</groupId>
  5. <artifactId>spring-core</artifactId>
  6. <version>5.0.8.RELEASE</version>
  7. </dependency>
  8. <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
  9. <dependency>
  10. <groupId>org.springframework</groupId>
  11. <artifactId>spring-beans</artifactId>
  12. <version>5.0.8.RELEASE</version>
  13. </dependency>
  14. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-context</artifactId>
  18. <version>5.0.8.RELEASE</version>
  19. </dependency>
  20. <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
  21. <dependency>
  22. <groupId>commons-logging</groupId>
  23. <artifactId>commons-logging</artifactId>
  24. <version>1.2</version>
  25. </dependency>

2.创建spring的配置文件

在resources包下创建.xml文件

文件头如下

  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. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:mc="http://www.springframework.org/schema/mvc"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/mvc
  12. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  13. </beans>

3.使用spring容器获取对象

创建一个实体类

  1. package com.jie;
  2. public class HelloWorld {
  3. private String name;
  4. private int age;
  5. private String address;
  6. public void sayHello(){
  7. System.out.println("Hello World!!!!");
  8. }
  9. public HelloWorld(String name, int age) {
  10. this.name = name;
  11. this.age = age;
  12. }
  13. public HelloWorld(String name, int age,String address) {
  14. this.name = name;
  15. this.age = age;
  16. this.address=address;
  17. }
  18. public void setAddress(String address) {
  19. this.address = address;
  20. }
  21. public String getAddress() {
  22. return address;
  23. }
  24. public void setName(String name) {
  25. this.name = name;
  26. }
  27. public String getName() {
  28. return name;
  29. }
  30. public void setAge(int age) {
  31. this.age = age;
  32. }
  33. public int getAge() {
  34. return age;
  35. }
  36. public HelloWorld() {
  37. }
  38. }

在.xml文件中配置

  1. <!--
  2. id 当前类的名字 唯一的 不可重复
  3. class 类的全限定名称
  4. scorp 默认:singleton(单例模式) prototype多例模式 request session
  5. -->
  6. <bean class="com.jie.HelloWorld" name="helloWorld2 world hello" id="helloWorld" scope="prototype"></bean>
  7. <bean id="hw" class="com.jie.HelloWorld" scope="prototype">
  8. <!--
  9. property 给指定属性赋值
  10. 第一种方法:通过setter方法注入
  11. DI依赖注入
  12. -->
  13. <!--<property name="name" value="浪子一秋"></property>-->
  14. <!--<property name="age" value="21"></property>-->
  15. <!--
  16. 第二种方法:通过有参 !前提是要有有参构造函数
  17. index 通过参数的索引注入
  18. type 通过参数的类型注入
  19. -->
  20. <!--<constructor-arg index="0" value="一叶知秋"></constructor-arg>-->
  21. <!--<constructor-arg index="1" value="22"></constructor-arg>-->
  22. <!--经过测试,这个是按照类型顺序赋值-->
  23. <constructor-arg type="int" value="111"></constructor-arg>
  24. <constructor-arg type="java.lang.String" value="啧啧啧"></constructor-arg>
  25. <constructor-arg type="java.lang.String" value="郑州"></constructor-arg>
  26. </bean>

上面的两个bean配置的是同一个类,是为了后年的测试,里面的测试不再单独写了,不明白的再问

测试类

/**
* Spring IOC 控制反转 控制权的反向转移
* 原来:谁用水创建 new 类名();
* 现在:提前创建好所需要的对象,当使用时,不是自己创建,而是从容器中获取对象
*
* 使用步骤:
* 1.导入jar包 spring-core spring-beans spring-context commons-logging
* 2.创建spring的配置文件
* 3.使用spring容器获取对象
*
* 类 :单例模式 多例模式
* 默认情况下是单例模式
* 在配置文件中可以设置为其他模式
*
* DI依赖注入
* 原始:通过有参构造直接给属性赋值 或者是通过无参+set方法赋值
* 现在:spring容器中
*/

  1. package com.jie;
  2. import org.junit.Test;
  3. import org.springframework.beans.factory.BeanFactory;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class TestHello {
  6. @Test
  7. public void test1(){
  8. //谁使用,谁创建
  9. HelloWorld world=new HelloWorld();
  10. HelloWorld world1=new HelloWorld();
  11. System.out.println(world);//com.jie.HelloWorld@2077d4de
  12. System.out.println(world1);//com.jie.HelloWorld@7591083d
  13. //不同对象
  14. world.sayHello();
  15. }
  16. @Test
  17. public void test2(){
  18. //spring 从spring容器中获取对象
  19. //读取spring配置文件,创建bean工厂
  20. BeanFactory factory=new ClassPathXmlApplicationContext("/applicationContext.xml");
  21. //从bean工厂中获取bean对象
  22. //通过id查找
  23. HelloWorld world=(HelloWorld)factory.getBean("helloWorld");
  24. HelloWorld world1=(HelloWorld)factory.getBean("helloWorld");
  25. //通过类型得到对象
  26. //HelloWorld world2=factory.getBean(HelloWorld.class);
  27. //通过name查找
  28. HelloWorld world3=(HelloWorld)factory.getBean("helloWorld2");
  29. //通过id+类查找(官方推荐)
  30. HelloWorld world4=factory.getBean("hw",HelloWorld.class);
  31. System.out.println(world);//com.jie.HelloWorld@5a1c0542
  32. System.out.println(world1);//com.jie.HelloWorld@5a1c0542
  33. //同一个对象 推断为单例模式
  34. world.sayHello();
  35. }
  36. }

发表评论

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

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

相关阅读

    相关 Spring 框架——spring入门教程

    打个广告,帮朋友卖点东西,东西超便宜的哟【衣服鞋子等】,厂家直接出货,绝对低于市场价!!! 一般都比市场价便宜3—7折【都是牌子货】,如果您感兴趣,可以扫描屏幕下方的二维码,感

    相关 Spring框架入门操作

    > 学了几个框架之后,学习Spring框架的时候就很简单了,这篇博客先说一下spring框架的操作步骤,他的IOC、DI、AOP在以后的文章中再分享。 > > 如何创建一个s

    相关 Spring框架入门

    1.1专业术语了解 组件/框架设计 侵入式设计 引入了框架,对现有的类的结构有影响;即需要实现或继承某些特定类。 例如: Struts框架 继承ActionSupp...