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

快速入门

      • 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

Spring从入门到入土——依赖注入(DI)

Spring从入门到入土——Bean的作用域

Spring从入门到入土——自动装配

Spring从入门到入土——使用注解

Spring从入门到入土——AOP就这么简单

Spring从入门到入土——代理模式

HelloSpring

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


<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-webmvcartifactId>
    <version>5.2.0.RELEASEversion>
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="zhonghu"/>
    </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 有三种方式

<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">

        
        
    bean>
    
    

    <bean id="userT" class="com.zhonghu.pojo.UserT">

        <constructor-arg index="0" value="zhonghu"/>
    bean>

    
    

    
        <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设置别名,可以同时设置多个别名


<alias name="userT" alias="userNew"/>

Bean的配置




<bean id="hello" name="hello2 h2,h3;h4" class="com.zhonghu.pojo.Hello">
   <property name="name" value="Spring"/>
bean>

import

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

<import resource="{path}/beans.xml"/>

你可能感兴趣的:(所有文章,Java,spring)