在此之前,我已经接触到了DataSource和JTA事务了,我们都应用到了JNDI技术;在Spring中提供了JNDI技术的整合支持,JNDI如何使用,我就不累赘了,具体我们来看一下整合的源代码:
代码类结构:
我们从上而下进行分析;
首先是:
JndiTemplate:
jndiTemplate提供了对JNDI服务器的vjndi对象的绑定,查询,撤销绑定和重新绑定,从一定意义上来说他跟JdbcTemplate等是同意概念的类,直接与资源管理器进行交互,并且同样的是采用的回调机制,我们可以看见其依赖了JndiCallbake接口:
其对绑定,差性能,撤销,重新绑定的实现的源代码如下:
这段代码生成了上下文,进而把上下文传递给了JndiCallBack,在这个接口的回调方法,我们直接使用context对资源管理器进行curd交互;
绑定对象:
重新绑定:
撤销绑定:
通过JndiTemplate,我们实现了底层访问代码;形成了具体实现层;与上层开来;
JndiAccessor:只是单纯的对JndiTemplate进行了包装,隔离了底层的实现细节;
JndiLocatorSupport的主要扩展的功能是,如果我们在配置JndiObjectFactoryBean的时候,配置了参数:resourceRef为true的话,那么就会如果我们查找的资源中间不包含:或者java:等前缀,我们会自动的会该路径加上java:comp/env,主要是匹配在j2ee容器里面的jndi资源的查找;
最后,我们再来看一下:
在JndiObjectFactoryBean我们实际上得到的是:
jndiObject :
初始化生成如下:
当proxyTnterface不为空的时候
this .jndiObject = JndiObjectProxyFactory.createJndiObjectProxy( this );
生成了一个从Jndi目录中取得了的对象的代理类:
否则直接将从目录服务器中得到的对象暴露出来;如果查找出现异常,将会把DefaultObject暴露出来;
代码类结构:
我们从上而下进行分析;
首先是:
JndiTemplate:
jndiTemplate提供了对JNDI服务器的vjndi对象的绑定,查询,撤销绑定和重新绑定,从一定意义上来说他跟JdbcTemplate等是同意概念的类,直接与资源管理器进行交互,并且同样的是采用的回调机制,我们可以看见其依赖了JndiCallbake接口:
其对绑定,差性能,撤销,重新绑定的实现的源代码如下:
- public Object execute(JndiCallback contextCallback) throws NamingException {
- Context ctx = createInitialContext();
- try {
- return contextCallback.doInContext(ctx);
- }
- finally {
- try {
- ctx.close();
- }
- catch (NamingException ex) {
- logger.debug("Could not close JNDI InitialContext", ex);
- }
- }
- }
- protected Context createInitialContext() throws NamingException {
- return new InitialContext(getEnvironment());
- }
这段代码生成了上下文,进而把上下文传递给了JndiCallBack,在这个接口的回调方法,我们直接使用context对资源管理器进行curd交互;
绑定对象:
- public Object lookup(final String name) throws NamingException {
- if (logger.isDebugEnabled()) {
- logger.debug("Looking up JNDI object with name [" + name + "]");
- }
- return execute(new JndiCallback() {
- public Object doInContext(Context ctx) throws NamingException {
- Object located = ctx.lookup(name);
- if (located == null) {
- throw new NameNotFoundException(
- "JNDI object with [" + name + "] not found: JNDI implementation
- returned null");
- }
- return located;
- }
- });
- }
- public void bind(final String name, final Object object) throws NamingException {
- if (logger.isDebugEnabled()) {
- logger.debug("Binding JNDI object with name [" + name + "]");
- }
- execute(new JndiCallback() {
- public Object doInContext(Context ctx) throws NamingException {
- ctx.bind(name, object);
- return null;
- }
- });
- }
- public void rebind(final String name, final Object object) throws NamingException {
- if (logger.isDebugEnabled()) {
- logger.debug("Rebinding JNDI object with name [" + name + "]");
- }
- execute(new JndiCallback() {
- public Object doInContext(Context ctx) throws NamingException {
- ctx.rebind(name, object);
- return null;
- }
- });
- }
撤销绑定:
- public void unbind(final String name) throws NamingException {
- if (logger.isDebugEnabled()) {
- logger.debug("Unbinding JNDI object with name [" + name + "]");
- }
- execute(new JndiCallback() {
- public Object doInContext(Context ctx) throws NamingException {
- ctx.unbind(name);
- return null;
- }
- });
- }
- }
JndiAccessor:只是单纯的对JndiTemplate进行了包装,隔离了底层的实现细节;
JndiLocatorSupport的主要扩展的功能是,如果我们在配置JndiObjectFactoryBean的时候,配置了参数:resourceRef为true的话,那么就会如果我们查找的资源中间不包含:或者java:等前缀,我们会自动的会该路径加上java:comp/env,主要是匹配在j2ee容器里面的jndi资源的查找;
- protected String convertJndiName(String jndiName) {
- // Prepend container prefix if not already specified and no other scheme given.
- if (isResourceRef() && !jndiName.startsWith(CONTAINER_PREFIX) && jndiName.indexOf(':') == -1) {
- jndiName = CONTAINER_PREFIX + jndiName;
- }
- return jndiName;
- }
在JndiObjectFactoryBean我们实际上得到的是:
jndiObject :
初始化生成如下:
- public void afterPropertiesSet() throws IllegalArgumentException, NamingException {
- super.afterPropertiesSet();
- if (this.proxyInterface != null) {
- if (this.defaultObject != null) {
- throw new IllegalArgumentException(
- "'defaultObject' is not supported in combination with 'proxyInterface'");
- }
- // We need a proxy and a JndiObjectTargetSource.
- this.jndiObject = JndiObjectProxyFactory.createJndiObjectProxy(this);
- }
- else {
- if (!this.lookupOnStartup || !this.cache) {
- throw new IllegalArgumentException(
- "Cannot deactivate 'lookupOnStartup' or 'cache' without specifying a 'proxyInterface'");
- }
- if (this.defaultObject != null && getExpectedType() != null &&
- !getExpectedType().isInstance(this.defaultObject)) {
- throw new IllegalArgumentException("Default object [" + this.defaultObject +
- "] of type [" + this.defaultObject.getClass().getName() +
- "] is not of expected type [" + getExpectedType().getName() + "]");
- }
- // Locate specified JNDI object.
- this.jndiObject = lookupWithFallback();
- }
- }
当proxyTnterface不为空的时候
this .jndiObject = JndiObjectProxyFactory.createJndiObjectProxy( this );
生成了一个从Jndi目录中取得了的对象的代理类:
- private static class JndiObjectProxyFactory {
- private static Object createJndiObjectProxy(JndiObjectFactoryBean jof) throws NamingException {
- // Create a JndiObjectTargetSource that mirrors the JndiObjectFactoryBean's configuration.
- JndiObjectTargetSource targetSource = new JndiObjectTargetSource();
- targetSource.setJndiTemplate(jof.getJndiTemplate());
- targetSource.setJndiName(jof.getJndiName());
- targetSource.setExpectedType(jof.getExpectedType());
- targetSource.setResourceRef(jof.isResourceRef());
- targetSource.setLookupOnStartup(jof.lookupOnStartup);
- targetSource.setCache(jof.cache);
- targetSource.afterPropertiesSet();
- // Create a proxy with JndiObjectFactoryBean's proxy interface and the JndiObjectTargetSource.
- ProxyFactory proxyFactory = new ProxyFactory();
- proxyFactory.addInterface(jof.proxyInterface);
- proxyFactory.setTargetSource(targetSource);
- return proxyFactory.getProxy();
- }
- }