|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
你通过从书的数量和开发周期及运行速度来证明:net和ruby要比java简单。
1.新建一个Java一般工程,并需导进spring-aop.jar包;
2.建UserInfo类:
packageaop.secure;
publicclassUserInfo{
privateStringuserName;
privateStringpassword;
publicUserInfo(StringuserName,Stringpassword){
this.userName=userName;
this.password=password;
}
publicStringgetPassword(){
returnpassword;
}
publicStringgetUserName(){
returnuserName;
}
}
3.建平安信息提醒类SecureBean:
packageaop.secure;
publicclassSecureBean{
publicvoidwriteSecureMessage(){
System.out.println("EverytimeIlearnsomethingnewanditpushessomeoldstuffoutofmybrain.");
}
}
4.建切面类SecurityAdvice完成org.springframework.aop.MethodBeforeAdvice:
packageaop.secure;
importjava.lang.reflect.Method;
importorg.springframework.aop.MethodBeforeAdvice;
publicclassSecurityAdviceimplementsMethodBeforeAdvice{
privateSecurityManagersecurityManager;
publicSecurityAdvice(){
this.securityManager=newSecurityManager();
}
publicvoidbefore(Methodmethod,Object[]args,Objecttarget)throwsThrowable{
UserInfouser=securityManager.getLoggedOnUser();
if(user==null){
System.out.println("Nouserauthenticated.");
thrownewSecurityException("Methodname:"+method.getName());
}elseif("chigo".equals(user.getUserName())&&"chigo".equals(user.getPassword())){
System.out.println("OKAY!");
}else{
System.out.println("Loggedinuseris:"+user.getUserName());
thrownewSecurityException("User"+user.getUserName()+"isnotallowedaccesstomethod"+method.getName());
}
}
}
<p>
先说优点,首先和C,C++这些语言比起来,java很简单,去掉指针的java,非常好理解,自动垃圾回收机制也很好,自从JDK1.5推出以后,性能上又有了很大提高。 |
|