0
点赞
收藏
分享

微信扫一扫

初始Spring(贰)

juneyale 2022-05-03 阅读 40

目录

3,HelloSpring

4,IOC创建对象的方式 

5,Spring配置 

5.1,别名

5.2,Bean的配置

5.3,import

6,依赖注入 

6.1,构造器注入

6.2,set方法注入[重点]


3,HelloSpring

创建Hello类:

package com.xiao.domain;

public class Hello {
    private String str;

    public String getStr() {
        return str;
    }

    public Hello() {
    }

    public void setStr(String str) {
        this.str = str;
    }

    public Hello(String str) {
        this.str = str;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }
}

 配置bean:

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 使用spring来创建对象,在Spring中这都称为Bean
        类型 变量名=new 类型();
        Hello hello=new Hello();
        bean=对象   new Hello();

        id=变量名
        class=new的对象
        property=给对象中的属性设置一个值
     -->
    <bean id="hello" class="com.xiao.domain.Hello">
        <property name="str" value="Spring"/>
    </bean>

</beans>

测试:

import com.xiao.domain.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        //获取Spring的上下文对象!
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //我们的对象都在Spring中管理了我们要使用对象之间取出来就可以了
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());
    }
}

结果:

Hello{str='Spring'}

思考问题?

  • Hello对象是谁创建的?

        Hello对象是由Spring创建的

  • Hello对象的属性是怎么设置的? 

        Hello对象的属性是由Spring设置的

这个过程就叫做控制反转;

4,IOC创建对象的方式 

  1. 使用无参构造创建对象,默认!
  2. 假设我们需要使用有参构造创建对象:
    1. 下标赋值:                                                                                                                          
          <bean id="user" class="com.xiao.domain.User">
              <!-- index:传入构造器参数的下标 value:传入的值 -->
              <constructor-arg index="0" value="小步"/>
          </bean>
    2. 通过类型匹配                                                                                                                    
          <!-- 类型匹配(不建议使用) -->
          <bean id="user" class="com.xiao.domain.User">
              <!-- type:构造器传入值的类型 value:传入的值 -->
              <constructor-arg type="java.lang.String" value="小步"/>
          </bean>
    3. 通过参数名匹配                                                                                                                 
          <!-- 参数名匹配 -->
          <bean id="user" class="com.xiao.domain.User">
              <!-- type:构造器传入值的类型 value:传入的值 -->
              <constructor-arg name="name" value="小步"/>
          </bean>

总结:当ClassPathXmlApplicationContext();加载xml文件时会创建其中全部配置的对象!

扩展:Spring底层实现是采用了一个Map进行键值对的存储的!

 

5,Spring配置 

5.1,别名

    <!-- 别名:如果添加了别名我们可以使用别名去获取对象 -->
    <alias name="user" alias="newUser"/>

5.2,Bean的配置

    <!--
     id:唯一的标识符,代表我们的变量名
     class: bean对于的类必须包名+类名
     name: 起的别名并且可以起多个可以用空格逗号等分割
     -->
    <bean id="testUser" class="com.xiao.domain.TestUser" name="test test1">
        <property name="name" value="小步"/>
    </bean>

5.3,import

 这个import一般用于团队开发使用,他可以将多个配置文件合并为一个。

假设现在有多人开发,这三个人复制不同的类开发,不同的类需要注册在不同的bean中,我们可以利用import将所有人的beans.xml合并为一个,使用的时候之间使用总配置就可以了!

  • applicationContext.xml
    <import resource="beans1.xml"/>
    <import resource="beans2.xml"/>
    <import resource="beans3.xml"/>

6,依赖注入 

6.1,构造器注入

前面已经说过了于4.ioc创建方式!

6.2,set方法注入[重点]

[环境搭建]

复杂类型:

package com.xiao.domain;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

测试真实对象

package com.xiao.domain;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }
}

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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="student" class="com.xiao.domain.Student">
        <!-- 第一种普通值注入 -->
        <property name="name" value="小步"/>
    </bean>

</beans>

测试类:

import com.xiao.domain.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) applicationContext.getBean("student");
        System.out.println(student.getName());
    }
}
举报

相关推荐

0 条评论