《Pro Spring》学习笔记之Spring与JNDI

实验环境:JBoss 5.0 beta2

服务接口:

 

package  Jndi;

public   interface  MessageResolver  {
   
public String getMessage();
}

四种获取JNDI的实现类:

代码比较简单,不再啰唆了

package  Jndi;

import  javax.naming.Context;
import  javax.naming.NamingException;

import  org.springframework.jndi.JndiCallback;
import  org.springframework.jndi.JndiTemplate;

public   class  JndiCallBackMessageResolver  implements  MessageResolver  {

    
private String message;
    
public String getMessage() {
        StringBuffer buffer
=new StringBuffer();
        String temp[];
        
if(message==null){
            temp
=lookupMessage();
            
for (int i = 0; i < temp.length; i++{
                buffer.append(temp[i]);
            }

        }

        
return buffer.toString();
    }

    
    
public String[] lookupMessage(){
        JndiTemplate template
=new JndiTemplate();
        String temp[]
=new String[]{};
        String names[]
=new String[]{"java:comp/env/message1","java:comp/env/message2"};
        
try {
            temp
=(String[])template.execute(new BulkLoopupCallback(names));
        }
 catch (NamingException e) {
          System.out.println(e.getMessage());
        }

        
return temp;
    }

    
    
private class BulkLoopupCallback implements JndiCallback{
        
private String[] names;
        
public BulkLoopupCallback(String names[]){
            
this.names=names;
        }

        
public Object doInContext(Context ctx) throws NamingException{
            String messages[]
=new String[names.length];
            
for (int i = 0; i < names.length; i++{
                messages[i]
=(String)ctx.lookup(names[i]);
            }

            
return messages;
        }

        
    }


}

 

 

package  Jndi;

public   class  JndiObjectFactoryBeanMessageResolver  implements  MessageResolver  {
    
private String message;
    
public void setMessage(String message) {
        
this.message = message;
    }

    
public String getMessage() {
    
        
return message;
    }


}

 

 

 

package  Jndi;

import  javax.naming.Context;
import  javax.naming.InitialContext;
import  javax.naming.NamingException;

public   class  TraditionalJNDIMessageResolver  implements  MessageResolver  {
    
    
private String message;
    
public String getMessage() {
        
if(message==null){
            lookupMessage();
        }

        
return message;
    }


    
public String lookupMessage(){
        Context ctx;
        
try {
            ctx 
= new InitialContext();
            message
=(String)ctx.lookup("java:comp/env/message");    
        }
 catch (NamingException e) {
          System.out.println(e.getMessage());
        }

        
return message;
    }

}

 

 

package  Jndi;

import  javax.naming.Context;
import  javax.naming.InitialContext;
import  javax.naming.NamingException;

import  org.springframework.jndi.JndiTemplate;

public   class  JndiTemplateMessageResolver   implements  MessageResolver {

    
private String message;
    
public String getMessage() {
        
if(message==null){
            lookupMessage();
        }

        
return message;
    }

    
    
public String lookupMessage(){
        JndiTemplate template
=new JndiTemplate();
        
try {
            message
=(String)template.lookup("java:comp/env/message");
        }
 catch (NamingException e) {
          System.out.println(e.getMessage());
        }

        
return message;
    }


}

 

Servlet

 

package  Jndi;

import  java.io.IOException;
import  java.io.PrintWriter;

import  javax.servlet.ServletException;
import  javax.servlet.http.HttpServlet;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;

public   class  JndiDemoServlet  extends  HttpServlet  {

    
private ApplicationContext context;
    
public JndiDemoServlet() {
        
super();
    }


    
public void destroy() {
        
super.destroy(); // Just puts "destroy" string in log
        
// Put your code here
    }



    
public void doGet(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {
        String message1
=((MessageResolver)context.getBean("traditionalJNDIMessageResolver")).getMessage();
        String message2
=((MessageResolver)context.getBean("jndiTemplateMessageResolver")).getMessage();
        String message3
=((MessageResolver)context.getBean("jndiObjectFactoryBeanMessageResolver")).getMessage();
        
        String message4
=((MessageResolver)context.getBean("jndiCallBackMessageResolver")).getMessage();
        
        response.setContentType(
"text/html");
        PrintWriter out 
= response.getWriter();
        out.println(
"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
        out.println(
"<HTML>");
        out.println(
"  <BODY>");
        out.print(
"jndiTranditional:"+message1+"<br><br>");
        out.print(
"jndiJndiTemplate:"+message2+"<br><br>");
        out.print(
"jndiObjectFactory:"+message3+"<br><br>");
        out.print(
"jndiCallBakc:"+message4+"<br><br>");
        out.println(
"  </BODY>");
        out.println(
"</HTML>");
        out.flush();
        out.close();
    }


    
public void doPost(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {

         doGet(request,response);
    }


    
public void init() throws ServletException {
        context
=new ClassPathXmlApplicationContext("WEB-INF/applicationContext.xml");
    }


}

 

spring配置文件:

使用JndiObjectFactoryBean的时候,要为resourceRef注入属性true,通知JndiObjectFactoryBean属性是保存在应用程序环境中的,因此如果需要,应该使用java:comp/env的前缀读取JNDI

<? 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-2.0.xsd" >
   
<!--  传统获取jndi  -->
   
< bean  id ="traditionalJNDIMessageResolver"  class ="Jndi.TraditionalJNDIMessageResolver" >
   
</ bean >
   
<!--  使用jndiTemplate获取jndi  -->
   
< bean  id ="jndiTemplateMessageResolver"  class ="Jndi.JndiTemplateMessageResolver" >
   
</ bean >
   
<!--  使用jndiObjectFactory获取jndi  -->
    
< bean  id ="jndiObjectFactoryBeanMessageResolver"  class ="Jndi.JndiObjectFactoryBeanMessageResolver" >
      
< property  name ="message" >
        
< bean  class ="org.springframework.jndi.JndiObjectFactoryBean" >
          
< property  name ="jndiName" >
            
< value > message </ value >
          
</ property >
          
< property  name ="resourceRef" >
            
< value > true </ value >
          
</ property >
        
</ bean >
      
</ property >
   
</ bean >
   
<!--  使用JndiCallBakc  -->
    
< bean  id ="jndiCallBackMessageResolver"  class ="Jndi.JndiCallBackMessageResolver" >
   
</ bean >
</ beans >

 

web.xml(配置jndi数据)

 

<? xml version="1.0" encoding="UTF-8" ?>
< web-app  version ="2.4"  
    xmlns
="http://java.sun.com/xml/ns/j2ee"  
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
  
< servlet >
    
< servlet-name > JndiDemoServlet </ servlet-name >
    
< servlet-class > Jndi.JndiDemoServlet </ servlet-class >
  
</ servlet >

  
< servlet-mapping >
    
< servlet-name > JndiDemoServlet </ servlet-name >
    
< url-pattern > /servlet/JndiDemoServlet </ url-pattern >
  
</ servlet-mapping >
  
< welcome-file-list >
    
< welcome-file > index.jsp </ welcome-file >
  
</ welcome-file-list >
  
  
< env-entry >
    
< env-entry-name > message </ env-entry-name >
    
< env-entry-type > java.lang.String </ env-entry-type >
    
< env-entry-value > hello world </ env-entry-value >
  
</ env-entry >
  
  
< env-entry >
    
< env-entry-name > message1 </ env-entry-name >
    
< env-entry-type > java.lang.String </ env-entry-type >
    
< env-entry-value > hello my  </ env-entry-value >
  
</ env-entry >
  
< env-entry >
    
< env-entry-name > message2 </ env-entry-name >
    
< env-entry-type > java.lang.String </ env-entry-type >
    
< env-entry-value > lovely world </ env-entry-value >
  
</ env-entry >
</ web-app >

 

打开浏览器,运行http://localhost:8080/ProSpringStudyWeb/servlet/JndiDemoServlet

结果如下:

jndiTranditional:hello world

jndiJndiTemplate:hello world

jndiObjectFactory:hello world

jndiCallBakc:hello mylovely world

你可能感兴趣的:(《Pro Spring》学习笔记之Spring与JNDI)