【Spring】HelloSpring,IOC创建对象的方式,Spring配置,依赖注入

本文详细介绍了Spring框架的基本使用,包括导入jar包、创建Java类、编写Spring配置文件并进行测试。讲解了IOC(控制反转)的概念,即对象由Spring容器创建和管理,并通过setter方法进行依赖注入。还探讨了无参和有参构造器创建对象的方式,以及别名、Bean配置和import的用法。此外,文章通过实例详细展示了如何通过setter注入复杂类型的属性,并介绍了p命名空间和c命名空间的注入方式。最后,讨论了Bean的作用域,包括Singleton和Prototype模式。

1.HelloSpring

1.1使用过程

1.导入jar包

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

2.编写一个java类

public class Hello {
   
   
   private String name;

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

   public void show(){
   
   
       System.out.println("Hello,"+ name );
  }
}

3.编写spring配置文件 , 这里我们命名为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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    使用Spring来创建对象,在Spring这些都称为Bean
    类型 变量名  =  new 类型();
    Hello hello = new Hello();
    bean = 对象 new Hello();

    id = 变量名
    class = new 的对象;
    property 相当于给对象中的属性设置一个值!
-->
    <bean id="hello" class="com.adie.pojo.Hello">
        <property name="str" value="Spring"/>
    </bean>

</beans>

4.进行测试

import com.adie.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容器设置的

这个过程就叫控制反转(IOC) :

  • 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的
  • 反转 : 程序本身不创建对象 , 而变成被动的接收对象 .

依赖注入 : 就是利用set方法来进行注入的.(在上述案例中,如果我们在Hello的java类中删除set方法,在beans.xml中将会报错)

IOC是一种编程思想,由主动的编程变成被动的接收

可以通过ClassPathXmlApplicationContext类去浏览一下底层源码 。

1.2修改案例一

在上一个博客中的案例,新增一个Spring配置文件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
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="mysqlImpl" class="com.adie.dao.UserDaoMysqlImpl"/>
    <bean id="oracleImpl" class="com.adie.dao.UserDaoOracleImpl"/>

    <bean id="UserServiceImpl" class="com.adie.service.UserServiceImpl">
<!--
ref:引用Spring容器中创建好的对象
value:具体的值,基本数据类型
-->
        <property name="userDao" ref="mysqlImpl"/>
    </bean>

</beans>

测试

@Test
public void test2(){
   
   
   //获取ApplicationContext;拿到Spring的容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //容器在手,需要什么就直接get什么
        UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
        userServiceImpl.getUser();
}

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

2.IOC创建对象的方式

2.1使用无参构造创建对象,默认

1、User.java

package com.adie.pojo;
public class User {
   
   
    private String name;

    public User(String name){
   
   
        this.name=name;
    }

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

    public String getName() {
   
   
        return name;
    }

    public void show(){
   
   
        System.out.println("name="+name);
    }
}

2、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
       http://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="user" class="com.kuang.pojo.User">
       <property name="name" value="adie"/>
   </bean>

</beans>

3、测试类

@Test
public void test(){
   
   
   ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
   //在执行getBean的时候, user已经创建好了 , 通过无参构造
   User user = (User) context.getBean("user");
   //调用对象的方法 .
   user.show();
}

结果发现,在调用show方法之前,User对象已经通过无参构造初始化了!
在这里插入图片描述

2.2通过有参构造创建对象

​ 1.下标赋值

<bean id="user" class="com.adie.pojo.User">
    <constructor-arg index="0" value="a碟碟"/>
</bean>

​ 2.通过类型创建,不建议使用

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a碟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值