Java全栈课程之Spring详解——依赖注入

本文详细介绍了Spring框架中构造器注入和set方式的依赖注入,包括使用`set`方法注入bean对象的属性,以及通过``标签在XML配置文件中的实例化和属性赋值。同时探讨了bean的作用域,如单例、原型模式以及在web应用中的request、session和application作用域。
摘要由CSDN通过智能技术生成

一、构造器注入

二、set方式注入

        1.依赖注入:set注入

        2.依赖:bean对象的创建依赖于容器

        3.注入;bean对象中的所有属性,有容器来注入

        4.环境搭建

                ① 复杂类型

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

                ② 真实对象

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> game;
    private String wife;
    private Properties info;

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGame() {
        return game;
    }

    public void setGame(Set<String> game) {
        this.game = game;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", game=" + game +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

                ③ 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="student" class="com.sun.pojo.Student">
	  <!--第一种 普同值注入value-->
        <property name="name" value="小张雪"/>
    </bean>
</beans>

                ④ 测试类

public class MyText {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans. xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.getName());
    }
}

                ⑤ 完善注解

<?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="address" class="com.sun.pojo.Address"/>
    <bean id="student" class="com.sun.pojo.Student">
        <!--第一种,普同值注入value-->
        <property name="name" value="小张雪"/>
        <!--第二种,Bean注入:ref-->
        <property name="address" ref="address"/>
        <!--数组注入-->
        <property name="books">
            <array>
                <value>java</value>
                <value>c</value>
                <value>python</value>
                <value>c++</value>
            </array>
        </property>
        <!--List注入-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>刷抖音</value>
                <value>敲代码</value>
            </list>
        </property>
        <!--Map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="2222222222222222"/>
                <entry key="银行卡" value="1111111111111111"/>
            </map>
        </property>
        <!--Set注入-->
        <property name="game">
            <set>
                <value>王者</value>
                <value>吃鸡</value>
                <value>蛋仔</value>
            </set>
        </property>
        <!--null注入-->
        <property name="wife">
            <null>/</null>
        </property>
        <!--Properties注入-->
        <property name="info">
            <props>
                <prop key="driver">20240205</prop>
                <prop key="url">男</prop>
                <prop key="username">小明</prop>
                <prop key="password">123456</prop>
            </props>
        </property>
    </bean>
</beans>

三、其他方式注入

        我们可以使用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
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--p:命名空间注入,可以直接注入属性的值:property-->
    <bean id="user" class="com.sun.pojo.User" p:name="小张" p:age="18"/>
    <!--c:命名空间注入,通过构造器注入:construct-args-->
    <bean id="user2" class="com.sun.pojo.User" c:age="18" c:name="小张"/>
</beans>

测试:

@Test
public void text2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
    User user = context.getBean("user", User.class);
    System.out.println(user);
}
@Test
public void text3(){
    ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
    User user = context.getBean("user2", User.class);
    System.out.println(user);
}

注意:p命名和c命名空间不能直接使用,需要导入xml约束

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

四、bean的作用域

Scope

Description

singleton

(Default) Scopes a single bean definition to a single object instance for each Spring loC container.

prototype

Scopes a single bean definition to any number of object instances.

request

Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a beancreated off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.

session

Scopes a single bean definition to the lifecycle of an HTTP Session . Only valid in the context of a web-aware Spring ApplicationContext.

application

Scopes a single bean definition to the lifecycle of a ServletContext . Only valid in the context of a web-aware Spring ApplicationContext.

websocket

Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.

 1.单例模式(spring默认机制)

<bean id="user2" class="com.sun.pojo.User" c:age="18" c:name="小张" scope="singleton"/>

2.原型模式:每次从容器中get的时候,都会产生一个新对象

<bean id="user2" class="com.sun.pojo.User" c:age="18" c:name="小张" scope="prototype"/>

3.其余的request、session、application,这些个只能在web开发中使用到 

  • 29
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小孙同学1024

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

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

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

打赏作者

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

抵扣说明:

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

余额充值