尝试Spring2.0的AOP
首先得写一个User类
/*
* User.java
*
* Created on 2007年2月7日, 上午10:24
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package net.bccn.vlinux.springexample;
import java.util.Date;
/**
*
* @author vlinux
*/
public final class User {
private String username;
private String password;
private int age;
private Date birthday;
public static final int USERNAME_LENGTH_MIN = 4;
public static final int USERNAME_LENGTH_MAX = 16;
public static final int PASSWORD_LENGTH_MIN = 4;
public static final int PASSWORD_LENGTH_MAX = 16;
public static final int AGE_VALUE_MAX = 99;
public static final int AGE_VALUE_MIN = 0;
public User( String username, String password, int age, Date birthday ) {
this.setUsername(username);
this.setPassword(password);
this.setAge(age);
this.setBirthday(birthday);
}
public static User createInstance( String username,
String password,
int age,
Date birthday ) {
User user = null;
try{
user = new User( username, password, age, birthday );
} catch( IllegalArgumentException ex ) {
//
} finally {
return user;
}
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public int getAge() {
return age;
}
public Date getBirthday() {
return birthday==null?null:(Date)birthday.clone();
}
public User clone() {
User cloneUser = new User(this.getUsername(),
this.getPassword(),
this.getAge(),
this.getBirthday());
return cloneUser;
}
public boolean equals(Object obj) {
if( obj instanceof User ) {
return false;
} else if( obj == null ) {
return false;
} else {
User user = (User)obj;
return user.getUsername().equals(this.getUsername());
}
}
public String toString() {
return "username="+getUsername()+" "+
"password="+getPassword()+" "+
"age="+getAge()+" "+
"birthday="+getBirthday();
}
public int hashCode() {
return getUsername().length()+
getPassword().length()+
getAge()+
(int)(getBirthday().getTime()/1000000);
}
private void setUsername(String username) {
if( username==null ||
username.length()<USERNAME_LENGTH_MIN ||
username.length()>USERNAME_LENGTH_MAX) {
throw new IllegalArgumentException("错误的用户名长度");
}
this.username = username;
}
private void setPassword(String password) {
if( password==null ||
password.length()<PASSWORD_LENGTH_MIN ||
password.length()>PASSWORD_LENGTH_MAX) {
throw new IllegalArgumentException("错误的密码长度");
}
this.password = password;
}
private void setAge(int age) {
if( age<AGE_VALUE_MIN || age>AGE_VALUE_MAX) {
throw new IllegalArgumentException("错误的年龄");
}
this.age = age;
}
private void setBirthday(Date birthday) {
if( birthday==null ) {
throw new IllegalArgumentException("错误的生日");
}
this.birthday = birthday;
}
}
/*
* User.java
*
* Created on 2007年2月7日, 上午10:24
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package net.bccn.vlinux.springexample;
import java.util.Date;
/**
*
* @author vlinux
*/
public final class User {
private String username;
private String password;
private int age;
private Date birthday;
public static final int USERNAME_LENGTH_MIN = 4;
public static final int USERNAME_LENGTH_MAX = 16;
public static final int PASSWORD_LENGTH_MIN = 4;
public static final int PASSWORD_LENGTH_MAX = 16;
public static final int AGE_VALUE_MAX = 99;
public static final int AGE_VALUE_MIN = 0;
public User( String username, String password, int age, Date birthday ) {
this.setUsername(username);
this.setPassword(password);
this.setAge(age);
this.setBirthday(birthday);
}
public static User createInstance( String username,
String password,
int age,
Date birthday ) {
User user = null;
try{
user = new User( username, password, age, birthday );
} catch( IllegalArgumentException ex ) {
//
} finally {
return user;
}
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public int getAge() {
return age;
}
public Date getBirthday() {
return birthday==null?null:(Date)birthday.clone();
}
public User clone() {
User cloneUser = new User(this.getUsername(),
this.getPassword(),
this.getAge(),
this.getBirthday());
return cloneUser;
}
public boolean equals(Object obj) {
if( obj instanceof User ) {
return false;
} else if( obj == null ) {
return false;
} else {
User user = (User)obj;
return user.getUsername().equals(this.getUsername());
}
}
public String toString() {
return "username="+getUsername()+" "+
"password="+getPassword()+" "+
"age="+getAge()+" "+
"birthday="+getBirthday();
}
public int hashCode() {
return getUsername().length()+
getPassword().length()+
getAge()+
(int)(getBirthday().getTime()/1000000);
}
private void setUsername(String username) {
if( username==null ||
username.length()<USERNAME_LENGTH_MIN ||
username.length()>USERNAME_LENGTH_MAX) {
throw new IllegalArgumentException("错误的用户名长度");
}
this.username = username;
}
private void setPassword(String password) {
if( password==null ||
password.length()<PASSWORD_LENGTH_MIN ||
password.length()>PASSWORD_LENGTH_MAX) {
throw new IllegalArgumentException("错误的密码长度");
}
this.password = password;
}
private void setAge(int age) {
if( age<AGE_VALUE_MIN || age>AGE_VALUE_MAX) {
throw new IllegalArgumentException("错误的年龄");
}
this.age = age;
}
private void setBirthday(Date birthday) {
if( birthday==null ) {
throw new IllegalArgumentException("错误的生日");
}
this.birthday = birthday;
}
}
[此贴子已经被作者于2007-2-7 10:59:53编辑过]