java中springioc依赖注入的方式有哪些-创新互联

这篇文章将为大家详细讲解有关java中spring ioc依赖注入的方式有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联一直在为企业提供服务,多年的磨炼,使我们在创意设计,网络营销推广到技术研发拥有了开发经验。我们擅长倾听企业需求,挖掘用户对产品需求服务价值,为企业制作有用的创意设计体验。核心团队拥有超过十载以上行业经验,涵盖创意,策化,开发等专业领域,公司涉及领域有基础互联网服务达州服务器托管App定制开发、手机移动建站、网页设计、网络整合营销。

spring ioc注入的三种方式是:1、Setter方法注入,是容器通过调用无参构造器或无参static工厂 方法实例化bean之后,调用该bean的setter方法。2、构造方法注入。3、P命名空间注入。

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("大家好!");
    }
 
}


    
    
 
    
    
        
    
 

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;
    }
 
}


    
    
 
    
    
        
    
 

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 + "岁,大家好!");
    }
 
}


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

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 helloServices;
 
    public void setName (String name) {
        this.name = name;
    }
    
    public void setAge (String age) {
        this.age = age;
    }
    
    public void setHelloServices(List helloServices) {
        this.helloServices = helloServices;
    }
 
    @Override
    public void selfIntroduction() {
        // 向大家打招呼
        helloServices[0].sayHello("我叫"+ name + ",今年" + age + "岁,大家好!");
    }
 
}


       xmlns:p="http://www.springframework.org/schema/p"
       
       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">
    
    

    
    ...........
    
 
    
        
        
    

    
    
 

关于“java中spring ioc依赖注入的方式有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


网站栏目:java中springioc依赖注入的方式有哪些-创新互联
当前路径:http://lszwz.com/article/ggejs.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款