Guice真的无法享受企业级组件吗,JavaEye里炮轰Guice的占绝大多数。但如果Guice能整合Spring,那么我们似乎可以做很多有意义的事了。那么开始Spring整合之旅吧。不过crazybob在整合方面极不配合,就给了我们一个单元测试类,然后让我们自力更生。好在Guice本身足够简单。
首先还是来一个最简单无聊的HelloWorld整合吧。
HelloWorld.java
package com.leo.domain;
public class HelloWorld {
public String sayHello(String str) {
return str;
}
}
够简单了吧。
现在开始编写你的applicationContext.xml(如果不熟悉Spring的话,可能无法继续往下看)。
为保持简洁,去掉了那一段DTD。
<beans> <bean id="helloWorld" class="com.leo.domain.HelloWorld" /> </beans>
好了,开始与Guice整合吧。
Guice的核心就是 com.google.inject.Module ,它类似于 Spring 的 bean 工厂。 HelloWorldMyModule.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"));
}
}
其中:
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 ,我个人感觉他在这一点上非常吝啬。 注意这一句:
binder.bind(HelloWorld.class).toProvider(
fromSpring(HelloWorld.class, "helloWorld"));
这与普通Guice本身托管的Bean注入不一样,fromSpring很明显说明这个bean来自于spring,而且与helloWorld与applicationContext.xml中定义的那个是一致的。
好了,我们开始来个单元测试
HelloWorldTest.java
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 version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- Java Mail --> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.163.com" /> <!-- 有些邮件服务器发送邮件需要认证,所以必须提供帐号 --> <property name="username" value="kyo100900@163.com" /> <property name="password" value="111111" /> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> <!--一个邮件模板(测试时使用)--> <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage"> <property name="to" value="superleo_cn@hotmail.com" /> <property name="from" value="kyo100900@163.com" /> <property name="subject" value="fdsfdsfsdfsd" /> </bean> </beans>
上面就是大家在 Spring 配置文件里写烂了的代码,别的什么整合代码同理,就不多说了。 这时候,我们写一个 JavaMailModule
JavaMailModule.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 org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import com.google.inject.Binder;
import com.google.inject.Module;
public class JavaMailModule implements Module {
public void configure(Binder binder) {
final BeanFactory beanFactory = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml"});
binder.bind(BeanFactory.class).toInstance(beanFactory);
binder.bind(SimpleMailMessage.class).toProvider(
fromSpring(SimpleMailMessage.class, "mailMessage"));
binder.bind(MailSender.class).toProvider(
fromSpring(JavaMailSenderImpl.class, "mailSender"));
}
}
和HelloWorld一样,只是分别换成了mailMessage, mailSender。写到这里大家肯定明白了,Guice与Spring整合其实非常非常简单。
写个测试吧:
JavaMail.java
package com.leo.util;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.leo.module.MyModuleSpring;
public class JavaMail {
@Inject
private MailSender mailSender;
@Inject
private SimpleMailMessage mailMessage;// 邮件模板
public void sendMail() {
System.out.println("-----------发送邮件!---------");
mailMessage.setText("send maiiiiiiiiiiiiiiiil~~~~~~~~~~~~~~~~~~");
try {
System.out.println(mailSender.toString());
mailSender.send(mailMessage);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("-----------发送成功!---------");
}
public static void main(String... strings) {
MyModuleSpring spring = new MyModuleSpring();
JavaMail mail = new JavaMail();
Injector in = Guice.createInjector(spring);
in.injectMembers(mail);
mail.sendMail();
}
}
只要用户名密码正确,没有设置什么代理,防火墙之类的,就能正常的运行,但大家如果测试的时候,请把电子邮件改改。至于说与Hibernate,事务等整合,已经出了wrap框架了,有兴趣可以看看robbin写的《Warp framework - 一个相当有前途的Java框架》。 因此Guice不是完全不可能使用所谓的企业级组件的,直接使用Spring就可很容易做到。那么在应用Guice的时候,我感觉可以单独建立一个SpringModule,将Guice无法做到的事通过Spring工厂来帮你组装,其它方面,比如说Struts2的Action或Service层之间的注入等,直接用@Inject就得了,不要那么多set,get,(Struts2需要放入到值栈的属性例外)我们项目大多数Action超过几千行了,光set,get就几百行。Spring配置文件估计拼在一起也有几千行,但所谓注入到的其它企业级组件还不到5个,剩下全是service,dao,action等等。就算Spring可以autowire,也不是解决问题的根本。
Guice当然也有缺点——太过于简单。不少JavaEE开发人员感觉似乎没有什么实际价值,更像是一种玩具。但我看好Guice的思想——简单而且类型安全。