Rumah  >  Artikel  >  Java  >  spring ioc注入的三种方式是什么

spring ioc注入的三种方式是什么

青灯夜游
青灯夜游asal
2021-02-04 17:47:5717323semak imbas

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

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 + "岁,大家好!");
    }
 
}
930406f08da8ee4a2ff134b688d29d1d
4f80a00a6ad72fb96688de7ce488e30a
       xmlns:p="http://www.springframework.org/schema/p"
       5e49173f29e4a67ddaa90131e7a99dea
       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">
    
    9df278062f1e81860f65eb12100b2b72

    a9792929f72ef4e41fe56b009215d491
    ...........
    4bb0e59fd50cbfe6f6ce2215b9d94243
 
    1e928befbc0f48f44eec15e59207e744
        093f30557f32572e8ac2e4ba8bcdba31
        30fe8a23ea2fb08456a9dcc68d3d7007
    0cb3faf542ecac6f480b475d17bb9dd0

    ea108911772f997d1c30f6a3a707e315
    516e0c3bf2fb206b6995515c46ebe8c94bb0e59fd50cbfe6f6ce2215b9d94243
 
a6748f3712e7c0e1f60ffca24b3db7b0

更多计算机编程相关知识,请访问:编程入门!!

Atas ialah kandungan terperinci spring ioc注入的三种方式是什么. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:java怎么把字符串转换成整数Artikel seterusnya:Java流程控制介绍