Скачать презентацию
Идет загрузка презентации. Пожалуйста, подождите
Презентация была опубликована 9 лет назад пользователемАнна Ширкова
1 Evgeniy Krivosheev Vyacheslav Yakovenko Last update: Feb, 2012 Spring Framework Module 4 – JNDI
2 2 Contents Spring integration with JNDI
3 3 Spring :: Integration with JNDI Basic function of any naming service is to depict understandable object names (such as addresses, identifiers) and link them to objects that are generally used in software. For example, DNS translates computer names ( into IP addresses ( ).
4 4 Spring :: Integration with JNDI JNDI: Java Naming and Directory Interface JNDI is used for mapping the object names in the distributed system
5 5 Spring :: JNDI JNDI directory operations: // get the Initial Context Context ctx = new InitialContext(); // bind object String name = "mary"; String = ctx.bind(name, ); // lookup object String str = (String) ctx.lookup("mary"); // unbind object ctx.unbind(name);
6 6 Spring :: JNDI new InitialContext()
7 7 AB AB JNDI repository Name B_NAME to look for B Register in JNDI under the B_NAME Traditional approach: dependencies inside the code Service Locator (JNDI в JEE) pattern: objects in the repository IoC: objects know nothing about each other AB Application context - Creates A object - Initialize - Creates A object -Initialize and inject B object dependency class A { private B b; } class B { } Spring Framework :: JNDI vs. IoC / DI
8 8 Spring :: Integration with JNDI Generally, referencing to JNDI is needed when it is necessary to access DataSource and JtaTransactionManager. Spring provides rather simple and intuitive way of accessing them:
9 9 Spring :: Integration with JNDI However, JNDI support in Spring is not limited to this; Main classes used for work with JNDI in Spring are: –org.springframework.jndi.JndiTemplate –Interface org.springframework.jndi.JndiCallback –org.springframework.jndi.JndiObjectFactoryBean
10 10 Spring :: Integration with JNDI org.springframework.jndi.JndiTemplate: Responsible for object lookup, binding, rebinding, and unbind; Implements callback-based interface: – T execute(JndiCallback contextCallback) Example: JndiTemplate template = new JndiTemplate(); … template.bind("SomeKey", "SomeValue"); … String value = (String)template.lookup("SomeKey"); template.rebind("SomeKey", "SomeOtherValue");
11 11 Spring :: Integration with JNDI org.springframework.jndi.JndiCallback : For example, when you want to log JNDI operations you have to: –Create new class that will implement JndiCallback; –Move it to execute method from JndiTemplate;
12 12 Spring :: Integration with JNDI org.springframework.jndi.JndiCallback : JndiTemplate template = new JndiTemplate(); CustomJndiAccessForLogging callback = new CustomJndiAccessForLogging("key"); Object result = template.execute(callback); public class CustomJndiAccessForLogging implements JndiCallback { private String key; public CustomJndiAccessForLogging(String key) { this.key = key;} public Object doInContext(Context context) throws NamingException { System.out.println("Start lookup operation"); Object value = context.lookup(key); System.out.println("End lookup operation"); return value; }
13 13 Spring :: Integration with JNDI org.springframework.jndi.JndiObjectFactoryBean : Responsible for data retrieval from JNDI repositiry; Main strategy is to look up objects and cache them; Can be modified either declaratively or programmatically;
14 14 Spring :: Integration with JNDI org.springframework.jndi.JndiObjectFactoryBean : java:comp/env/welcomeMessage In InfoFromJndi bean the value for welcomeMessage field is taken from JNDI Attribute value for welcomeMessage is substituted for JndiObjectFactoryBean that gets its value from JNDI public class InfoFromJndi { String welcomeMessage; }
15 15 Any questions!?
Еще похожие презентации в нашем архиве:
© 2024 MyShared Inc.
All rights reserved.