关于java:三Spring从入门到入土快速上手Spring与IOC创建对象方式

23次阅读

共计 5175 个字符,预计需要花费 13 分钟才能阅读完成。

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 的配置元数据将这些 bean 配置为 <bean/>
  • 基于 Java 的配置:应用 @Bean 在 @Configuration 类中应用带注解的办法。

这里介绍 是基于注解的配置

<?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">
    <!--
       value: 具体的值
       ref:援用 Spring 容器中曾经创立好的对象。-->
        <property name="str" value="Spring"/>
    </bean>
</beans>

其中这里的 id 是标识单个 bean 定义的字符串;class 是定义 Bean 的类型并应用齐全限定的类名。

实例化容器

实例化容器能够通过 Spring 提供的 ApplicationContext 构造函数。其通过一个或多个参数来从各种内部资源中加载配置元数据 Classpath。

@Test
public void test(){
   // 解析 beans.xml 文件 , 生成治理相应的 Bean 对象
   ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
   //getBean : 参数即为 spring 配置文件中 bean 的 id .
   Hello hello = (Hello) context.getBean("hello");
   // Hello hello = context.getBean("hello",Hello.class);
   System.out.println(hello.toString());
}
后果以及总结
  • 后果

  • 总结
    • 在后果中咱们能够很清晰的看到,其调用了 set 办法,也就是说 set 办法是实现 Ioc 的根底。
    • Hello 对象是谁创立的?——是 Spring 创立的
    • Hello 对象的属性是怎么设置的?——hello 对象的属性是由 Spring 容器实现的。
  • 这个过程就是管制反转:
    • 管制:谁来管制对象的创立,传统应用程序是程序员来管制创立的,应用 spring 后,对象是由 spring 来创立的
    • 反转:程序自身不创建对象,而是变为被动承受对象
  • 依赖注入:就是利用 set 办法来进行注入的

用这种办法后,咱们就不须要再程序中进行批改,要实现不同的操作,只须要在 xml 配置文件中进行批改即可。

所谓的 IOC 就是:对象由 Spring 来创立、治理、拆卸

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 对象就曾经通过无参结构初始化了。容器在创立的时候就曾经创建对象了。

Spring 通过无参结构来创建对象,而后用 set 办法进行注入。

其 name 属性是类中的成员属性

通过有参构造方法来创立

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();}
总结

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

其 name 属性外面对应有参结构的形参名称

Spring 的 Bean 在什么时候被实例化

应用 BeanFactory 作为工厂类

所有的 Bean 都是在第一次应用该 Bean 的时候初始化(懒加载)

应用 ApplicationContext 作为工厂类
  • 如果 bean 的 scope 是 singleton 的,并且 lazy-init 为 false(默认是 false,所以能够不必设置),则 ApplicationContext 启动的时候就实例化该 Bean,并且将实例化的 Bean 放在一个 map 构造的缓存中,下次再应用该 Bean 的时候,间接从这个缓存中取
  • 如果 bean 的 scope 是 singleton 的,并且 lazy-init 为 true,则该 Bean 的实例化是在第一次应用该 Bean 的时候进行实例化
  • 如果 bean 的 scope 是 prototype 的,则该 Bean 的实例化是在第一次应用该 Bean 的时候进行实例化

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.zhonghu.pojo.Hello">
   <property name="name" value="Spring"/>
</bean>

import

个别用于团队开发,能够将多个配置文件导入合并为一个。

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

总结

  • IOC 创建对象的形式
    • 无参结构:其 name 属性是类中的成员属性
    • 有参结构:其 name 属性外面对应有参结构的形参名称
  • Spring Bean 在什么时候初始化
    • 应用 BeanFactory 作为工厂类
      • 所有的 Bean 都是在第一次应用该 Bean 的时候初始化(懒加载)
    • 应用 ApplicationContext 作为工厂类
      • 如果 bean 的 scope 是 singleton 的,并且 lazy-init 为 false(默认是 false,所以能够不必设置),则 ApplicationContext 启动的时候就实例化该 Bean,并且将实例化的 Bean 放在一个 map 构造的缓存中,下次再应用该 Bean 的时候,间接从这个缓存中取
      • 如果 bean 的 scope 是 singleton 的,并且 lazy-init 为 true,则该 Bean 的实例化是在第一次应用该 Bean 的时候进行实例化
      • 如果 bean 的 scope 是 prototype 的,则该 Bean 的实例化是在第一次应用该 Bean 的时候进行实例化

最初

  • 如果感觉看完有播种,心愿能给我点个赞,这将会是我更新的最大能源,感激各位的反对
  • 欢送各位关注我的公众号【java 冢狐】,专一于 java 和计算机基础知识,保障让你看完有所播种,不信你打我
  • 如果看完有不同的意见或者倡议,欢送多多评论一起交换。感激各位的反对以及厚爱。

欢送关注公众号“Java 冢狐”,获取最新消息

正文完
 0