Maven创建Spring程序 IoC创建对象的方式

目录

使用Maven创建Spring程序

总结

 IoC创建对象的方式

使用无参构造创建对象(默认方式)

使用有参构造创建对象


使用Maven创建Spring程序

项目结构:

编写Hello实体类

package com.damin.pojo;

public class Hello {
    private String str;

    public Hello() {
    }

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

    public String getStr() {
        return str;
    }

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

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

配置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">

<!--    使用Spring创建对象,在Spring中这些都称为bean
        该id属性是标识单个bean定义的字符串。= 变量名
        该class属性定义bean的类型,并使用完全限定的类名。 = new的对象
-->
    <bean id="hello" class="com.damin.pojo.Hello">
        <property name="str" value="wudimin"/>
    </bean>

</beans>

测试

import com.damin.pojo.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对象是谁创建的?
    • hello对象是由Spring创建的。
  • Hello对象的属性是怎么设置的?
    • hello对象的属性是由Spring容器设置的。
  • 控制:谁来控制对象的创建 ?
    • 传统应用程序的对象是由程序本身控制创建的;
    • 使用Spring后,对象是由Spring来创建的。
  • 反转:程序本身不创建对象,而变成被动的接收对象。
  • 依赖注入:就是利用set方法来进行注入。
  • IoC是一种编程思想,由主动的编码变成被动的接收。

学到此,我们就不用在程序中去改动了。要实现不同的操作,只需要在xml配置文件中进行修改。所谓的IoC:对象由Spring来创建、管理和装配。

 IoC创建对象的方式

使用无参构造创建对象(默认方式)

实体类:

public class User {
    private String name;

    public User() {
        System.out.println("无参构造被加载!");
    }
}

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="user" class="com.damin.pojo.User">
<!--    使用无参构造创建对象 —— 默认方式!-->
        <property name="name" value="wudimin"/>

    </bean>

</beans>

测试:

import com.damin.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    public static void main(String[] args) {

        //在配置文件加载的时候,容器中管理的对象就已经初始化了!
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user =(User) context.getBean("user");
    }
}

使用有参构造创建对象

<?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="user" class="com.damin.pojo.User">
        <!--使用有参构造创建对象-->

        <!--第一种方法:下标赋值-->
        <constructor-arg index="0" value="wudimin1"/>
        <!--第二种方法:类型赋值-->
        <constructor-arg type="java.lang.String" value="wudimin2"/>
        <!--第三种方法:参数名赋值-->
        <constructor-arg name="name" value="wudimin3"/>

    </bean>

</beans>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值