Spring自动代理机制

(2008年08月20日)发表于BlogJava-首页技术区
     摘要: 我们一直使用ProxyFactoryBean来显式的创建AOP代理。但是在很多场合,这种方式将会使编写配置文件的工作量大大增加;由于要从ProxyFactoryBean获得代理对象,也会使应用和Spring之间的耦合度增加。下面介绍使用Spring提供的自动代理机制来解决这类问题。   阅读全文

残梦追月 2008-08-20 19:02 发表评论
阅读全文...
本站相关内容:

Spring自动代理机制

     摘要: 我们一直使用ProxyFactoryBean来显式的创建AOP代理。但是在很多场合,这种方式将会使编写配置文件的工作量大大增加;由于要从ProxyFactoryBean获得代理对象,也会使应用和Spring之间的耦合度增加。下面介绍使用Spring提供的自动代理机制来解决这类问题。   阅读全文

残梦追月 2008-08-17 22:19 发表评论

Spring学习笔记 2007-10-28 Spring实现代理机制

上一笔记,介绍了代理机制,这一节介绍Spring是如何实现代理。
通过一个例子来说明。
包下载地址(两个都要下):
http://www.blogjava.net/Files/ducklyl/springaop.rar
http://www.blogjava.net/Files/ducklyl/Spring.rar

(1)创建LogBeforeAdvice类(实现MethodBeforeAdvice接口,会在目标对象的方法执行之前被中呼叫)

package com.proxy;

import java.lang.reflect.*;
import java.util.logging.Logger;
import java.util.logging.Level;
import org.springframework.aop.MethodBeforeAdvice;;

public class LogBeforeAdvice  implements MethodBeforeAdvice{

 private Logger logger=Logger.getLogger(this.getClass().getName());
 public void before(Method method,Object[] args,Object target) throws Throwable
 {
  logger.log(Level.INFO,"mehtod starts "+method);
 }
}


(2)创建配置文件advice-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
 <bean id="logBeforeAdvice" class="com.proxy.LogBeforeAdvice" />
 <bean id="helloSpeaker" class="com.proxy.HelloSpeaker" />
 
 <bean id="helloProxy"
  class="org.springframework.aop.framework.ProxyFactoryBean"><!--建立代理对象-->
  <property name="proxyInterfaces"><!--代理接口-->
   <value>com.proxy.IHello</value>
  </property>
  <property name="target"><!--代理目标-->
   <ref bean="helloSpeaker" />
  </property>
  <property name="interceptorNames"><!--代理实现类-->
   <list>
    <value>logBeforeAdvice</value>
   </list>
  </property>
 </bean>
 
</beans>
(3)测试类SpringAOPDemo

package com.proxy;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class SpringAOPDemo {
 public static void main(String[] args)
 {
  //读取配置文件
  ApplicationContext context=new FileSystemXmlApplicationContext("advice-config.xml");
  IHello helloProxy=(IHello)context.getBean("helloProxy");
  helloProxy.hello("ducklyl");
  
 }
}

运行测试类,结果如下:
Hello,ducklyl



ducklyl 2007-10-28 17:47 发表评论

Spring Autowiring自动装配

     摘要: 残梦追月原创,转载请注明。

在应用中,我们常常使用 标签为JavaBean注入它依赖的对象。但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的 标签。实际上,这种方式也会在另一种形式上增加了应用程序的复杂性,那么如何解决这个问题呢?Spring为我们提供了一个自动装配的机制,尽管这种机制不是很完善,但是在应用中结合 标签还是可以大大的减少我们的劳动强度。前面提到过,在定义Bean时, 标签有一个autowire属性,我们可以通过指定它来让容器为受管JavaBean自动注入依赖对象。

   阅读全文

残梦追月 2008-07-20 15:13 发表评论

Spring+Struts2+Hibernate,tomcat5.5不定时自动关闭


作者: liuwei1981  链接: http://liuwei1981.javaeye.com/blog/203513  发表时间: 2008年06月15日

声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

    最近完成一个网站,使用了Spring+Struts2+Hibernate3.2框架,spring整合了struts2和hibernate,由spring管理struts的Action类和heibernate.

   在项目完成后,部署上线,在前几天运转正常,但在昨天,突然发现tomcate服务器会不定时的自动关闭,查看日志,没有发生大的错误,但服务器就是莫名其妙的自动关闭了.

   仔细查看日志,发现了这样一段:

  

 Closing Spring root WebApplicationContext
 support.XmlWebApplicationContext (doClose:823) - 
Closing org.springframework.web.context.support.XmlWebApplicationContext
@1a6684f: display name [Root WebApplicationContext]; startup date [Sat Jun 14 14:32:40 CST 2008]; 
root of context hierarchy
support.DefaultListableBeanFactory (destroySingletons:340) - 
Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory
@24de7d: defining beans [dataSource,sessionFactory,
transactionManager,org.springframework.aop.config.internalAutoProxyCreator,
ServiceMethods,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0,
txAdvice,hibernateTemplate,commonService,
baseService,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#1,
purviewService,staticService,complainService,userService,linkService,departmentService,
sequenceService,newsService,categoryService,imageService,GovpubService,TypeService,
WorkGuideService,ApplyService,AddonsService,
QyxxService,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#2,
mailSender,mailMessage,mailService,indexAction,newsAction,complainAction,complainMngAction,
staticAction,loginAction,linkAction,sysmngAction,movmentAction,creditPubAction,speTopicAction,
partPoliticAction,conAlertAction,govCultureAction,workStudyAction,departmentAction,userAction,
imageAction,noticeAction,GovpubAction,OfficeAction,WorkGuideAction,GovpubIndexAction,ApplyAction,
qyxxAction,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#3,newsDwr,
categoryDwr,deptDwr,userDwr,linkDwr]; root of factory hierarchy
hibernate3.LocalSessionFactoryBean (destroy:981) - Closing Hibernate SessionFactory
impl.SessionFactoryImpl (close:769) - closing

 显然是spring自动关闭了web容器,并将其管理的hibernate的session关闭,日志最后的关闭的bean类都是网站配置使用的action和service类.

 

   在google了这方面的问题,没有找到类似的,只有在javaeye上找到一篇问题相同的提问blog,没有人留言解答.十分郁闷.

   初步猜测,是服务器资源紧张导致tomcat自动关闭,在其中的一个service类中,找到了一个查询方法,使用了synchronized修饰关键字,这个方法根据用户输入的条件数据,大概就几万的数据量,且该功能在网站上使用非常频繁,是不是这个原因导致资源用尽服务器自动关闭?

   把方法都去掉这个修饰关键字,网站运行没有发生以上错误,但发生错误的环境已经不再了(发生错误的时候,网站后台用户正在频繁点击录入数据),不知道是否解决这个问题,还会不会再次发生.

 

   不知道有没有人遇到在ssh开发框架web应用中,发生过tomcat莫名关闭的问题,希望有哪位大虾给予回答.

  


本文的讨论也很精彩,浏览讨论>>


JavaEye推荐



学习Spring(六) 自动装配 autowire

今天来整理一下Spring的自动装配 autowire一节,在这里我们要解决以下问题: §1 什么是自动装配? §2 自动装配的意义? §3 自动装配有几种类型? §4 如何启用自动装配? §5 自动装配将引发的问题? §1 什么是自动装配? Spring reference 写道The Spring container is able to autowire relationships between collaborating beans. This means that it is pos ... [阅读全文]

已有 0 人发表留言,猛击->> 这里<<-参与讨论


JavaEye推荐



互联网相关内容:
Spring自动代理机制 (2008年08月17日)
Spring学习笔记 2007-10-28 Spring实现代理机制 (2007年10月28日)
Spring Autowiring自动装配 (2008年07月20日)
Spring+Struts2+Hibernate,tomcat5.5不定时自动关闭 (2008年06月15日)
学习Spring(六) 自动装配 autowire (2009年03月02日)
spring与自动调度任务 (2009年03月10日)
spring与自动调度任务(二) (2009年03月11日)
Spring自动装配的学习 (2009年04月03日)
JAVA代理机制初探 (2007年09月08日)