• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

spring,ioc模式与ejb3调用

servlet/jsp 搞代码 7年前 (2018-06-18) 114次浏览 已收录 0个评论

我们先看看在servlet中怎么调用EJB3:
public class HelloServlet extends GenericServlet {
private Hello _hello;

public void setHello(Hello hello)
{

http://www.gaodaima.com/42279.htmlspring,ioc模式与ejb3调用

_hello = hello;
}

public void service(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
PrintWriter out = res.getWriter();

out.println(_hello.hello());
}
}
然后我们看看xml中的配置方法:
<web-app>
<servlet servlet-name="hello" servlet-class="com.hongsoft.HelloServlet">
<init hello="_ejb.HelloBean.HelloBean__EJB"/>
</servlet>
</web-app>
我们看到了什么?配置文件中把HelloBean传给了setHello作为参数,当然,HelloBean实

现了Hello接口;service中,直接调用_hello.hello(),实际调用的是HelloBean的

hello()方法实现.
呵呵,通过set方法来将需要的HelloBean传递给servlet,如果我们的需求发生了改变,

新加了Hello2Bean实现,我们只需要在配置文件中进行修改就可以了,这,就是ioc模式

中的type2.
Spring Framework采用的也是type2的ioc模式,比如spring配置文件如下:
<beans>
<bean id=“studentType1" class=“com.hongsoft.test.StudentType1"/>
<bean id=“teacher“ class=“com.hongsoft.test.Teacher">
<property name=“student">
<ref bean=“studentType1"/>
</property>
</bean>
</beans>
那么我们在代码中就可以这么使用:
public class Teacher {

private Student student;

public void setStudent(Student student) {
this.student = student;
}
//写论文方法
public void writePaper() {
student.writePaper();
}
}
这样,如果要写IOC的论文,就找一个精通IOC的学生写;如果要写BPEL的论文,就找一个

精通BPEL的学生写,要修改的地方,也就是配置文件而已.

欢迎大家阅读《spring,ioc模式与ejb3调用》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:spring,ioc模式与ejb3调用

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址