Spring Without IoC

FuqiangWang


2014年从msn space存档中重新恢复出来!

或许有的时候你并不想用spring的beanFactory,比如,你目前不做j2EE,而是做一般的java应用,并且,项目也已经启动了,并没有使用spring的IoC支持,这种情况下,你是否会怀念spring中提供的种种便利那?!

其实没有了spring的BeanFactory,你同样可以很容易的使用spring的种种便利,不要忘了,spring的BeanFactory组装的也仅仅是普通的pojo而已,现在只是通过一样的方式构造并使用它们就可以了。

比如,我们要在应用中使用spring提供的发送mail的API的支持,那么现在我们现在就可以这样写:

JavaMailSender mailSender = new JavaMailSenderImpl();
((JavaMailSenderImpl)mailSender).setHost("mail.livedoor.cn");
((JavaMailSenderImpl)mailSender).setUsername("[email protected]");
((JavaMailSenderImpl)mailSender).setPassword("458524033931");
// IF your Mail Server request SMTP authentication, you must set properties below,
// Otherwise, mail sending will fail.
Properties prop = new Properties();
prop.setProperty("mail.smtp.auth","true");
((JavaMailSenderImpl)mailSender).setJavaMailProperties(prop);

mailSender.send(new MimeMessagePreparator(){
public void prepare(MimeMessage message) throws Exception
{
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setTo("[email protected]");
helper.setFrom("[email protected]");
helper.setSubject("Spring Mail Test Without IoC");
helper.setText("Spring Mail is cool!");
}
});

或者,我们仅仅为了拦截某个对象的方法,而不是很普遍,那么我们也可以这样:

BusinessImpl impl = new BusinessImpl();
BusinessBeforeAdvice advice = new BusinessBeforeAdvice();
// construct advisor in Regexp Way
RegexpMethodPointcutAdvisor advisor = new RegexpMethodPointcutAdvisor();
advisor.setAdvice(advice);
advisor.setPattern(".*doSomething.*");

ProxyFactory factory = new ProxyFactory();
factory.setTarget(impl);
factory.addAdvisor(advisor);
IBusiness bo = (IBusiness)factory.getProxy();
bo.doSomething();

怎么样?!是不是很容易那?

仅仅是这几天实在想不出写些什么,故此闲话些许。


>>>>>> 更多阅读 <<<<<<


欢迎加入「福强私学」

跨越2190个日夜,始终坚持“实践 + 原创”打造的715125字专属知识库,囊括了(但不限于)从职场、技术、管理与商业等多个板块的内容。

  • 一个ChatGPT触达不到的地方
  • 一个带你超越AI/人工智能的地方
  • 一个与你一起成长的地方

https://afoo.me/kb.html


开天窗,拉认知,订阅「福报」,即刻拥有自己的全模态人工智能。

订阅「福报」