Spring从入门到入土——快速上手Spring

   日期:2020-05-05     浏览:144    评论:0    
核心提示:HelloSpring导入依赖​ 我们要想使用Spring,首先肯定要先导入其jar包,我们只需要在java

快速入门

      • HelloSpring
        • 编写代码
          • 编写一个Hello实体类
          • 编写Spring文件,命名为beans.xml
          • 测试
          • 结果以及总结
      • IOC创建对象方式
        • 通过无参构造方法来创建
          • User.java
          • beans.xml
          • 测试
          • 结果
        • 通过有参构造方法来创建
          • UserT.java
          • beans.xml 有三种方式
          • 测试
          • 总结
      • Spring的配置
        • 别名
        • Bean的配置
        • import

相关文章推荐:

跟着官网学spring—快速入门指南

跟着官网学Spring—构建RESTful Web服务

Spring从入门到入土——概述以及IOC理论推导

Spring从入门到入土——快速上手Spring

HelloSpring

​ 我们要想使用Spring,首先肯定要先导入其jar包,我们只需要在maven配置文件中加入响应的依赖,就会自动下载相应的依赖项,

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.0.RELEASE</version>
</dependency>

编写代码

编写一个Hello实体类
public class Hello {
    public String str;
   
    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        System.out.println("set");
        this.str = str;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }
}
编写Spring文件,命名为beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--使用spring来创建对象-->
    <bean id="hello" class="com.zhonghu.pojo.Hello">
        <property name="str" value="Spring"/>
    </bean>
</beans>
测试
@Test
public void test(){
   //解析beans.xml文件 , 生成管理相应的Bean对象
   ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
   //getBean : 参数即为spring配置文件中bean的id .
   Hello hello = (Hello) context.getBean("hello");
   System.out.println(hello.toString());
}
结果以及总结
  • 结果

  • 总结
    • 在结果中我们可以很清晰的看到,其调用了set方法,也就是说set方法是实现Ioc的基础。
    • Hello对象是谁创建的?——是Spring创建的
    • Hello对象的属性是怎么设置的?——hello对象的属性是由Spring容器实现的。
  • 这个过程就是控制反转:
    • 控制:谁来控制对象的创建,传统应用程序是程序员来控制创建的,使用spring后,对象是由spring来创建的
    • 反转:程序本身不创建对象,而是变为被动接受对象
  • 依赖注入:就是利用set方法来进行注入的

IOC创建对象方式

通过无参构造方法来创建

User.java
public class User {

   private String name;

   public User() {
       System.out.println("user无参构造方法");
  }

   public void setName(String name) {
       this.name = name;
  }

   public void show(){
       System.out.println("name="+ name );
  }

}
beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.zhonghu.pojo.User">
        <property name="name" value="kuangshen"/>
    </bean>

</beans>
测试
@Test
public void test(){
   ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
   //在执行getBean的时候, user已经创建好了 , 通过无参构造
   User user = (User) context.getBean("user");
   //调用对象的方法 .
   user.show();
}
结果

​ 可以发现,在调用show方法前,User对象就已经通过无参构造初始化了

通过有参构造方法来创建

UserT.java
public class UserT {

   private String name;

   public UserT(String name) {
       this.name = name;
  }

   public void setName(String name) {
       this.name = name;
  }

   public void show(){
       System.out.println("name="+ name );
  }

}
beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 第一种根据参数名字来设置-->
    <bean id="userT" class="com.zhonghu.pojo.UserT">
<!-- name 指参数名-->
        <constructor-arg name="name"value="zhonghu"/>
        
    </bean>
    
    
<!-- 第二种根骨index参数下标来设置-->
    <bean id="userT" class="com.zhonghu.pojo.UserT">
<!-- index值构造方法,下标从0开始 -->
        <constructor-arg index="0" value="zhonghu"/>
    </bean>

    
    
<!-- 第三种 根据参数类型设置 十分不推荐-->
    <bean id="useT"class="com.zhonghu.pojo.UserT">
        <constructor-arg type="java.lang.String" value="zhonghu"/>
    </bean>
</beans>
测试
@Test
public void testT(){
   ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
   UserT user = (UserT) context.getBean("userT");
   user.show();
}
总结

​ 在配置文件加载的时候。其中管理的对象都已经初始化了

Spring的配置

别名

alias 为bean设置别名,可以同时设置多个别名

<!--设置别名:在获取Bean的时候可以使用别名获取-->
<alias name="userT" alias="userNew"/>

Bean的配置

<!--bean就是java对象,由Spring创建和管理-->

<!-- id 是bean的标识符,要唯一,如果没有配置id,name就是默认标识符 如果配置id,又配置了name,那么name是别名 name可以设置多个别名,可以用逗号,分号,空格隔开 如果不配置id和name,可以根据applicationContext.getBean(.class)获取对象; class是bean的全限定名=包名+类名 -->
<bean id="hello" name="hello2 h2,h3;h4" class="com.kuang.pojo.Hello">
   <property name="name" value="Spring"/>
</bean>

import

一般用于团队开发,可以将多个配置文件导入合并为一个。

<import resource="{path}/beans.xml"/>
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
更多>相关资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服