Spring配置

5 Spring配置

  • 别名
  • Bean配置

Spring配置_第1张图片

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

一般用于团队开发,导入合并为一个,项目中多人开发,不同类需要注册在不同在bean中,可以利用import将不同的bean合并为总的,使用时直接使用总的。

6 依赖注入

6.1 构造器注入

6.2 Set方式注入

  • 依赖:bean对象的创建依赖于容器
  • 注入:bean对象中所有的属性,由容器来注入!

【环境搭建】

  • 复杂类型
package com.kuang.pojo;

/**
 * @author shkstart
 * @create 2020-09-15 14:49
 */
public class Address {
     

    private  Address address;

    public Address getAddress() {
     
        return address;
    }

    public void setAddress(Address address) {
     
        this.address = address;
    }
}
  • 真实情况创建
package com.kuang.pojo;

import java.util.*;

/**
 * @author shkstart
 * @create 2020-09-15 14:49
 */
public class Student {
     
    private  String name;
    private  Address address;
    private  String[] books;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private  String wife;
    private Properties info;



    public String getName() {
     
        return name;
    }

    public Address getAddress() {
     
        return address;
    }

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

    public List<String> getHobbies() {
     
        return hobbies;
    }

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

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

    public String getWife() {
     
        return wife;
    }

    public Properties getInfo() {
     
        return info;
    }

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

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

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

    public void setHobbies(List<String> hobbies) {
     
        this.hobbies = hobbies;
    }

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

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

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

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

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

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

    <bean id="student" class="com.kuang.pojo.Student">
        <property name="name" value="liutao"/>

    bean>

beans>
  • 测试类

import com.kuang.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author shkstart
 * @create 2020-09-15 15:00
 */
public class MyTest {
     

    public static void main(String[] args) {
     
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student st = (Student) context.getBean("student");
        System.out.println(st.getAddress());


    }
}

完善注入信息


<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="address" class="com.kuang.pojo.Address">
    bean>

    <bean id="student" class="com.kuang.pojo.Student">
        
        <property name="name" value="liutao"/>
        
        <property name="address" ref="address"/>
        
        <property name="books">
            <array>
                <value>红楼梦value>
                <value>水浒传value>
                <value>三国演义value>
                <value>红楼梦value>
            array>
        property>

        

        <property name="hobbies">
            <list>
                <value>听歌value>
                <value>看电影value>
                <value>写代码value>
            list>
        property>

        
        <property name="card">
            <map>
                <entry key="身份证" value="111112222"/>
                <entry key="银行卡" value="23541455254"/>
            map>
        property>
        
        <property name="games">
            <set>
                <value>LOLvalue>
                <value>COCvalue>
                <value>ABCvalue>
            set>
        property>

        <property name="wife">
            <null>null>
        property>

        <property name="info">
            <props>
                <prop key="学号">2019020202prop>
                <prop key="性别">prop>
                <prop key="姓名">刘涛prop>
            props>
        property>

    bean>

beans>

6.3 扩展方式注入

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.kuang.pojo.User"
          p:name="liutao" p:age="12"/>
    <bean id="user2" class="com.kuang.pojo.User"
          c:age="14" c:name="liudeyang">
    bean>

beans>

test:

 @Test
    public void myTest(){
     
        ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
        User user = (User) context.getBean("user");
        System.out.println(user.toString());
    }

注意点:

  • p命名和c命名不能直接导入,需要xml约束,具体见官网:https://docs.spring.io/spring-framework/docs/5.1.17.RELEASE/spring-framework-reference/core.html#beans-p-namespace

6.4 bean的作用域

1.单例模式(默认)
2.原型模式
每次从容器中get时都会产生新的对象
3.其余的request,session,application 在web中开发

你可能感兴趣的:(Spring)