??xml version="1.0" encoding="utf-8" standalone="yes"?>国产亚洲一级,亚洲成人av在线电影,春暖花开成人亚洲区http://www.aygfsteel.com/dodoma/zh-cnTue, 17 Jun 2025 21:18:24 GMTTue, 17 Jun 2025 21:18:24 GMT60java虚拟?http://www.aygfsteel.com/dodoma/archive/2006/04/05/39445.htmldodomadodomaWed, 05 Apr 2006 10:25:00 GMThttp://www.aygfsteel.com/dodoma/archive/2006/04/05/39445.htmlhttp://www.aygfsteel.com/dodoma/comments/39445.htmlhttp://www.aygfsteel.com/dodoma/archive/2006/04/05/39445.html#Feedback1http://www.aygfsteel.com/dodoma/comments/commentRss/39445.htmlhttp://www.aygfsteel.com/dodoma/services/trackbacks/39445.html1:抽象规范
2:一个具体实?br />3:一个虚拟机实例

java虚拟机的生命周期
java虚拟机的天职是:q行一个javaE序.当一个javaE序q行开始运行时,一个虚拟机实例׃生了.当一个计机上同时运行三个javaE序.则将产生三个java虚拟机实?每个E序q行在自q虚拟机里?不会q扰.当程序运行完毕时,虚拟机将自动退?

java虚拟机里面有两种U程,守护U程和非守护U程.守护U程是说java虚拟qU程,如垃圾收集线E?而非守护U程则是java中运行的E序U程.当非守护U程都运行完?java虚拟机将退?

一个java虚拟Z要包括了:c{载子pȝ,q行时数据区,执行引擎,内存区等{?

q行时数据区------主要?1 ?2  Ҏ?3 java?br />
堆和Ҏ区对虚拟机实例中所有的对象都是׃n?而java栈区,是对每个U程都是独立? 当一个class被蝲入到 classloader中时,会解析它的类型信?把这些类型信息放到方法区,而把E序中运行的对象,攑ֈ堆区.当一个新U程被创?分配一个新的java?java栈中保存?是方法中的一些变?状?java栈是由很多的java栈l成?一个栈帧包含了一个方法运行的状?当一个方法被执行的时?压入一个新的java栈到java栈中,Ҏq回的时?把栈弹出?抛弃?


Ҏ?br />
在java虚拟Z,被装载的cȝcd信息和类的静态变量被存储在方法区q样的内存里?javaE序q行?会查找这些个信息.Ҏ区的大小,是动态的.也可以不是连l的.可自由在堆中分配.也可以由用户或者程序员指定.Ҏ区可被垃圾收?

Ҏ区可以保存以下信?br />q个cd的全限定?br />直接类的全限定?br />是类cdq是接口
cd的访问修饰符
M直接类接口的全限定名的有序列表.
该类型的帔R?br />字段信息 cM声明的每个字D及光?如字D名,cd.修饰W号.
Ҏ信息:如方法名,q回cd.参数表列.修饰W号.字节?操作数栈和栈帧中局部变量区大小{等
c静态变?br />一个到cclassloader的引?br />一个到classcȝ引用


?br />用来存储q行时的对象实例

java?br />每启动一个新的线E?׃分配C个java?java栈以帧ؓ单位保存U程的运行状?它有两种操作.入栈,出栈.
当一个方法被调用?入栈,当一个方法返回时,出栈,或者当Ҏ出现异常.也出?


l成部分 局部变量区,操作数栈,帧数据区.


dodoma 2006-04-05 18:25 发表评论
]]>
effective_java阅读W记5http://www.aygfsteel.com/dodoma/archive/2006/03/31/38503.htmldodomadodomaFri, 31 Mar 2006 08:35:00 GMThttp://www.aygfsteel.com/dodoma/archive/2006/03/31/38503.htmlhttp://www.aygfsteel.com/dodoma/comments/38503.htmlhttp://www.aygfsteel.com/dodoma/archive/2006/03/31/38503.html#Feedback0http://www.aygfsteel.com/dodoma/comments/commentRss/38503.htmlhttp://www.aygfsteel.com/dodoma/services/trackbacks/38503.htmlWhen you switch from a language with manual memory management, such as C or C++, to agarbage-collected language, your job as a programmer is made much easier by the fact that your objects are automatically reclaimed when you're through with them. It seems almost like magic when you first experience it. It can easily lead to the impression that you don't have to think about memory management, but this isn't quite true.
java中也要注意内存管?br />
// Can you spot the "memory leak"?
public class Stack {
private Object[] elements;
private int size = 0;
public Stack(int initialCapacity) {
this.elements = new Object[initialCapacity];
}
Effective Java: Programming Language Guide
17
public void push(Object e) {
ensureCapacity();
elements[size++] = e;
}
public Object pop() {
if (size == 0)
throw new EmptyStackException();
return elements[--size];
}
/**
* Ensure space for at least one more element, roughly
* doubling the capacity each time the array needs to grow.
*/
private void ensureCapacity() {
if (elements.length == size) {
Object[] oldElements = elements;
elements = new Object[2 * elements.length + 1];
System.arraycopy(oldElements, 0, elements, 0, size);
}
}
}

q里有一个内存泄?br />如果一个栈先是增长Q然后再收羃Q那么,从栈中弹出来的对象将不会被当做垃圑֛Ӟ即使用栈的客户E序不再引用q些对象Q它们也不会被回收。这是因为,栈内部维护着对这些对象的q期引用Qobsolete re f e re n c eQ。所谓过期引用,是指永远也不会再被解除的引用

Ҏ
public Object pop() {
if (size==0)
throw new EmptyStackException();
Object result = elements[--size];
elements[size] = null; // Eliminate obsolete reference清除引用
return result;
}

当程序员W一ơ被cMq样的问题困扰的时候,他们往往会过分小心:对于每一个对象引用,一旦程序不再用到它Q就把它清空。这样做既没必要Q也不是我们所期望的,因ؓq样做会把程序代码弄得很乱,q且可以惛_q会降低E序的性能。“清I对象引用”这L操作应该是一U例外,而不是一U规范行为。消除过期引用最好的Ҏ是重用一个本来已l包含对象引用的变量Q或者让q个变量l束其生命周期。如果你是在最紧凑的作用域范围内定义每一个变量(见第2 9条)Q则q种情Ş׃自然而然地发生。应该注意到Q在目前的J V M实现q_上,仅仅退出定义变量的代码块是不够的,要想使引用消失,必须退出包含该变量的方法?br />
一般而言Q只要一个类自己理它的内存Q程序员应该警惕内存泄漏问题。一旦一个元素被释放掉,则该元素中包含的M对象引用应该要被清空?br />
内存泄漏的另一个常见来源是~存。一旦你把一个对象引用放C个缓存中Q它很Ҏ被遗忘掉Q从而得它不再有用之后很长一D|间内仍然留在~存?br />

dodoma 2006-03-31 16:35 发表评论
]]>
effective_java阅读W记4http://www.aygfsteel.com/dodoma/archive/2006/03/31/38485.htmldodomadodomaFri, 31 Mar 2006 07:19:00 GMThttp://www.aygfsteel.com/dodoma/archive/2006/03/31/38485.htmlhttp://www.aygfsteel.com/dodoma/comments/38485.htmlhttp://www.aygfsteel.com/dodoma/archive/2006/03/31/38485.html#Feedback0http://www.aygfsteel.com/dodoma/comments/commentRss/38485.htmlhttp://www.aygfsteel.com/dodoma/services/trackbacks/38485.htmlIt is often appropriate to reuse a single object instead of creating a new functionally equivalent object each time it is needed. Reuse can be both faster and more stylish. An object can always be reused if it is immutable

String s = new String("silly"); // DON'T DO THIS!

String s = "No longer silly";//do this

In addition to reusing immutable objects, you can also reuse mutable objects that you know will not be modified. Here is a slightly more subtle and much more common example of what not to do, involving mutable objects that are never modified once their values have been computed:
除了重用非可变的对象之外Q对于那些已知不会被修改的可变对象,你也可以重用它们?br />
such as
public class Person {
private final Date birthDate;
// Other fields omitted
public Person(Date birthDate) {
this.birthDate = birthDate;
}
// DON'T DO THIS!
public boolean isBabyBoomer() {
Calendar gmtCal =
Calendar.getInstance(TimeZone.getTimeZone("GMT"));
gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
Date boomStart = gmtCal.getTime();
gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
Date boomEnd = gmtCal.getTime();
return birthDate.compareTo(boomStart) >= 0 &&
birthDate.compareTo(boomEnd) < 0;
}
}

改良的版?
The isBabyBoomer method unnecessarily creates a new Calendar, TimeZone, and two Date instances each time it is invoked. The version that follows avoids this inefficiency with a static initializer:

class Person {
private final Date birthDate;
public Person(Date birthDate) {
this.birthDate = birthDate;
}
/**
* The starting and ending dates of the baby boom.
*/
private static final Date BOOM_START;
private static final Date BOOM_END;
static {
Calendar gmtCal =
Calendar.getInstance(TimeZone.getTimeZone("GMT"));
gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
BOOM_START = gmtCal.getTime();
gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
BOOM_END = gmtCal.getTime();
}
public boolean isBabyBoomer() {
return birthDate.compareTo(BOOM_START) >= 0 &&
birthDate.compareTo(BOOM_END) < 0;
}
}

。一个适配器是指这样一个对象:它把功能?br />托给后面的一个对象,从而ؓ后面的对象提供一个可选的接口。由于适配器除了后面的对象之外Q没有其他的状态信息,所以针Ҏ个给定对象的特定适配器而言Q它不需要创建多个适配器实例?br />
This item should not be misconstrued to imply that object creation is expensive and should be avoided. On the contrary, the creation and reclamation of small objects whose constructors do little explicit work is cheap, especially on modern JVM implementations. Creating additional objects to enhance the clarity, simplicity, or power of a program is generally a good thing.
Conversely, avoiding object creation by maintaining your own object pool is a bad idea unless the objects in the pool are extremely heavyweight. A prototypical example of an object that does justify an object pool is a database connection. The cost of establishing the connection is sufficiently high that it makes sense to reuse these objects. Generally speaking, however, maintaining your own object pools clutters up your code, increases memory footprint, and harms performance. Modern JVM implementations have highly optimized garbage collectors that easily outperform such object pools on lightweight objects.

dodoma 2006-03-31 15:19 发表评论
]]>
effective_java阅读W记3http://www.aygfsteel.com/dodoma/archive/2006/03/30/38371.htmldodomadodomaThu, 30 Mar 2006 14:22:00 GMThttp://www.aygfsteel.com/dodoma/archive/2006/03/30/38371.htmlhttp://www.aygfsteel.com/dodoma/comments/38371.htmlhttp://www.aygfsteel.com/dodoma/archive/2006/03/30/38371.html#Feedback0http://www.aygfsteel.com/dodoma/comments/commentRss/38371.htmlhttp://www.aygfsteel.com/dodoma/services/trackbacks/38371.html
Occasionally you'll want to write a class that is just a grouping of static methods and static fields.有时?你想写一个类,只是需要他提供了一pd的函数操作等,而不惌它实例化.?java.lang.Math or java.util.Arrays.
但是如果你不提供构造函?~译器会自动d一?
所以必L供一?此时,把构造函数设|ؓprivate.可以达到目?
一般用与工L.

// Noninstantiable utility class
public class UtilityClass {
// Suppress default constructor for noninstantiability
private UtilityClass() {
// This constructor will never be invoked
}
... // Remainder omitted
}

׃private的构咱函?该类不能被实例化.同时.不能被承了.

dodoma 2006-03-30 22:22 发表评论
]]>
effective-java阅读W记2http://www.aygfsteel.com/dodoma/archive/2006/03/30/38290.htmldodomadodomaThu, 30 Mar 2006 09:24:00 GMThttp://www.aygfsteel.com/dodoma/archive/2006/03/30/38290.htmlhttp://www.aygfsteel.com/dodoma/comments/38290.htmlhttp://www.aygfsteel.com/dodoma/archive/2006/03/30/38290.html#Feedback0http://www.aygfsteel.com/dodoma/comments/commentRss/38290.htmlhttp://www.aygfsteel.com/dodoma/services/trackbacks/38290.html
A singleton is simply a class that is instantiated exactly once [Gamma98, p. 127].如数据库资源{?
There are two approaches to implementing singletons. Both are based on keeping the
constructor private and providing a public static member to allow clients access to the sole
instance of the class. In one approach, the public static member is a final field:
// Singleton with final field
public class Elvis {
public static final Elvis INSTANCE = new Elvis();
private Elvis() {
...
}
... // Remainder omitted
}

1:构造函数私?br />2:提供一个静态static的final成员

The private constructor is called only once, to initialize the public static final field
Elvis.INSTANCE. 
Exactly one Elvis instance will exist once the Elvis class is initialized—no more,
no less. Nothing that a client does can change this.

In a second approach, a public static factory method is provided instead of the public static final field:
// Singleton with static factory
public class Elvis {
private static final Elvis INSTANCE = new Elvis();
private Elvis() {
...
}
public static Elvis getInstance() {
return INSTANCE;
}
... // Remainder omitted
}

用静态方法返?而不是直接返回一个实?

如果对对象序列化,要做多一点工??br />To make a singleton class serializable (Chapter 10), it is not sufficient merely to add
implements Serializable to its declaration. To maintain the singleton guarantee, you must
also provide a readResolve method (Item 57). Otherwise, each deserialization of a serialized instance will result in the creation of a new instance, 否则,l过对象反序列化?会导致创Z一个新的对?leading, in the case of our example, to spurious Elvis sightings. To prevent this, add the following readResolve method to the Elvis class:
// readResolve method to preserve singleton property
private Object readResolve() throws ObjectStreamException {
/*
* Return the one true Elvis and let the garbage collector
* take care of the Elvis impersonator.
*/
return INSTANCE;
}



dodoma 2006-03-30 17:24 发表评论
]]>
effective-java阅读W记1http://www.aygfsteel.com/dodoma/archive/2006/03/30/38282.htmldodomadodomaThu, 30 Mar 2006 09:08:00 GMThttp://www.aygfsteel.com/dodoma/archive/2006/03/30/38282.htmlhttp://www.aygfsteel.com/dodoma/comments/38282.htmlhttp://www.aygfsteel.com/dodoma/archive/2006/03/30/38282.html#Feedback0http://www.aygfsteel.com/dodoma/comments/commentRss/38282.htmlhttp://www.aygfsteel.com/dodoma/services/trackbacks/38282.htmlConsider providing static factory methods instead of
constructors 考虑以静态工厂方法取代构造函?br />
The normal way for a class to allow a client to obtain an instance is to provide a public
constructor.

A class can provide a public static factory method, which is simply
a static method that returns an instance of the class

such as
public static Boolean valueOf(boolean b) {
return (b ? Boolean.TRUE : Boolean.FALSE);
}

advantages
1:One advantage of static factory methods is that, unlike constructors, they have names.
一个类静态工厂方法是有名字的.If the parameters to a constructor do not, in and of themselves, describe the object being returned, a static factory with a well-chosen name can make a class easier to use and the resulting client code easier to read. 如BigInteger(int, int,Random)只知道new了一个BigInteger对象,而BigInteger.probablePrime()能说明返回的可能是一个素数对?另外,如果一个构造函C仅由于参数顺序不同而意思也不同.静态工厂方法就更加有用?它更能说明函数的意?因ؓ它有一个名?
the reason are : If the parameters to a constructor do not, describe the object being
returned, a static factory with a well-chosen name can make a class easier to use and the
resulting client code easier to read.

2:A second advantage of static factory methods is that, unlike constructors, they are not
required to create a new object each time they're invoked.每次h?不需要重新创Z个对?---单例的意?q可以在M时刻控制q个对象,同时在比较的时候也不需要用equals而用= =可以解决了.

3:A third advantage of static factory methods is that, unlike constructors, they can return
an object of any subtype of their return type.可以q回一个原q回cd的子cd对象.
One application of this flexibility is that an API can return objects without making their
classes public.Hiding implementation classes in this fashion can lead to a very compact API.q种灉|性的一个应用是Q一个A P I可以q回一个对象,同时又不使该对象的类成ؓ公有的。以q种方式把具体的实现c隐藏v来,可以得到一个非常简z的A P I。这Ҏ术非帔R合
于基于接口的框架l构Q因为在q样的框架结构中Q接口成为静态工厂方法的自然q回cd
sample:
// Provider framework sketch
public abstract class Foo {
// Maps String key to corresponding Class object
private static Map implementations = null;
// Initializes implementations map the first time it's called
private static synchronized void initMapIfNecessary() {
if (implementations == null) {
implementations = new HashMap();
// Load implementation class names and keys from
// Properties file, translate names into Class
// objects using Class.forName and store mappings.
...
}
}
public static Foo getInstance(String key) {
initMapIfNecessary();
Class c = (Class) implementations.get(key);
if (c == null)
return new DefaultFoo();
try {
return (Foo) c.newInstance();
} catch (Exception e) {
return new DefaultFoo();
}
}
}

公有的静态工厂方法所q回的对象的cM仅可以是非公有的Q而且该类可以随着每次调用
而发生变化,q取决于静态工厂方法的参数倹{只要是已声明的q回cd的子cdQ都是允
许的。而且Qؓ了增Y件的可维护性,q回对象的类也可以随着不同的发行版本而不同?br />disadvantages:

1:The main disadvantage of static factory methods is that classes without public or protected constructors cannot be subclassed.
cd果不含公有的或者受保护的构造函敎ͼ׃能被子类化。对于公有的静态工厂所q回的非公有c,也同样如?br />
2:A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods.


In summary, static factory methods and public constructors both have their uses, and it pays to understand their relative merits. Avoid the reflex to provide constructors without first considering static factories because static factories are often more appropriate. If you've weighed the two options and nothing pushes you strongly in either direction, it's probably best to provide a constructor simply because it's the norm.






dodoma 2006-03-30 17:08 发表评论
]]>
spring之aop:LogThrowAdvicehttp://www.aygfsteel.com/dodoma/archive/2006/03/28/37768.htmldodomadodomaTue, 28 Mar 2006 04:57:00 GMThttp://www.aygfsteel.com/dodoma/archive/2006/03/28/37768.htmlhttp://www.aygfsteel.com/dodoma/comments/37768.htmlhttp://www.aygfsteel.com/dodoma/archive/2006/03/28/37768.html#Feedback0http://www.aygfsteel.com/dodoma/comments/commentRss/37768.htmlhttp://www.aygfsteel.com/dodoma/services/trackbacks/37768.html接口修改?br />package net.blogjava.dodoma.spring.aop;

public interface HelloI {
 public String sayHello(String firstName,String lastName)throws Exception;
 }

cM改ؓ
package net.blogjava.dodoma.spring.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Hello implements HelloI {
 protected static final Log log=LogFactory.getLog(Hello.class);
 private String msg;
 public Hello(){}
 public Hello(String msg){
  this.msg=msg;
 }
 
 public String getMsg() {
  return msg;
 }
 public void setMsg(String msg) {
  this.msg = msg;
 }
 public String sayHello(String firstName, String lastName) throws Exception{
  // TODO Auto-generated method stub
  log.info("in the class "+this.getClass().getName()+"'s method sayHello()");
  throw new Exception("here is a exception !");
  return (msg+" "+firstName+" "+lastName);
 }
}



package net.blogjava.dodoma.spring.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.ThrowsAdvice;

public class LogThrowAdvice implements ThrowsAdvice {
 protected static final Log log = LogFactory.getLog(LogThrowAdvice.class);
 public void afterThrowing(Exception e)throws Throwable{
  log.info("in the class "+this.getClass().getName()+"'s method afterThrowing()");
  log.info("the exception is "+e.getMessage());
 }
}


package net.blogjava.dodoma.spring.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class HelloTest {
 protected static final Log log = LogFactory.getLog(HelloTest.class);

 /**
  * @param args
  * @throws Exception
  */
 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
  Resource rs = new ClassPathResource("beans.xml");
  BeanFactory bf = new XmlBeanFactory(rs);

  HelloI h = (HelloI) bf.getBean("theBean");
  log.info("starting...");
  try {
   log.info(h.sayHello("ma", "bin"));
  } catch (Exception e) {
   e.printStackTrace();
  }
  log.info("end...");
  
  
  ProxyFactory factory=new ProxyFactory();
  factory.addAdvice(new LogThrowAdvice ());
  factory.setTarget(new Hello("hello"));
  try{
  HelloI hi=(HelloI)factory.getProxy();
  hi.sayHello("ma","bin");}
  catch(Exception e){e.printStackTrace();}
 }

}



dodoma 2006-03-28 12:57 发表评论
]]>
spring之aop:LogAroundAdvicehttp://www.aygfsteel.com/dodoma/archive/2006/03/28/37766.htmldodomadodomaTue, 28 Mar 2006 04:52:00 GMThttp://www.aygfsteel.com/dodoma/archive/2006/03/28/37766.htmlhttp://www.aygfsteel.com/dodoma/comments/37766.htmlhttp://www.aygfsteel.com/dodoma/archive/2006/03/28/37766.html#Feedback1http://www.aygfsteel.com/dodoma/comments/commentRss/37766.htmlhttp://www.aygfsteel.com/dodoma/services/trackbacks/37766.htmlLogAroundAdvice 通知
package net.blogjava.dodoma.spring.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LogAroundAdvice implements MethodInterceptor {
 protected static final Log log = LogFactory.getLog(LogAroundAdvice.class);

 public Object invoke(MethodInvocation arg) throws Throwable {
  // 调用目标对象之前
  log.info("before the target object");
  Object val=arg.proceed();
  //调用目标对象之后
  log.info("the arg is "+arg);
  log.info("after the target object");
  return val;
 }

}

试Ҏ

package net.blogjava.dodoma.spring.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class HelloTest {
 protected static final Log log = LogFactory.getLog(HelloTest.class);

 /**
  * @param args
  * @throws Exception
  */
 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
  Resource rs = new ClassPathResource("beans.xml");
  BeanFactory bf = new XmlBeanFactory(rs);

  HelloI h = (HelloI) bf.getBean("theBean");
  log.info("starting...");
  try {
   log.info(h.sayHello("ma", "bin"));
  } catch (Exception e) {
   e.printStackTrace();
  }
  log.info("end...");
  
  
  ProxyFactory factory=new ProxyFactory();
  factory.addAdvice(new LogAroundAdvice ());
  factory.setTarget(new Hello("hello"));
  try{
  HelloI hi=(HelloI)factory.getProxy();
  hi.sayHello("ma","bin");}
  catch(Exception e){e.printStackTrace();}
 }

}



dodoma 2006-03-28 12:52 发表评论
]]>
spring之aop:LogAfterAdvicehttp://www.aygfsteel.com/dodoma/archive/2006/03/28/37761.htmldodomadodomaTue, 28 Mar 2006 04:37:00 GMThttp://www.aygfsteel.com/dodoma/archive/2006/03/28/37761.htmlhttp://www.aygfsteel.com/dodoma/comments/37761.htmlhttp://www.aygfsteel.com/dodoma/archive/2006/03/28/37761.html#Feedback0http://www.aygfsteel.com/dodoma/comments/commentRss/37761.htmlhttp://www.aygfsteel.com/dodoma/services/trackbacks/37761.html接口和实现类见LogBeforeAdvice的例?br />package net.blogjava.dodoma.spring.aop;

import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.AfterReturningAdvice;

public class LogAfterAdvice implements AfterReturningAdvice {
 protected static final Log log = LogFactory.getLog(LogAfterAdvice.class);
 public void afterReturning(Object returnVal, Method m, Object[] args,
   Object target) throws Throwable {
  // TODO Auto-generated method stub
  log.info("in the class "+this.getClass().getName()+"'s method afterReturning()");

  log.info("the target class is:" + target.getClass().getName());
  log.info("the target method is:" + m.getName());

  for (int i = 0; i < args.length; i++) {
   log.info("the method's args is:" + args[i]);
  }
  
  log.info("the returnValue is "+returnVal);
  //试,如果q回装备发生了异?如何处理程序流E?br />  //throw new Exception("q回装备发生异常");
 }

}

试代码

package net.blogjava.dodoma.spring.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class HelloTest {
 protected static final Log log = LogFactory.getLog(HelloTest.class);

 /**
  * @param args
  * @throws Exception
  */
 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
  Resource rs = new ClassPathResource("beans.xml");
  BeanFactory bf = new XmlBeanFactory(rs);

  HelloI h = (HelloI) bf.getBean("theBean");
  log.info("starting...");
  try {
   log.info(h.sayHello("ma", "bin"));
  } catch (Exception e) {
   e.printStackTrace();
  }
  log.info("end...");
  
  
  ProxyFactory factory=new ProxyFactory();
  factory.addAdvice(new LogAfterAdvice());
  factory.setTarget(new Hello("hello"));
  try{
  HelloI hi=(HelloI)factory.getProxy();
  hi.sayHello("ma","bin");}
  catch(Exception e){e.printStackTrace();}
 }

}


在beans.xml中加入如下代?br /><bean id="theLogAfterAdvice" class="net.blogjava.dodoma.spring.aop.LogAfterAdvice"/>

<property name="interceptorNames">
      <list>
        <value>theLogAfterAdvice</value><!--此时直接使用advice,表明q个cL有的实例,Ҏ,都n受advice的拦?->
       </list> 
</property>

切入点可省略,如需要的?br />?br />
<!--切入?->
  <!--Note: An advisor assembles pointcut and advice-->
  <bean id="theBeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice">
      <ref local="theLogAfterAdvice"/>
    </property>
    <!--匚w模式-->
    <property name="pattern">
      <value>.*</value>
    </property>
  </bean>



dodoma 2006-03-28 12:37 发表评论
]]>
spring之aop:LogBeforeAdvicehttp://www.aygfsteel.com/dodoma/archive/2006/03/28/37755.htmldodomadodomaTue, 28 Mar 2006 04:02:00 GMThttp://www.aygfsteel.com/dodoma/archive/2006/03/28/37755.htmlhttp://www.aygfsteel.com/dodoma/comments/37755.htmlhttp://www.aygfsteel.com/dodoma/archive/2006/03/28/37755.html#Feedback0http://www.aygfsteel.com/dodoma/comments/commentRss/37755.htmlhttp://www.aygfsteel.com/dodoma/services/trackbacks/37755.html接口
package net.blogjava.dodoma.spring.aop;

public interface HelloI {
 public String sayHello(String firstName,String lastName);
 }

实现c?br />package net.blogjava.dodoma.spring.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Hello implements HelloI {
 protected static final Log log=LogFactory.getLog(Hello.class);
 private String msg;
 public Hello(){}
 public Hello(String msg){
  this.msg=msg;
 }
 public String getMsg() {
  return msg;
 }
 public void setMsg(String msg) {
  this.msg = msg;
 }
 public String sayHello(String firstName, String lastName) {
  // TODO Auto-generated method stub
  log.info("in the class "+this.getClass().getName()+"'s method sayHello()");
  return (msg+" "+firstName+" "+lastName);
 }
}

BeforeAdvice通知

package net.blogjava.dodoma.spring.aop;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.MethodBeforeAdvice;

/**
 * Ҏ调用之前.
 * 先调用此Ҏ
 * @author dodoma
 **/
public class LogBeforeAdvice implements MethodBeforeAdvice {
 protected static final Log log = LogFactory.getLog(LogBeforeAdvice.class);

 public void before(Method m, Object[] args, Object target) throws Throwable {
  log.info("in the class "+this.getClass().getName()+"'s method before()");

  log.info("the target class is:" + target.getClass().getName());
  log.info("the target method is:" + m.getName());

  for (int i = 0; i < args.length; i++) {
   log.info("the method's args is:" + args[i]);
  }
  //试,如果在before通知中发生了异常,E序程如?br />  //throw new Exception("异常");
 }
}

试c?br />package net.blogjava.dodoma.spring.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class HelloTest {
 protected static final Log log = LogFactory.getLog(HelloTest.class);
 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
 //应用spring的ioc容器
  Resource rs = new ClassPathResource("beans.xml");
  BeanFactory bf = new XmlBeanFactory(rs);

  HelloI h = (HelloI) bf.getBean("theBean");
  log.info("starting...");
  try {
   log.info(h.sayHello("ma", "bin"));
     } catch (Exception e) {
   e.printStackTrace();
  }
  log.info("end...");
  
  //如果没有使用spring的ioc,可以直接用如下代码测?br />  ProxyFactory factory=new ProxyFactory();
  factory.addAdvice(new LogBeforeAdvice());//d通知
  factory.setTarget(new Hello("hello"));//d被代理的cd?br />  try{
  HelloI hi=(HelloI)factory.getProxy();
  hi.sayHello("ma","bin");}
  catch(Exception e){e.printStackTrace();}
 }

}

spring的配|文件beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "

<beans>

<!--享受日志的类-->
<bean id="theTargetBean" class="net.blogjava.dodoma.spring.aop.Hello">
 <property name="msg">
     <value>hello</value>
    </property>
     
</bean>

<!--advices-->
<bean id="theLogBeforeAdvice" class="net.blogjava.dodoma.spring.aop.LogBeforeAdvice"/>

<!--CONFIG-->
  <bean id="theBean" class="org.springframework.aop.framework.ProxyFactoryBean">
    <!--接口-->
    <property name="proxyInterfaces">
      <value>net.blogjava.dodoma.spring.aop.HelloI</value>
    </property>
    <!--被代理的c?->
    <property name="target">
      <ref local="theTargetBean"/>
    </property>
    <!--加在代理cM的advice-->
    <property name="interceptorNames">
      <list>
        <value>theLogBeforeAdvice</value><!--此时直接使用advice,表明q个cL有的实例,Ҏ,都n受advice的拦?->
      </list>
    </property>
  </bean>
  
  <!--切入?可以_匚wc?Ҏ,也可以不需要这?->
  <!--Note: An advisor assembles pointcut and advice-->
  <bean id="theBeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice">
      <ref local="theLogBeforeAdvice"/>
    </property>
    <!--匚w模式-->
    <property name="pattern">
      <value>.*</value>
    </property>
  </bean>
    
</beans>



dodoma 2006-03-28 12:02 发表评论
]]>
վ֩ģ壺 | ˳| | ޼| | ϽϽ| ʡ| տ| ۽| | | ɽ| | żҿ| ¯| ¡| | ĵ| | | Ҵ| | | | | ̩| | | ɽ| ǿ| հ| | | | ͨ| | | | Ȫ| ͭ| |