| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2590 人关注过本帖
标题:尝试Spring2.0的AOP
取消只看楼主 加入收藏
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
 问题点数:0 回复次数:12 
尝试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;
}

}

[此贴子已经被作者于2007-2-7 10:59:53编辑过]

搜索更多相关主题的帖子: AOP 尝试 
2007-02-07 10:54
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 
再发调用业务逻辑层的接口
/*
* UserService.java
*
* Created on 2007年2月7日, 上午11:38
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package net.bccn.vlinux.springexample;

/**
*
* @author vlinux
*/
public interface UserService {

/**
* 登录
*/
User login(String username, String password);


/**
* 注册用户
*/
boolean register( User newUser );


/**
* 删除用户
*/
User remove( String username );

/**
* 获取用户
*/
User get(String username);


/**
* 判断用户是否已经存在
*/
boolean isExisted( User user );

}

[此贴子已经被作者于2007-2-7 12:04:27编辑过]


淘宝杜琨
2007-02-07 11:34
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 
给出UserDatabase的实现
/*
* UserDatabaseImpl.java
*
* Created on 2007年2月7日, 上午11:05
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package net.bccn.vlinux.springexample;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
*
* @author vlinux
*/
public class UserDatabaseImpl implements UserDatabase {

private static Map<String,User> userMap = new HashMap<String,User>();

public User get(String username) {
return userMap.get(username);
}

public Collection<User> get() {
return userMap.values();
}

public boolean add(User newUser) {
return userMap.put( newUser.getUsername(),newUser )!=null;
}

public User delete(String username) {
return userMap.remove( username );
}

}


淘宝杜琨
2007-02-07 12:22
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 
给出UserService的实现
/*
* UserServiceImpl.java
*
* Created on 2007年2月7日, 上午11:53
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package net.bccn.vlinux.springexample;

/**
*
* @author vlinux
*/
public class UserServiceImpl implements UserService {

private UserDatabase userDatabase;

public UserServiceImpl( UserDatabase userDatabase ) {
this.userDatabase = userDatabase;
}

public User login(String username, String password) {
User loginUser = userDatabase.get( username );
if( loginUser!=null && loginUser.getPassword().equals(password) ) {
return loginUser;
} else {
return null;
}
}

public boolean register(User newUser) {
return userDatabase.add( newUser );
}

public User remove(String username) {
return userDatabase.delete( username );
}

public User get(String username) {
return userDatabase.get( username );
}

public boolean isExisted(User user) {
return get( user.getUsername() )!=null;
}



}


淘宝杜琨
2007-02-07 12:23
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 
补充一个数据库的接口
/*
* UserDatabase.java
*
* Created on 2007年2月7日, 上午10:56
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package net.bccn.vlinux.springexample;

import java.util.Collection;

/**
*
* @author vlinux
*/
public interface UserDatabase {

/**
* 增加新用户
*/
boolean add( User newUser );

/**
* 删除用户
*/
User delete( String username );

/**
* 获取单个用户
*/
User get( String username );

/**
* 获取所有用户
*/
Collection<User> get();

}


淘宝杜琨
2007-02-07 12:24
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 
spring的xml配置文档
<?xml version="1.0" encoding="UTF-8"?>

<!--
Document : mybean.xml
Created on : 2007年2月6日, 下午5:16
Author : vlinux
Description:
Purpose of the document follows.
-->

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<aop:aspectj-autoproxy/>

<bean id="userDatabase" class="net.bccn.vlinux.springexample.UserDatabaseImpl" scope="prototype"/>

<bean id="userService" class="net.bccn.vlinux.springexample.UserServiceImpl" scope="singleton">
<constructor-arg ref="userDatabase"/>
</bean>

</beans>


淘宝杜琨
2007-02-07 12:27
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 
测试我们的代码
/*
* Main.java
*
* Created on 2007年2月7日, 下午12:05
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package net.bccn.vlinux.springexample;

import java.util.Date;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
*
* @author vlinux
*/
public class Main {

/**
* Creates a new instance of Main
*/
public Main() {
ListableBeanFactory factory = new ClassPathXmlApplicationContext("/net/bccn/vlinux/springexample/beans_spring.xml");
GenericBeanFactoryAccessor gbfa = new GenericBeanFactoryAccessor(factory);

UserService userService = gbfa.getBean("userService");
User userA = new User("vlinux","xunilv",21,new Date());
User userB = new User("knocker","rekconk",30,new Date());
User userC = new User("live41","14evil",24,new Date());

userService.register( userA );
userService.register( userB );
userService.register( userC );

User tempA = userService.login( "vlinux","xunilv" );
System.out.println( "tempA is :"+tempA );

User tempB = userService.get( "live41" );
System.out.println( "tempB is :"+tempB );

}


public static void main(String... args) {
new Main();
}

}




这是输出的效果
init:
deps-jar:
compile-single:
run-single:
2007-2-7 12:28:16 org.springframework.core.CollectionFactory <clinit>
信息: JDK 1.4+ collections available
2007-2-7 12:28:17 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [net/bccn/vlinux/springexample/beans_spring.xml]
2007-2-7 12:28:17 org.springframework.context.support.AbstractRefreshableApplicationContext refreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=17881115]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [org.springframework.aop.config.internalAutoProxyCreator,userDatabase,userService]; root of BeanFactory hierarchy
2007-2-7 12:28:17 org.springframework.context.support.AbstractApplicationContext refresh
信息: 3 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=17881115]
2007-2-7 12:28:17 org.springframework.aop.framework.DefaultAopProxyFactory <clinit>
信息: CGLIB2 available: proxyTargetClass feature enabled
2007-2-7 12:28:17 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
信息: Bean 'org.springframework.aop.config.internalAutoProxyCreator' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2007-2-7 12:28:17 org.springframework.context.support.AbstractApplicationContext initMessageSource
信息: Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@1fc2fb]
2007-2-7 12:28:17 org.springframework.context.support.AbstractApplicationContext initApplicationEventMulticaster
信息: Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@bfea1d]
2007-2-7 12:28:17 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [org.springframework.aop.config.internalAutoProxyCreator,userDatabase,userService]; root of BeanFactory hierarchy]
tempA is :username=vlinux password=xunilv age=21 birthday=Wed Feb 07 12:28:18 CST 2007
tempB is :username=live41 password=14evil age=24 birthday=Wed Feb 07 12:28:18 CST 2007
生成成功(总时间:2 秒)

淘宝杜琨
2007-02-07 12:28
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 
以上的代码还不是AOP的代码,仅仅是IOC的代码
其中1~7楼的代码是真正我们自己写的代码,这个是不管到那个地方,用那个框架都必须要完成的东西。所以不要以为那些属于spring

真正属于spring的其实只有8楼的配置文档和9楼的调用程序而已。

你可以看到,通过spring的IOC容器,UserDatabase和UserService魔术般的被组合到了一起。
待会再发关于AOP的代码,你会看到更不可思议的事情。

淘宝杜琨
2007-02-07 12:32
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 
scope="prototype" 的意思是,整个程序运行期间只能有一个对象存在

scope="singleton"则意思正好相反

淘宝杜琨
2007-02-07 19:48
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 
不是~~你记反了~

淘宝杜琨
2007-02-07 20:44
快速回复:尝试Spring2.0的AOP
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031244 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved