Spring依赖注入

 什么是Spring的依赖注入?有什么好处

依赖注入:是指程序运行过程中,如果需要创建一个对象,无须再代码中new创建,而是依赖外部的注入。
spring的依赖注入对调用者和被调用者几乎没有任何要求,完全支持对pojo之间依赖关系的管理

new对象:类的头部进行实例化对象和依赖注入一个效果,这个时候该对象不管是否使用都贯穿该类的始终。该类对象不被回收,这个实例化对象也不会被回收。
如果要使用多例对象则最好使用new创建对象而不是依赖注入,即使依赖注入有多例模式也不推荐。

spring实现了对象池,一些对象创建和使用完毕之后不会被销毁,放进对象池(某种集合)以备下次使用,下次再需要这个对象,不new,直接从池里出去来用。节省时间,节省cpu。做到内存节省并且代码的耦合度也降低。
面向接口编程中,让依赖注入只需要找到符合规范的接口注入即可实现调用者和被调用者解耦。对象的调用关系由spring管理。

实体类

配置文件:

<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.heerlin.pojo.Address">
        <property name="address" value="泸州"/>
    </bean>

    <bean id="student" class="com.heerlin.pojo.Student">
<!--        普通值直接使用value-->
        <property name="name" value="何耳林"/>
<!--        第二种,bean注入,ref-->
        <property name="address" ref="address"/>
<!--        数组注入,ref-->
        <property name="books">
           <array>
               <value>红楼梦</value>
               <value>西游记</value>
               <value>水浒传</value>
           </array>
        </property>
<!--        List-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>敲代码</value>
                <value>看电影</value>
            </list>
        </property>
<!--        map-->
        <property name="card">
            <map>
                <entry key="身份证" value="551515"/>
                <entry key="银行卡" value="1111111"/>

            </map>
        </property>

<!--        set-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>COC</value>
                <value>BOB</value>
            </set>
        </property>

    </bean>

</beans>

 测试:

import com.heerlin.pojo.Student;
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");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());

    }
}

 结果:

Student{name='何耳林', address=Address{address='泸州'}, books=[红楼梦, 西游记, 水浒传], hobbys=[听歌, 敲代码, 看电影], card={身份证=551515, 银行卡=1111111}, games=[LOL, COC, BOB]}

c命名和p命名空间注入:

p:命名空间

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空注入,可以直接注入属性的值:property-->
    <bean id="user" class="com.heerlin.pojo.User" p:name="张林林" p:age="18"/>
</beans>

测试

 c:命名空间

必须要有构造器

测试:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值