一、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

最新文章

  1. C# 调用webservice 几种办法(转载)
  2. Python 3 与 MySQL 5.6
  3. - dequeueReusableCellWithIdentifier:
  4. thinkphp类的调用
  5. iOS WKWebView详解
  6. 为什么要CGI
  7. ZOJ Monthly, November 2014
  8. java学习,从一个字符串中统计同一类型出现的次数
  9. LESS语法备忘
  10. 样式优先级、margin
  11. Scrapy的架构初探
  12. CentOS 7.2 配置mysql5.7
  13. awk输出单引号,双引号【转】
  14. nodejs教程 安装express及配置app.js文件的详细步骤
  15. LogisticRegression 和 LogisticRegressionCV
  16. 杭电ACM 1297 Children’s Queue
  17. WP8.1学习系列(第十一章)——中心控件Hub开发指南
  18. Delphi编程实现是否开启“平滑屏幕字体边缘“
  19. iOS 9的新的改变 iOS SDK Release Notes for iOS 9 说了些改变
  20. Java中应该返回零长度数组或空集合,而不是返回null(转)

热门文章

  1. Boa Web Server 缺陷报告及其修正方法
  2. 8.5-7 mkfs、dumpe2fs、resize2fs
  3. 一些固化了的语音识别模块demo, 手机重力传感器获取
  4. SpringBoot基础学习(一) SpringBoot概念、简单案例实现、单元测试及热部署讲解
  5. springboot整合JDBC出现Loading class `com.mysql.jdbc.Driver&#39;. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver&#39;.
  6. Nginx限制访问速率和最大并发连接数模块--limit
  7. selenium模拟不同浏览器的方式
  8. Python-Redis-常用操作&amp;管道
  9. Jmeter- 笔记9 - CLI(无图形界面)
  10. 服务化部署框架Paddle Serving