java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring通过IOC来创建对象

Spring(二):Spring通过IOC来创建对象

作者:熬夜加班写代码

下面小编就为大家带来一篇详谈Spring对IOC的理解(推荐篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一、IOC如何获取对象

1.1 Spring是如何获取对象的?

①新建一个maven项目后导入webmvc的依赖:因为webmvc包含了很多其他依赖,为了省事,干脆导入一个总的,方便省事!版本嘛!个人比较喜欢用最新版。

    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.5</version>
    </dependency>

②新建实体测试类:

public class Person {
    private String name;
    private int age;
    private String like;
    private String high;
    //get、set、tostring方法为了篇幅省略,可以自己加或者使用lombok
}

③在resources目录下新建ContextAplication.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="Person" class="entity.Person">
        <property name="age" value="23"></property>
        <property name="name" value="丁大大"></property>
        <property name="like" value="钓鱼"></property>
        <property name="high" value="173"></property>
    </bean>
</beans>

④以上前提之后,你会发现你的测试Person类种发生了变化:点击可以跳转到指定的xml位置哦~

在这里插入图片描述

⑤测试:

Context.getBean() 不指定类时,需要强制转换,所以建议使用第二种方式来获取对象

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
//        Person person = (Person) Context.getBean("Person");//这里不指定的话需要强转,建议用下面的方式来拿对象
        Person person = Context.getBean("Person",Person.class);
        System.out.println(person);
    }
}

⑥执行结果如下:成功拿到值!

在这里插入图片描述

⑦总结:

1.2 改造案例由xml选择创建对象

①xml:

 <bean id="StudentMapperImpl" class="mapper.impl.StudentMapperImpl"/>
    <bean id="TeacherMapperImpl" class="mapper.impl.TeacherMapperImpl"/>
    <bean id="PersonServiceImpl" class="service.impl.PersonServiceImpl">
        <property name="studentMapper" ref="StudentMapperImpl"/>
    </bean>

②测试:

        ApplicationContext Context1 = new ClassPathXmlApplicationContext("ContextAplication.xml");
        PersonServiceImpl personServiceImpl = Context1.getBean("PersonServiceImpl", PersonServiceImpl.class);
        personServiceImpl.getPersonInfo();

③执行结果:

在这里插入图片描述

⑤总结:

对象由Spring 来创建 , 管理 , 装配 !这就是 IOC!

二、IOC是通过什么方式来创建对象的?

2.1 通过无参构造函数来创建对象

①以Person类为例子,但是加上一个无参构造函数!

public class Person {
    private String name;
    private int age;
    private String like;
    private String high;
    public Person() {
        //输出一句话证明自己被调用了!
        System.out.println("我是Person类的无参构造函数!我被调用了!!!!");
    }
    //set、get、tostring方法因为篇幅原因省略,请手动加上!
}

②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="Person" class="entity.Person">
        <property name="age" value="23"></property>
        <property name="name" value="丁大大"></property>
        <property name="like" value="钓鱼"></property>
        <property name="high" value="173"></property>
    </bean>
</beans>

③测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person person = Context.getBean("Person", Person.class);
        System.out.println(person);
    }
}

④执行结果:

在这里插入图片描述

⑤去除无参构造,增加有参构造:

在这里插入图片描述

xml配置程序直接报错:

在这里插入图片描述

⑥总结:

Spring创建对象默认是通过无参构造函数创建的!能通过有参构造函数来创建对象嘛?能!看下面!

2.2 通过有参构造方法来创建对象

①前提于 2.1 一致,新增有参构造函数:(因为类中,默认的也就是不写构造参数就是无参构造,写了有参构造才能真正意义上去除无参构造,这个不用解释太多吧,java基础的内容了~!)

    public Person(String name, int age, String like, String high) {
        this.name = name;
        this.age = age;
        this.like = like;
        this.high = high;
    }

②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="Person" class="entity.Person">
<!--        <property name="name" value="丁大大"></property>-->
<!--        <property name="age" value="23"></property>-->
<!--        <property name="like" value="钓鱼"></property>-->
<!--        <property name="high" value="173"></property>-->
        <constructor-arg index="0" value="丁大大"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg type="java.lang.String" value="钓鱼"/>
        <constructor-arg type="java.lang.String" value="173"/>
    </bean>
</beans>

③执行结果:

在这里插入图片描述

⑤总结:

三、Spring的配置

3.1 alias(别名):

①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">
    <alias name="Person" alias="personAlias1"/>
    <alias name="Person" alias="personAlias2"/>
    <alias name="Person" alias="personAlias3"/>
    <bean id="Person" class="entity.Person">
        <constructor-arg index="0" value="丁大大"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg type="java.lang.String" value="钓鱼"/>
        <constructor-arg type="java.lang.String" value="173"/>
    </bean>
</beans>

②测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person person = Context.getBean("personAlias1", Person.class);
        System.out.println(person);
    }
}

③执行结果:

在这里插入图片描述

④总结:讲实话,这玩意用处不大,因为还有更好的方式来设置别名!

3.2 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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <alias name="Person" alias="personAlias1"/>
    <alias name="Person" alias="personAlias2"/>
    <alias name="Person" alias="personAlias3"/>
    <bean id="Person" name="person1,person2 person3;person4" class="entity.Person">
        <constructor-arg index="0" value="丁大大"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg type="java.lang.String" value="钓鱼"/>
        <constructor-arg type="java.lang.String" value="173"/>
    </bean>
</beans>

②测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person person = Context.getBean("person4", Person.class);
        System.out.println(person);
    }
}

③执行结果:

在这里插入图片描述

④总结:

在这里插入图片描述

3.3 import(团队合作之导入)

①在实际工作的开发过程中,一个项目可能由多个程序员来进行开发,所以为了解决共性问题,比如:同一文件提交时都进行了修改可能引起冲突,所以我们使用import来解耦!

②新建多个xml配置文件:

在这里插入图片描述

ContextAplication.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">
    <import resource="dyj1.xml"/>
    <import resource="dyj3.xml"/>
    <import resource="dyj2.xml"/>
</beans>

dyj1.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 name="person1,person2 person3;person4" class="entity.Person">
        <constructor-arg index="0" value="丁大大1"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg type="java.lang.String" value="钓鱼1"/>
        <constructor-arg type="java.lang.String" value="173"/>
    </bean>
</beans>

dyj2.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 name="person1,person2 person3;person4" class="entity.Person">
        <constructor-arg index="0" value="丁大大2"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg type="java.lang.String" value="钓鱼2"/>
        <constructor-arg type="java.lang.String" value="173"/>
    </bean>
</beans>

dyj3.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 name="person1,person2 person3;person4" class="entity.Person">
        <constructor-arg index="0" value="丁大大3"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg type="java.lang.String" value="钓鱼3"/>
        <constructor-arg type="java.lang.String" value="173"/>
    </bean>
</beans>

③执行:

在这里插入图片描述

④总结:

总结

本篇文章就到这里了,希望能帮助到你,也希望您能够多多关注脚本之家的更多内容!

您可能感兴趣的文章:
阅读全文