三 依赖注入 (dependency injection) 的学习

Similar documents
AvalonMiner Raspberry Pi Configuration Guide. AvalonMiner 树莓派配置教程 AvalonMiner Raspberry Pi Configuration Guide

Understanding IO patterns of SSDs

ICP Enablon User Manual Factory ICP Enablon 用户手册 工厂 Version th Jul 2012 版本 年 7 月 16 日. Content 内容

PCU50 的整盘备份. 本文只针对操作系统为 Windows XP 版本的 PCU50 PCU50 启动硬件自检完后, 出现下面文字时, 按向下光标键 光标条停在 SINUMERIK 下方的空白处, 如下图, 按回车键 PCU50 会进入到服务画面, 如下图

实验三十三 DEIGRP 的配置 一 实验目的 二 应用环境 三 实验设备 四 实验拓扑 五 实验要求 六 实验步骤 1. 掌握 DEIGRP 的配置方法 2. 理解 DEIGRP 协议的工作过程

朱晔和你聊 Spring 系列 S1E2: SpringBoot 并不神秘

Previous on Computer Networks Class 18. ICMP: Internet Control Message Protocol IP Protocol Actually a IP packet

TW5.0 如何使用 SSL 认证. 先使用 openssl 工具 1 生成 CA 私钥和自签名根证书 (1) 生成 CA 私钥 openssl genrsa -out ca-key.pem 1024

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

计算机组成原理第二讲 第二章 : 运算方法和运算器 数据与文字的表示方法 (1) 整数的表示方法. 授课老师 : 王浩宇

Microsoft RemoteFX: USB 和设备重定向 姓名 : 张天民 职务 : 高级讲师 公司 : 东方瑞通 ( 北京 ) 咨询服务有限公司

Logitech G302 Daedalus Prime Setup Guide 设置指南

如何查看 Cache Engine 缓存中有哪些网站 /URL

Command Dictionary CUSTOM

1. DWR 1.1 DWR 基础 概念 使用使用 DWR 的步骤. 1 什么是 DWR? Direct Web Remote, 直接 Web 远程 是一个 Ajax 的框架

OTAD Application Note

搭建最简单的 SpringMVC 框架 ( 使用 maven)

Chapter 11 SHANDONG UNIVERSITY 1

Apache Kafka 源码编译 Spark 大数据博客 -

TBarCode OCX Microsoft ActiveX compliant Barcode Control

Air Speaker. Getting started with Logitech UE Air Speaker. 快速入门罗技 UE Air Speaker. Wireless speaker with AirPlay. 无线音箱 (AirPlay 技术 )

Triangle - Delaunay Triangulator

PTZ PRO 2. Setup Guide 设置指南

Technology: Anti-social Networking 科技 : 反社交网络

IPC 的 Proxy-Stub 设计模式 ( c)

<properties> <jdk.version>1.8</jdk.version> <project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties>

Chapter 1 (Part 2) Introduction to Operating System

nbns-list netbios-type network next-server option reset dhcp server conflict 1-34

Jbuilder 2007 开发 EJB3.0 Entity 罗代均 ldj_work#126.com 2007 年 8 月

Epetra_Matrix. August 14, Department of Science and Engineering Computing School of Mathematics School Peking University

#MDCC Swift 链式语法应 用 陈乘

Supplementary Materials on Semaphores

mod_callcenter callcenter.conf.xml 范例 odbc-dsn

CHINA VISA APPLICATION CONCIERGE SERVICE*

The Design of Everyday Things

1. Spring 整合 Jdbc 进行持久层开发

上汽通用汽车供应商门户网站项目 (SGMSP) User Guide 用户手册 上汽通用汽车有限公司 2014 上汽通用汽车有限公司未经授权, 不得以任何形式使用本文档所包括的任何部分

我们应该做什么? 告知性分析 未来会发生什么? 预测性分析 为什么会发生 诊断性分析 过去发生了什么? 描述性分析 高级分析 传统 BI. Source: Gartner

Skill-building Courses Business Analysis Lesson 3 Problem Solving

Wireless Presentation Pod

第二小题 : 逻辑隔离 (10 分 ) OpenFlow Switch1 (PC-A/Netfpga) OpenFlow Switch2 (PC-B/Netfpga) ServerB PC-2. Switching Hub

public static InetAddress getbyname(string host) public static InetAddress getlocalhost() public static InetAddress[] getallbyname(string host)

Ganglia 是 UC Berkeley 发起的一个开源集群监视项目, 主要是用来监控系统性能, 如 :cpu mem 硬盘利用率, I/O 负载 网络流量情况等, 通过曲线很容易见到每个节点的工作状态, 对合理调整 分配系统资源, 提高系统整体性能起到重要作用

XML allows your content to be created in one workflow, at one cost, to reach all your readers XML 的优势 : 只需一次加工和投入, 到达所有读者的手中

Packaging 10Apr2012 Rev V Specification MBXL HSG 1. PURPOSE 目的 2. APPLICABLE PRODUCT 适用范围

VAS 5054A FAQ ( 所有 5054A 整合, 中英对照 )

IPC 的 Proxy-Stub 设计模式 (b)

Logitech ConferenceCam CC3000e Camera 罗技 ConferenceCam CC3000e Camera Setup Guide 设置指南

OpenCascade 的曲面.

Safe Memory-Leak Fixing for C Programs

NyearBluetoothPrint SDK. Development Document--Android

Software Engineering. Zheng Li( 李征 ) Jing Wan( 万静 )

XPS 8920 Setup and Specifications

libde265 HEVC 性能测试报告

CloudStack 4.3 API 开发指南!

FLIGHT INSTRUMENT PANEL

U-CONTROL UMX610/UMX490/UMX250. The Ultimate Studio in a Box: 61/49/25-Key USB/MIDI Controller Keyboard with Separate USB/Audio Interface

密级 : 博士学位论文. 论文题目基于 ScratchPad Memory 的嵌入式系统优化研究

Chapter 2: Java OO II. Yang Wang wyang AT njnet.edu.cn

漂亮的测试. By Alberto Savoia 代码之美 第七章 史际帆

Apache OpenWhisk + Kubernetes:

NetScreen 概念与范例. ScreenOS 参考指南 第 7 卷 : 虚拟系统. ScreenOS 编号 SC 修订本 E

<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <!--- global properties --> <property>

Color LaserJet Pro MFP M477 入门指南

Build a Key Value Flash Disk Based Storage System. Flash Memory Summit 2017 Santa Clara, CA 1

Decode Zend. Darkness/Airsupply

Spark Standalone 模式应用程序开发 Spark 大数据博客 -

菜鸟调错 原文出处 : 菜鸟调错作者 : 刘水镜 本系列文章经作者授权在看云整理发布, 未经作者允许, 请勿转载! 菜鸟调错 分享开发中遇到的各种各样的错误, 以及解决方法, 让更多的人少走同样的弯路 本文档使用看云构建

2. Introduction to Digital Media Format

DEV Office 客户端开发增强

CA Application Performance Management

测试基础架构 演进之路. 茹炳晟 (Robin Ru) ebay 中国研发中心

display portal server display portal user display portal user count display portal web-server

Cisco IP Phone 8800 系列和 Cisco IP Conference Phone 8832 多平台 电话设置指南

¼'' TS 法律声明 10. MUSIC Tribe

S 1.6V 3.3V. S Windows 2000 Windows XP Windows Vista S USB S RGB LED (PORT1 PORT2 PORT3) S I 2 C. + 表示无铅 (Pb) 并符合 RoHS 标准 JU10 JU14, JU24, JU25

Multiprotocol Label Switching The future of IP Backbone Technology

XPages Extensibility API: Free Your Mind! 2011 IBM Corporation

WSV 让网站更加安全的几个小 妙招 徐栋 北京中达金桥技术服务有限公司

The relational Algebra & calculus cont. Reference : UC Berkeley

组播路由 - MSDP 和 PIM 通过走

Windows Batch VS Linux Shell. Jason Zhu

Command Dictionary -- DAMSTAB

Lecture 3 for pipelining

测试 SFTP 的 问题在归档配置页的 MediaSense

学习沉淀成长分享 EIGRP. 红茶三杯 ( 朱 SIR) 微博 : Latest update:

Parallel Programming Principle and Practice Lecture 7

MateCam X1 User Manual

武汉大学 学年度第 1 学期 多核架构及编程技术 试卷(A)

Python for Everybody. Exploring Data Using Python. Charles R. Severance

Lesson 20 Microcontroller Integrated Circuit With Read Only Memory

Specifications 产品规格书 USB-D M X 控制器. DESCRIPTION: USB-D M X Control 产品名称 : MODEL NO: USB-DMX512-CONTROL 产品型号 :

Congestion Control Mechanisms for Ad-hoc Social Networks 自组织社会网络中的拥塞控制机制

Grandstream Networks, Inc.

Autodesk Backburner 2011 安装手册

Keygen Codes For Photoshop Cs6 ->>> DOWNLOAD

: Operating System 计算机原理与设计

RPG IV 新特性概览. 钟嘉田 DB2 for i Performance, IBM 中国系统与科技研发中心

Open Office Pdf Editor Extension Download ->>> DOWNLOAD

Transcription:

三 依赖注入 (dependency injection) 的学习 EJB 3.0, 提供了一个简单的和优雅的方法来解藕服务对象和资源 使用 @EJB 注释, 可以将 EJB 存根对象注入到任何 EJB 3.0 容器管理的 POJO 中 如果注释用在一个属性变量上, 容器将会在它被第一次访问之前赋值给它 在 Jboss 下一版本中 @EJB 注释从 javax.annotation 包移到了 javax.ejb 下面的例子演示了怎样把 HelloWorldBean 无状态 session bean 的存根注入到 InjectionBean 类中 InjectionBean.java import com.foshanshop.ejb3.helloworld; import com.foshanshop.ejb3.injection; import javax.annotation.ejb; import javax.ejb.remote; @Stateless @Remote ({Injection.class) @EJB (beanname="helloworldbean") HelloWorld helloworld; public String SayHello() { return helloworld.sayhello(" 注入者 "); @EJB 注释的 beanname 属性指定 EJB 的类名 ( 不带包名 ), 他的另一个属性 mappedname 指定 Bean 实例的 JNDI 名 下面的片断演示了如何使用 beanname 或 mappedname 属性查找 HelloWorldBean 会话 bean @EJB (beanname="helloworldbean") //@EJB (mappedname="helloworldbean/remote") HelloWorld helloworld;.. @EJB 注释如果被用在 JavaBean 风格的 setter 方法上时, 容器会在属性第一次使用之前, 自动地用正确的参数调用 bean 的 setter 方法 下面的片断演示了这是如何做的 HelloWorld helloworld; @EJB (beanname="helloworldbean") public void sethelloworld(helloworld helloworld) {

this.helloworld = helloworld;.. 下面是 InjectionBean 的 Remote 业务接口 Injection.java package com.foshanshop.ejb3; public interface Injection { public String SayHello(); 下面是 Session Bean 的 JSP 客户端代码 : InjectionTest.jsp <%@ page contenttype="text/html; charset=gbk"%> <%@ page import="com.foshanshop.ejb3.injection, javax.naming.*, java.util.properties"%> <% Properties props = new Properties(); props.setproperty("java.naming.factory.initial", "org.jnp.interfaces.namingcontextfactory"); props.setproperty("java.naming.provider.url", "localhost:1099"); props.setproperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); InitialContext ctx; try { ctx = new InitialContext(props); Injection injection = (Injection) ctx.lookup("injectionbean/remote"); out.println(injection.sayhello()); catch (NamingException e) { out.println(e.getmessage()); %> 在 TONGWEB5.0 方面的改造 : HelloWorldBean 无状态 session bean 的存根注入到 InjectionBean 类中 @EJB (beanname="hellobean") @EJB 注释的 beanname 属性指定 EJB 的类名 ( 不带包名 ), 实现例子 InjectionBean.java: import com.foshanshop.ejb3.injection; import com.foshanshop.ejb3.localhello; import javax.ejb.ejb; import javax.ejb.remote;

@Stateless @Remote (Injection.class) @EJB (beanname="hellobean") LocalHello helloworld; public String SayHello() { return helloworld.sayhello(" 注入者 "); HelloManBean.java 如下 : import javax.ejb.local; import com.foshanshop.ejb3.localhello; @Stateless @Local(LocalHello.class) public class HelloManBean implements LocalHello { public String SayHello(String name) { return name+" 说,HelloManBean 你好!"; HelloBean 如下 : import javax.ejb.local; import javax.ejb.remote; import com.foshanshop.ejb3.localhello; import com.foshanshop.ejb3.remotehello; @Stateless @Remote (RemoteHello.class) @Local(LocalHello.class) public class HelloBean implements RemoteHello, LocalHello {

public String SayHello(String name) { return name+" 说,HelloBean 你好!"; 下面是 Session Bean 的 JSP 客户端代码 : InjectionTest.jsp <%@ page contenttype="text/html; charset=gbk"%> <%@ page import="com.foshanshop.ejb3.injection, javax.naming.*, java.util.properties"%> <% Properties props = new Properties(); props.setproperty("java.naming.factory.initial","com.tongweb.naming.serialinitconte xtfactory"); props.setproperty("java.naming.factory.url.pkgs","com.tongweb.naming"); props.setproperty("java.naming.factory.state","com.sun.corba.ee.impl.presentation.r mi.jndistatefactoryimpl"); props.setproperty("org.omg.corba.orbinitialhost","168.1.90.6"); props.setproperty("org.omg.corba.orbinitialport","5100"); InitialContext ctx; try { ctx = new InitialContext(props); //Injection injection = (Injection) ctx.lookup("injectionbean/remote"); Injection injection = (Injection) ctx.lookup("com.foshanshop.ejb3.injection"); out.println(injection.sayhello()); catch (NamingException e) { out.println(e.getmessage()); %> TONGWEB5.0 中依赖注入 BEAN 例子 InjectionBean 和 HelloBean 写法是一致的, 只是 Session Bean 的 JSP 客户端代码有改进 1 调用的 Properties 不同, JBOSS 调用方式 : Properties props = new Properties(); props.setproperty("java.naming.factory.initial", "org.jnp.interfaces.namingcontextfactory"); props.setproperty("java.naming.provider.url", "localhost:1099"); props.setproperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); TWEB5.0 调用方式 :

Properties props = new Properties(); props.setproperty("java.naming.factory.initial","com.tongweb.naming.serialinitcont extfactory"); props.setproperty("java.naming.factory.url.pkgs","com.tongweb.naming"); props.setproperty("java.naming.factory.state","com.sun.corba.ee.impl.presentation. rmi.jndistatefactoryimpl"); props.setproperty("org.omg.corba.orbinitialhost","168.1.90.6"); props.setproperty("org.omg.corba.orbinitialport","5800"); 2 ctx.lookup REMOTE 方式不同 : JBOSS lookup ctx.lookup("injectionbean/remote "); TONGWEB5.0 lookup ctx.lookup(com.foshanshop.ejb3.injection) 3 修改 tongweb-ejb-jar.xml <?xml version="1.0" encoding="utf-8" standalone="no"?> <!DOCTYPE tongweb-ejb-jar PUBLIC "-//TONGTECH,CO.LTD//DTD TWNS 1.0 EJB 3.0//EN" "http://www.tongtech.com/j2ee/dtds/tongweb-ejb-jar_3_0-1.dtd"> <tongweb-ejb-jar> <enterprise-beans> <unique-id>0</unique-id> <ejb> <ejb-name>hellobean</ejb-name> <jndi-name>com.foshanshop.ejb3.remotehello</jndi-name> <local-jndi-name>com.foshanshop.ejb3.localhello</local-jndi-name> <pass-by-reference>false</pass-by-reference> <is-read-only-bean>false</is-read-only-bean> <refresh-period-in-seconds>-1</refresh-period-in-seconds> <cmt-timeout-in-seconds>0</cmt-timeout-in-seconds> <gen-classes/> </ejb> <ejb> <ejb-name>hellomanbean</ejb-name> 把 <local-jndi-name>com.foshanshop.ejb3.localhello</local-jndi-name> 修改为 : <local-jndi-name>com.foshanshop.ejb3.localhello1</local-jndi-name> 否则部署出错, 原因 JNDI 的名字重复了 <pass-by-reference>false</pass-by-reference> <is-read-only-bean>false</is-read-only-bean> <refresh-period-in-seconds>-1</refresh-period-in-seconds> <cmt-timeout-in-seconds>0</cmt-timeout-in-seconds> <gen-classes/>

</ejb> <ejb> <ejb-name>injectionbean</ejb-name> <jndi-name>com.foshanshop.ejb3.injection</jndi-name> <pass-by-reference>false</pass-by-reference> <is-read-only-bean>false</is-read-only-bean> <refresh-period-in-seconds>-1</refresh-period-in-seconds> <cmt-timeout-in-seconds>0</cmt-timeout-in-seconds> <gen-classes/> </ejb> </enterprise-beans> </tongweb-ejb-jar> 附件是调试好的应用程序 : 结果 : 注入者说,HelloBean 你好!