您现在的位置是:群英 > 开发技术 > 编程语言
Spring ioc注入的方式有哪些,怎样实现?
Admin发表于 2022-01-22 17:49:35749 次浏览

    这篇文章给大家分享的是Spring ioc注入的方式。小编觉得挺实用的,因此分享给大家做个参考,Spring ioc注入主要有三种方式,文中的示例代码介绍得很详细,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。

    本教程操作环境:windows7系统、java8版本、Dell G3电脑。

    Spring IOC(依赖注入的三种方式):

    1、Setter方法注入

    Setter方法注入是容器通过调用无参构造器或无参static工厂 方法实例化bean之后,调用该bean的setter方法,即实现了基于setter的依赖注入。

package com.jpeony.spring.setter;
import com.jpeony.spring.common.HelloServiceImpl;public class HelloWord {
private HelloService helloService;
 
    // setter方式注入Bean
    public void setHelloService(HelloService helloService) {
        this.helloService = helloService;
    }
 
    @Override
    public void selfIntroduction() {
        // 向大家打招呼
        helloService.sayHello("大家好!");
    }
 
}
<?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声明:
         该bean类似于javaConfig中的@Bean注解;
         用于创建bean的类通过class属性来指定,并且需要使用全限定的类名。
         通过id指定bean的ID。如果不显示指定,默认使用class的全限定名进行命名。
         eg:
         com.jpeony.spring.common.HelloServiceImpl#0,其#0是一个计数器的形式,
         用来区分相同类型的其他bean。
         使用自动化命名很方便,但是没有多少实际用处,还是建议自己给bean显示设定ID。
     -->
    <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>
 
    <!-- setter注入bean -->
    <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord">
        <property name="helloService" ref="helloService"/>
    </bean>
 
</beans>

    2、构造方法注入

    构造器依赖注入通过容器触发一个类的构造器来实现的,该类有一系列参数,每个参数代表一个对其他类的依赖。

package com.jpeony.spring.setter;
import com.jpeony.spring.common.HelloServiceImpl;

public class HelloWord {
    private HelloService helloService;
 
    // 构造方法注入
    public HelloWord (HelloService helloService) {
        this.helloService = helloService;
    }
 
}
<?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声明:
         该bean类似于javaConfig中的@Bean注解;
         用于创建bean的类通过class属性来指定,并且需要使用全限定的类名。
         通过id指定bean的ID。如果不显示指定,默认使用class的全限定名进行命名。
         eg:
         com.jpeony.spring.common.HelloServiceImpl#0,其#0是一个计数器的形式,
         用来区分相同类型的其他bean。
         使用自动化命名很方便,但是没有多少实际用处,还是建议自己给bean显示设定ID。
     -->
    <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>
 
    <!-- 构造方法注入bean -->
    <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord">
        <constructor-arg><ref bean="helloService"/></constructor-arg>
    </bean>
 
</beans>

    3、P命名空间注入

package com.jpeony.spring.setter;
import com.jpeony.spring.common.HelloServiceImpl;

public class HelloWord {
    //名字
    private String name;
    //年龄
    private String age;
    //方法类
    private HelloService helloService;
 
    public void setName (String name) {
        this.name = name;
    }
    
    public void setAge (String age) {
        this.age = age;
    }
    
    public void setHelloService(HelloService helloService) {
        this.helloService = helloService;
    }
 
    @Override
    public void selfIntroduction() {
        // 向大家打招呼
        helloService.sayHello("我叫"+ name + ",今年" + age + "岁,大家好!");
    }
 
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       <!-- 引入p命名标签 -->
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>
 
    <!-- p标签注入bean -->
    <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord" 
     p:name="明明" p:age="24" p:helloService-ref="helloService"></bean>
 
</beans>

    P标签注入集合bean

package com.jpeony.spring.setter;
import com.jpeony.spring.common.HelloServiceImpl;
import java.util.List;

public class HelloWord {
    //名字
    private String name;
    //年龄
    private String age;
    //方法类
    private List<HelloService> helloServices;
 
    public void setName (String name) {
        this.name = name;
    }
    
    public void setAge (String age) {
        this.age = age;
    }
    
    public void setHelloServices(List<HelloService> helloServices) {
        this.helloServices = helloServices;
    }
 
    @Override
    public void selfIntroduction() {
        // 向大家打招呼
        helloServices[0].sayHello("我叫"+ name + ",今年" + age + "岁,大家好!");
    }
 
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       <!-- 引入p命名标签 -->
       xmlns:p="http://www.springframework.org/schema/p"
       <!-- 引入util命名标签 -->
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>

    <bean id="helloService2" class="com.jpeony.spring.common.HelloServiceImpl">
    ...........
    </bean>
 
    <util:list id="helloServices">
        <ref bean="helloService"/>
        <ref bean="helloService2"/>
    </util:list>

    <!-- p标签注入bean -->
    <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord" 
     p:name="明明" p:age="24" p:helloServices-ref="helloServices"></bean>
 
</beans>

    以上就是关于Spring ioc注入的方式的介绍,上述示例具有一定的参考价值,有需要的朋友可以了解看看,希望对大家学习有帮助,想要了解更多可以继续浏览群英网络其他相关的文章。

文本转载自PHP中文网

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: spring ioc注入
相关信息推荐
2022-08-24 17:59:03 
摘要:本篇文章带大家了解一下node中的EventEmitter,简单聊聊一下异步操作、error事件、EventEmitter类,希望对大家有所帮助!
2022-09-06 17:50:35 
摘要:本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM浏览器对象模型的相关问题,包括了windows对象的常见事件、定时器、js执行机制等等内容,下面一起来看一下,希望对大家有帮助。
2022-09-02 17:59:18 
摘要:php转16进制的函数有:1、bin2hex(),可把ASCII字符的字符串转换为十六进制值;2、dechex(),可把十进制数转换为十六进制数,3、base_convert(),可在任意进制之间转换数字。
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部