Guice真的无法享受企业级组件吗

fangcloud 690 2022-08-04

本文转载自网络公开信息

Guice真的无法享受企业级组件吗

Guice真的无法享受企业级组件吗,炮轰Guice的占绝大多数。但如果Guice能整合Spring,那么我们似乎可以做很多有意义的事了。那么开始Spring整合之旅吧。不过crazybob在整合方面极不配合,就给了我们一个单元测试类,然后让我们自力更生。好在Guice本身足够简单。  首先还是来一个最简单无聊的HelloWorld整合吧。 HelloWorld.javaJava代码 package com.leo.domain;     public class HelloWorld {         public String sayHello(String str) {           return str;       }   }  package com.leo.domain; public class HelloWorld { public String sayHello(String str) { return str; } }   够简单了吧。  现在开始编写你的applicationContext.xml(如果不熟悉Spring的话,可能无法继续往下看)。为保持简洁,去掉了那一段DTD。  Xml代码         beans>     好了,开始与Guice整合吧。 Guice的核心就是com.google.inject.Module,它类似于Spring的bean工厂。 HelloWorldMyModule.java  Java代码 package com.leo.module;     import static com.google.inject.spring.SpringIntegration.fromSpring;     import org.springframework.beans.factory.BeanFactory;   import org.springframework.context.support.ClassPathXmlApplicationContext;     import com.google.inject.Binder;   import com.google.inject.Module;   import com.leo.domain.HelloWorld;     public class HelloWorldMyModule implements Module {       public void configure(Binder binder) {           final BeanFactory beanFactory = new ClassPathXmlApplicationContext(                   new String[] { "applicationContext.xml", "daoContext.xml" });           binder.bind(BeanFactory.class).toInstance(beanFactory);           binder.bind(HelloWorld.class).toProvider(                   fromSpring(HelloWorld.class, "helloWorld"));       }   }  package com.leo.module; import static com.google.inject.spring.SpringIntegration.fromSpring; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.google.inject.Binder; import com.google.inject.Module; import com.leo.domain.HelloWorld; public class HelloWorldMyModule implements Module { public void configure(Binder binder) { final BeanFactory beanFactory = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml", "daoContext.xml" }); binder.bind(BeanFactory.class).toInstance(beanFactory); binder.bind(HelloWorld.class).toProvider( fromSpring(HelloWorld.class, "helloWorld")); } }    其中:  Java代码 final BeanFactory beanFactory = new ClassPathXmlApplicationContext(                   new String[] { "applicationContext.xml", "daoContext.xml" });           binder.bind(BeanFactory.class).toInstance(beanFactory);  final BeanFactory beanFactory = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml", "daoContext.xml" }); binder.bind(BeanFactory.class).toInstance(beanFactory);    定义了Guice与Spring整合后,将spring工厂也由Guice托管。我感觉crazybob在与Spring整合事件上非常低调,给了个吓死人的单元测试,而且没有任何文档,这个单元测试里的bean都是临时动态注入进去的,不知道是因为出自对Spring XML配置的不满,还是根本没想到用Guice的人会去整合Spring,我个人感觉他在这一点上非常吝啬。 注意这一句: Java代码 binder.bind(HelloWorld.class).toProvider(                   fromSpring(HelloWorld.class, "helloWorld"));  binder.bind(HelloWorld.class).toProvider( fromSpring(HelloWorld.class, "helloWorld"));   这与普通Guice本身托管的Bean注入不一样,fromSpring很明显说明这个bean来自于spring,而且与helloWorld与applicationContext.xml中定义的那个是一致的。 好了,我们开始来个单元测试 HelloWorldTest.javaJava代码 package com.leo.service;     import junit.framework.TestCase;     import com.google.inject.Guice;   import com.google.inject.Inject;   import com.google.inject.Injector;   import com.leo.domain.HelloWorld;   import com.leo.module.HelloWorldMyModule;     /**   * @author superleo   */  public class HelloWorldTest extends TestCase {         @Inject      private HelloWorld helloWorld;         public void testHelloWorld() {           HelloWorldMyModule module = new HelloWorldMyModule(); // 建HelloWorldModule,生成Guice的bean工厂           HelloWorldTest test = new HelloWorldTest();           Injector in = Guice.createInjector(module);           in.injectMembers(test); // 将HelloWorldTest所依赖的helloWorld注入进去           System.out.println(test.helloWorld.sayHello("hey, hello-world"));       }   }  package com.leo.service; import junit.framework.TestCase; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; import com.leo.domain.HelloWorld; import com.leo.module.HelloWorldMyModule; /** * @author superleo */ public class HelloWorldTest extends TestCase { @Inject private HelloWorld helloWorld; public void testHelloWorld() { HelloWorldMyModule module = new HelloWorldMyModule(); // 建HelloWorldModule,生成Guice的bean工厂 HelloWorldTest test = new HelloWorldTest(); Injector in = Guice.createInjector(module); in.injectMembers(test); // 将HelloWorldTest所依赖的helloWorld注入进去 System.out.println(test.helloWorld.sayHello("hey, hello-world")); } }   代码还是非常简单。运行后,正常的结果会出来在控制台上。但是这段代码能说明什么,无非就是helloWorld不需要set,get方法,但还多加了一大堆什么Injector, Module之类的。实际上在Guice与Struts2整合后,这些硬编码是不会出现的,而且如果不是因为与Spring整合,连Module里面配置HelloWorld都不要。但不是有人说Guice无法使用企业级组件吗?就单纯一个DI,根本就是玩具,但大家想想Guice能通过Spring来应用这些企业级组件的话,Guice还是有优势的。因为它够简单,够快,够灵活,强类型,本身没有XML(与其它框架整合,不能保证别的框架没有),几乎没有学习曲线,文档就一个HTML。因此,我现在想给大家运用JavaMail,这个算企业级常用的组件了吧? 继续编写Spring的applicationContext.xml applicationContext.xml  Xml代码 xml version="1.0" encoding="UTF-8"?>  >

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表亿方云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱daifeng@360.cn 处理。
上一篇:word文档为什么一插入图表就显示”无法创建对象,请确认对象已在系统注册表中注册“?
下一篇:点击最大化后还是铺满整个桌面,我想让表格文件只显示桌面的一半大小
相关文章

 发表评论

暂时没有评论,来抢沙发吧~