Spring-IoC-DI

(SPRING 中文网):https://springdoc.cn/inversion-control-and-dependency-injection-in-spring/
Spring 中的控制反转(IoC)和依赖注入(DI)

  • 控制反转(Inversion of Control)是软件工程中的一项原则,它将对象或程序部分的控制权转移到容器或框架中。
  • 依赖注入是可以用来实现 IoC 的一种模式,其中被反转的控制是设置对象的依赖。
    容器将对象与其他对象 “装配” 起来,或将对象 “注入” 到其他对象中。

依赖


<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-contextartifactId>
    <version>5.3.18version>
dependency>

<dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>4.13.1version>
dependency>

实体类

public class User {
    private String id;
    private String username;
    private String password;
}
public class Student {
    private String[] string;
    private Object object;
    private List<String> list;
    private Map<String,String> map;
    private Properties properties;
}

配置文件


<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 
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <import resource="studentBeans.xml"/>
    
    
    <bean id="user1" class="org.example.pojo.User" name="user1-1;user1-2">
        
        <property name="id" value="-1"/>
        <property name="password" value="1234"/>
        <property name="username" value="zhangsan"/>
    bean>
    
    <bean id="user2" class="org.example.pojo.User">
        <constructor-arg name="id" value=""/>
        <constructor-arg name="password" value=""/>
        <constructor-arg name="username" value=""/>
    bean>
    
    <alias name="user1" alias="user1-3"/>
    
    
    
    <bean id="userP" class="org.example.pojo.User" p:username="zhangsanp" p:password="123p" scope="singleton"/>
    
    <bean id="userC" class="org.example.pojo.User" c:id="1" c:username="zhangsanc" c:password="123c"/>
    
    <bean id="user3" class="org.example.pojo.Student">
        
        <property name="object" ref="user1"/>
        
        <property name="string">
            <array>
                <value>str1value>
                <value>str2value>
            array>
        property>
        
        <property name="map">
            <map>
                <entry key="k1" value="v1"/>
                <entry key="k2" value="v2"/>
            map>
        property>
        
        <property name="list">
            <null/>
        property>
        
        <property name="properties">
            <props>
                <prop key="username">rootprop>
                <prop key="password">1234prop>
            props>
        property>
    bean>
    
    
       
    <bean class="org.example.pojo.User" autowire="byName"/>
beans>

测试类

public class UserTest{
    @Test
    public void getUser(){
        // 传统方式创建对象
        // User user = new User();
        // IOC:由spring框架自动实例化
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("userBeans.xml");
        User userC = (User) beanFactory.getBean("userC");
    }
}

你可能感兴趣的:(spring)