先导,对IOC容器的理解

先导,对IOC容器的理解

  • 通俗的讲就是把你的class类交给spring的IOC容器去管理
  • 需要对该类的属性注入一些值,就可以通过spring提供的xml文件或者注解进行注入
  • 自己使用时在IOC容器工厂中去获取就可以了,从而实现控制

1、导入maven依赖 5.3.15版本

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>pro01-maven-ider-parent</artifactId>
        <groupId>com.mhy.maven</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>pro06-module-spring</artifactId>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.15</version>
        </dependency>

    </dependencies>

</project>

2、xml配置文件的常用bean注入

  • 实体类
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 Properties info;
    private String wife;
}
  • bean.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="address" class="com.mhy.pojo.Address">
        <property name="address" value="重庆交通大学"/>
    </bean>
    <bean id="student" class="com.mhy.pojo.Student">
        
        <property name="name" value="mhy"/>
        <property name="address" ref="address"/>
        <property name="books">
            <array>
                <value>西游记</value>
                <value>红楼梦</value>
                <value>水浒传</value>
                <value>三国演义</value>
            </array>
        </property>
        <property name="hobbys">
            <list>
                <value>数学</value>
                <value>代码</value>
            </list>
        </property>
        <property name="games">
            <set>
                <value>LOL</value>
                <value>CF</value>
                <value>DMF</value>
            </set>
        </property>
        <property name="card">
            <map>
                <entry key="身份证" value="5002372222287878787"/>
                <entry key="学生卡" value="631910040417"/>
            </map>
        </property>
        <property name="info">
            <props>
                <prop key="driver">com.mysql.jdbc.Driver</prop>
                <prop key="url">url=jdbc:mysql://localhost:3306/smbms?characterEncoding=utf8</prop>
                <prop key="username">root</prop>
                <prop key="password">123456</prop>
            </props>
        </property>
        <property name="wife">
            <null/>
        </property>

    </bean>

</beans>
  • 测试的代码
    @Test
    public void testSpring2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student);
        /*结果--->
        Student{name='mhy',
        address=Address{address='重庆交通大学'},
        books=[西游记, 红楼梦, 水浒传, 三国演义],
        hobbys=[数学, 代码],
        card={身份证=5002372222287878787, 学生卡=631910040417},
        games=[LOL, CF, DMF],
        info={
            password=123456,
            url=url=jdbc:mysql://localhost:3306/smbms?characterEncoding=utf8,
            driver=com.mysql.jdbc.Driver,
            username=root},
        wife='null'}
         */
    }
  • p和c标签的使用
<?xml version="1.0" encoding="UTF-8"?>
<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="hello" class="com.mhy.pojo.Hello">
        <property name="str" value="hello spring"/>
    </bean>
    <bean id="hello4" class="com.mhy.pojo.Hello">
        <property name="str" value="hello spring 2"/>
    </bean>
    <bean id="hello2" class="com.mhy.pojo.Hello" p:str="hello spring p"/>
    <bean id="hello3" class="com.mhy.pojo.Hello" c:str="hello spring c"/>

</beans>

注意:

  1. c标签是set注入
  2. p标签是有参数构造注入
posted @ 2022-03-25 17:20  水三丫  阅读(33)  评论(0编辑  收藏  举报