多种方式实现依赖注入

  1.Spring 使用setter访问器实现对属性的赋值,

  2.Spring 构造constructor方法赋值,

  3.接口注入

  4.Spring P命名空间注入直接量

  

setter访问器实现方式following

实体类中设置属性的set访问器

 public class Equip {
private String name; //装备名称
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

applicationContext.xml中使用

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
"> <bean id="armet" class="zym.game.entity.Equip">
<property name="name" value="战神头盔"/>
</bean>
</beans>

这样就实现了通过setter赋值     前台getBean("armet")的时候    该对象的name属性默认就是:战神头盔。

构造注入

使用设置注入时,Spring通过JavaBean的无参构造方法实例化对象。当我们编写了带参构造方法后,java虚拟机不会再提供默认的无参构造方法,建议自行添加一个无参构造方法。

 public class User implements Serializable {
private String name;
private String password;
    //car变量称为域属性
private Car car; @Override
public String toString() {
return "用户名:"+this.getName()+"\t密码:"+this.getPassword();
}; public User() {}
}

带有域属性的注入方式:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
"> <!-- 构造注入 域属性注入 -->
<bean id="user11" class="entity.User" scope="singleton">
<constructor-arg index="0" type="java.lang.String" value="zs"></constructor-arg>
16 <constructor-arg index="1" type="java.lang.String" value="pwd@123"></constructor-arg>
17 <constructor-arg index="2" type="entity.Car" ref="mycar"></constructor-arg>
</bean>
</beans>

P命名空间注入直接量

通过设置p:属性值    给类字段进行赋值

需要添加引用:xmlns:p="http://www.springframework.org/schema/p"
可以在pdf文档里找到

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 命名空间注入 -->
<bean id="user2" class="entity.User" p:name="zym" p:password="zym@123"></bean> </beans>

  

IOC和AOP使用扩展 多种方式实现依赖注入的更多相关文章

  1. Ioc和Aop扩展--多种方式实现依赖注入(构造注入,p命名空间注入,集合类型注入,注入null和注入空值)

    构造注入 语法: <constructor-arg>    <ref bean="bean的id"/> </constructor-arg> 1 ...

  2. Spring多种方式实现依赖注入

    平常的Java开发中,程序员在某个类中需要依赖其它类的方法. 通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理. Spring提出了依赖注入的思想,即依赖类不由 ...

  3. Spring——多种方式实现依赖注入

    在Spring的XML配置中,只有一种声明bean的方式:使用<bean>元素并指定class属性.Spring会从这里获取必要的信息来创建bean. 但是,在XML中声明DI时,会有多种 ...

  4. 多种方式实现依赖注入及使用注解定义bean

    构造注入 如何给构造方法中的参数注入方法呢如下 首先bean代码如下 package cn.pojo; public class Greeting { /** * 说的话 */ private Str ...

  5. 依赖注入及AOP简述(十二)——依赖注入对象的行为增强(AOP) .

    四.依赖注入对象的行为增强(AOP) 前面讲到,依赖注入框架的最鲜明的特点就是能够提供受容器管理的依赖对象,并且可以对对象提供行为增强(AOP)功能,所以这一章我们来讨论有关AOP的话题. 1.    ...

  6. ExpandoObject与DynamicObject的使用 RabbitMQ与.net core(一)安装 RabbitMQ与.net core(二)Producer与Exchange ASP.NET Core 2.1 : 十五.图解路由(2.1 or earler) .NET Core中的一个接口多种实现的依赖注入与动态选择看这篇就够了

    ExpandoObject与DynamicObject的使用   using ImpromptuInterface; using System; using System.Dynamic; names ...

  7. spring核心思想:IOC(控制反转)和DI(依赖注入)

    Spring有三大核心思想,分别是控制反转(IOC,Inversion Of Controller),依赖注入(DI,Dependency Injection)和面向切面编程(AOP,Aspect O ...

  8. 特性attribute,声明和使用attribute,应用attribute,AOP面向切面,多种方式实现AOP

    1 特性attribute,和注释有什么区别2 声明和使用attribute3 应用attribute4 AOP面向切面5 多种方式实现AOP ---------------------------- ...

  9. Spring IoC和AOP使用扩展

    Spring提供了设值注入,构造注入等依赖注入方式. 使用p命令空间可以简化属性注入的配置. Spring提供的增强处理类型包括前置增强,异常抛出增强,环绕增强,最终增强等. 通过Schema形式将P ...

随机推荐

  1. andriod学习之一

    今天安装了Android Studio, 但PinyinIME没有导入成功.然后看了Android的一些基础. 知道了Android的基本组件: Activity,服务,内容提供程序,广播接收器. 大 ...

  2. 常见C内存管理程序

    本文主要关注的是C内存管理程序,比较著名的几个C内存管理程序,其中包括: l   Doug Lea Malloc:Doug Lea Malloc实际上是完整的一组分配程序,其中包括Doug Lea的原 ...

  3. MySQL性能优化总结(转)https://yq.aliyun.com/articles/24249

    摘要: 一.MySQL的主要适用场景 1.Web网站系统 2.日志记录系统 3.数据仓库系统 4.嵌入式系统 二.MySQL架构图:   三.MySQL存储引擎概述 1)MyISAM存储引擎 MyIS ...

  4. 开源是一种态度、分享是一种精神 — FirApi发布、WeiXinApi更新

    在云计算盛行的年代,接触开发式的平台必不可少,因项目累积的代码也不少,之前本着"重复的事情自己做一次就够了,不需要其他人在重复为此工作."的想法发布了WeiXinApi.Boots ...

  5. ubuntu16.04解决播放swf视频文件问题

    使用下面 sudo apt-get install swfdec-gnome

  6. angularjs如何在视图渲染结束之后,或者render之后执行指令中的link方法呢?

    angularjs如何在视图渲染结束之后,或者render之后执行指令中的link方法 关键字: $timeout app.directive("myDirective",func ...

  7. php读取zip文件(删除文件,提取文件,增加文件)实例

    <?php /* php 从zip压缩文件中提取文件 */ $zip = new ZipArchive; if ($zip->open('jQuery五屏上下滚动焦点图代码.zip') = ...

  8. 去块率波 Deblocking filter

    基于块的视频编码的一个典型特点就是在图像中会出现偶发的可察觉的块结构,这是由于重构块的边缘像素与块内部像素相比恢复精度要低,块效应是目前压缩编码最明显的视觉失真之一.在H.264/ AVC视频编码标准 ...

  9. 智能指针 ADO数据库连接

    ADO库包含三个基本接口:_ConnectionPtr接口._CommandPtr接口和_RecordsetPtr接口._ConnectionPtr接口返回一个记录集或一个空指针.通常使用它来创建一个 ...

  10. C#如何定义全局变量

    C#中没有全局变量的概念,可以定义一个common类,通过静态变量来存放所有需要的全局变量,调用的时候通过common来调用即可. 例如:  public static class common // ...