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

1、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(依赖注入的常用数据类型):

1、注入直接量(基本数据类型、字符串)

<bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord">
<property name="name" value="明明"></property>
<property name="age" value="24"></property>
</bean>

2、引用其他Bean组件(面向接口编程)

    使用ref属性:

    <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>

    <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord">
<property name="helloService" ref="helloService"></property>
</bean>

使用ref标签:

<bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>

    <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord">
<property name="helloService">
<ref bean="helloService" />
</property>
</bean>

    使用P命名空间:

     <!-- 头文件加上这句 -->
xmlns:p="http://www.springframework.org/schema/p" <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/> <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord" p:helloService-ref="helloService"></bean>

3、使用内部Bean

    <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord">
<property name="helloService">
<bean class="com.jpeony.spring.common.HelloServiceImpl" />
</property>
</bean>

4、集合类型的属性

// 对应的getter setter
public class ALLCollection {
private List listElement;
private String[] arrayElement;
private Set setElement;
private Map mapElement;
private Properties propsElement; public void setListElement (List listElement) {
this.listElement = listElement;
} public List getListElement () {
return listElement;
} public void setArrayElement (String[] arrayElement) {
this.arrayElement= arrayElement;
} public String[] getArrayElement () {
return arrayElement;
} public void setSetElement (Set setElement) {
this.setElement= setElement;
} public Set getSetElement () {
return setElement;
} public void setMapElement (Map mapElement) {
this.mapElement= mapElement;
} public Map getMaptElement () {
return mapElement;
} public void setPropsElement (Properties propsElement) {
this.propsElement= propsElement;
} public Properties getpropsElement () {
return propsElement;
} }
<bean id="collection" class="com.zxf.DO.ALLCollection">
<property name="listElement">
<list>
<value>list苹果</value>
<value>list香蕉</value>
</list>
</property>
<property name="arrayElement">
<array>
<value>array苹果</value>
<value>array香蕉</value>
</array>
</property>
<property name="setElement">
<set>
<value>set苹果</value>
<value>set香蕉</value>
</set>
</property>
<property name="mapElement">
<map>
<entry>
<key><value>map1</value></key>
<value>map苹果</value>
</entry>
<entry>
<key><value>map2</value></key>
<value>map香蕉</value>
</entry>
</map>
</property>
<property name="propsElement">
<props>
<prop key="prop1">prop苹果</prop>
<prop key="porp2">prop香蕉</prop>
</props>
</property>
</bean>

文章整合至:https://blog.csdn.net/joe18576558921/article/details/80973385https://www.cnblogs.com/DDiamondd/p/11398678.html

Spring:Spring-IOC三种注入方式、注入不同数据类型的更多相关文章

  1. spring Bean的三种配置方式

    Spring Bean有三种配置方式: 传统的XML配置方式 基于注解的配置 基于类的Java Config 添加spring的maven repository <dependency> ...

  2. Spring boot 集成三种定时任务方式

    三种定时任务方式分别为 org.springframework.scheduling.annotation.Scheduled java.util.concurrent.ScheduledExecut ...

  3. Spring boot 集成三种拦截方式

    三种拦截方式分别为: javax.servlet.Filter org.springframework.web.servlet.HandlerInterceptor org.aspectj.lang. ...

  4. spring bean的三种管理方式·

    1.无参构造函数 1.xml文件配置内容 <!--无参构造函数--> <bean id="bean1" class="com.imooc.ioc.dem ...

  5. Spring Security Encryption三种加密方式

    Encryption One-way encryption       单项加密,客户端将要传递的值先加密(使用特定的加密方法),将原值和加密好的值传递过去,服务器端将原始数据也进行一次加密(两者加密 ...

  6. spring ioc三种注入方式

    spring ioc三种注入方式 IOC ,全称 (Inverse Of Control) ,中文意思为:控制反转 什么是控制反转? 控制反转是一种将组件依赖关系的创建和管理置于程序外部的技术. 由容 ...

  7. [转载]Spring下IOC容器和DI(依赖注入) @Bean及@Autowired

    Spring下IOC容器和DI(依赖注入) @Bean及@Autowired自动装配 bean是什么 bean在spring中可以理解为一个对象.理解这个对象需要换一种角度,即可将spring看做一门 ...

  8. spring注解之@Import注解的三种使用方式

    目录 1.@Import注解须知 2.@Import的三种用法 3.@Import注解的三种使用方式总结 @ 1.@Import注解须知 1.@Import只能用在类上 ,@Import通过快速导入的 ...

  9. spring配置datasource三种方式

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp34 spring配置datasource三种方式 1.使用org.spri ...

  10. Spring Boot将Mybatis返回结果转为驼峰的三种实现方式

    本文不再更新,可能存在内容过时的情况,实时更新请访问原地址:Spring Boot将Mybatis返回结果转为驼峰的三种实现方式: 我们通常获取Mybatis返回的数据结果时想要将字段以驼峰的形式返回 ...

随机推荐

  1. 6.2 gzip:压缩或解压文件

    gzip命令 用于将一个大的文件通过压缩算法(Lempel-Ziv coding(LZ77))变成一个小的文件.gzip命令不能直接压缩目录,因此目录需要先用tar打包成一个文件,然后tar再调用gz ...

  2. python内存管理总结

    之前在学习与工作中或多或少都遇到关于python内存管理的问题,现在将其梳理一下. python内存管理机制 第0层 操作系统提供的内存管理接口 c实现 第1层 基于第0层操作系统内存管理接口包装而成 ...

  3. easyUI中datagrid展示对象下属性以及显示多个子属性(Day_37)

    easyUI中datagrid展示对象下属性以及显示多个子属性 显示对象单个属性值 添加formatter属性 <th field="decidedzone" width=& ...

  4. error – Public key for *.rpm is not installed (--nogpgcheck)

    docker容器删除的东西比较多,有很多东西都没有,配置上源后发现有也问题 第一是源的选择不对应系统版本,第二是找不到gpgcheck文件 如果一时半会找不到gpgchenck file,使用 --n ...

  5. java和kotlin的可见性修饰符对比

    private 意味着只在这个类内部(包含其所有成员)可见: protected-- 和 private一样 + 在子类中可见. internal -- 能见到类声明的 本模块内 的任何客户端都可见其 ...

  6. Selenium3自动化测试【18】XPath定位元素(2)

    层级与属性结合定位 如果被定为的元素,无法通过自身属性来唯一标识自己,此时可以考虑借助上级元素来定位自己.举生活中的例子,一个婴儿刚出生,还没有姓名与身份证号,此时给婴儿进行检查时往往会标注为&quo ...

  7. Zabbix企业分布式监控工具

    前言:在工作中常常需要对服务器进行监控,但是要选择一款合适监控软件可不容易,今天介绍下zabbix这款监控软件 一.Zabbix介绍1.Zabbix是一个企业级的.开源的.分布式的监控套件2.Zabb ...

  8. Step By Step(Lua编译执行与错误)

    Step By Step(Lua编译执行与错误) 1. 编译:    Lua中提供了dofile函数,它是一种内置的操作,用于运行Lua代码块.但实际上dofile只是一个辅助函数,loadfile才 ...

  9. OpenResty搭建高性能服务端

    OpenResty搭建高性能服务端   Socket编程 Linux Socket编程领域为了处理大量连接请求场景,需要使用非阻塞I/O和复用,select.poll.epoll是Linux API提 ...

  10. HTML <a> 标签的 href 属性

    w3school页面的描述: HTML <a> 标签的 href 属性 HTML <a> 标签 实例 href 属性规定链接的目标: <a href="http ...