对已有程序进行了aspectj的整合

自从前几天发布了一个版本后,我就开始对ostocy-jshop做一些优化,让代码的耦合性减少。我就找到了aspectj在看了一些资料以后对其进行了整合。

简单的说下步骤和整合的功能代码吧

 

1,在eclipse中安装aspectj插件,找到help-=install new software 输入http://download.eclipse.org/tools/ajdt/36/update 这个地址。选择第一个安装即可。

 

 

 

2,把项目转换到aspectj模式下

 

右键项目--configuratie--有一个covert to aspectj 

 

3,看下spring的配置文件吧 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation=" 
          http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
          http://www.springframework.org/schema/tx 
          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-2.5.xsd 
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" default-autowire="byName">

	<aop:aspectj-autoproxy>
		
	</aop:aspectj-autoproxy>
	
	<context:component-scan base-package="com.jshop">
	</context:component-scan>
	<context:annotation-config />
	<context:property-placeholder location="classpath*:*.properties" />

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${jdbc.driver}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="initialPoolSize" value="${pool.initialPoolSize}" />
		<property name="minPoolSize" value="${pool.minPoolSize}" />
		<property name="maxPoolSize" value="${pool.maxPoolSize}" />
		<property name="maxIdleTime" value="${pool.maxIdleTime}" />
		<property name="acquireIncrement" value="${pool.acquireIncrement}" />
		<property name="checkoutTimeout" value="${pool.checkoutTimeout}" />
		<property name="maxIdleTimeExcessConnections" value="${pool.maxIdleTimeExcessConnections}" />
	</bean>
	<!-- 邮箱服务器配置 -->
	<bean id="javamailsenderimpl" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value="${email.host}"></property>
		<property name="defaultEncoding" value="${email.defaultEncoding}"></property>
		<property name="port" value="${email.port}"></property>
		<property name="username" value="${email.username}"></property>
		<property name="password" value="${email.password}"></property>
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">${email.auth}</prop>
				<prop key="mail.smtp.timeout">${email.timeout}</prop>
			</props>
		</property>

	</bean>
	
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
		destroy-method="close">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="mappingDirectoryLocations">
         <list>
            <value>classpath:com/jshop/entity</value>
         </list>
       </property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect 
                                        </prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.cache.use_second_level_cache">true</prop>
				<prop key="hibernate.cache.use_query_cache">false</prop>
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
			</props>
		</property>
		
	</bean>
	 <bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>
	<!-- <bean id="transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<props>
				<prop key="add*">PROPAGATION_REQUIRED</prop>
				<prop key="del*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="find*">PROPAGATION_REQUIRED</prop>
				<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
				
			</props>
		</property>
	</bean> -->
	
	<aop:config>
		<aop:advisor pointcut="execution(* com.jshop.action.service.*Service.*(..))" advice-ref="txAdvice"/>
	</aop:config>
 	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />  
	        <tx:method name="delete*" propagation="REQUIRED" />  
	        <tx:method name="update*" propagation="REQUIRED" /> 
			<tx:method name="get*" read-only="true"/>
			<tx:method name="query*" read-only="true"/>
			<tx:method name="find*" read-only="true"/>
			<tx:method name="load*" read-only="true"/>
			<tx:method name="*" rollback-for="Exception"/>
		</tx:attributes>
	</tx:advice>
	
	 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean> 

	<!-- 配置异步线程执行器 -->  <!-- 为异步线程执行器 进注入  -->
	<bean id="taskExecutor"
		class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
		<!-- 设置核心池子的大小 -->
		<property name="corePoolSize" value="10"></property>
		<!-- 设置最大池子的大小  --> 
		<property name="maxPoolSize" value="30"></property>
	</bean>


</beans>
	

 

最主要的就是这几行和注意载入的头 

 

<aop:aspectj-autoproxy>
		
	</aop:aspectj-autoproxy>
	
	<context:component-scan base-package="com.jshop">
	</context:component-scan>
	<context:annotation-config />

 这里其实就是在设置自动检索所有被标注了@AspectJ的类了

 

4,看下aspectj的类吧

 

 

@Aspect
public class CreateStaticHtmlAspect {
	private Map<String, Object> map = new HashMap<String, Object>();
	

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	/**
	 * 在商品增加/修改时更新静态页
	 * @throws TemplateException 
	 * @throws IOException 
	 */
	@After("execution(String com.jshop.action.GoodsTNAction.updateGoods())||execution(String com.jshop.action.GoodsTNAction.addGoods())")
	public void aftergoodsIUCreatestatichtml(JoinPoint joinPoint) throws IOException, TemplateException{
		GoodsTNAction gtn=(GoodsTNAction) joinPoint.getThis();
		if(gtn.getBean()!=null){
			String basepath=gtn.getDataCollectionTAction().getBasePath();
			String theme=gtn.getDataCollectionTAction().getDefaultTheme();
			String htmlpath="html/"+theme+"/shop/"+gtn.getBean().getGoodsid()+".html";
			gtn.getBean().setHtmlPath(htmlpath);
			//获取默认项目路径
			map.put(FreeMarkervariable.BASEPATH,basepath);
			//获取默认主题
			map.put(FreeMarkervariable.DEFAULTTHEMESIGN, theme);
			//获取商城基本信息
			map.put(FreeMarkervariable.JSHOPBASICINFO, gtn.getDataCollectionTAction().findJshopbasicInfo());
			//获取导航
			map.put(FreeMarkervariable.SITENAVIGATIONLIST, gtn.getDataCollectionTAction().findSiteNavigation());
			//获取页脚文章一级分类
			map.put(FreeMarkervariable.FOOTCATEGORY, gtn.getDataCollectionTAction().findFooterCateogyrT());
			//获取页脚文章一级分类下得文章
			map.put(FreeMarkervariable.FOOTERATRICLE, gtn.getDataCollectionTAction().findFooterArticle());
			//获取商品详细
			map.put(FreeMarkervariable.GOODSDETAIL, gtn.getBean());
			//这里获取商品参数和商品类型表中的对比并显示
			map.put(FreeMarkervariable.GOODSPARAMETERS,gtn.processGoodsparameters(gtn.getBean()));;//这是处理过后的商品参数,用在freemarker模板
			String htmlPath = gtn.getCreateHtml().createGoodsT(BaseTools.getApplicationthemesig() + "_" + ContentTag.TEMPLATENAMEFORGOODSDETAIL, gtn.getBean().getGoodsid(), map);
			if (Validate.StrNotNull(htmlPath)) {
				gtn.getGoodsTService().updateHtmlPath(gtn.getBean().getGoodsid(), htmlPath);
			}
		}
		
	}
}

 

至于aspectj的语法不多说了,功能很多,讲起来会更多。这里主要就是如何获取切入点的上下文。也就是我如何获取被我after的方法所在类的所有变量和方法和依赖注入的对象。只要JoinPoint作为参数就行了。然后你想拿什么就拿什么。可以断点自己看看。

 

 

结束,就这样吧。以上代码均来自我写的开源商城程序,希望大家可以来一起学习一起进步。我也是个菜鸟,希望一起进步啊。

地址:https://github.com/sdywcd/ostocy-jshop  

或者 http://code.google.com/p/ostocy-jshop/downloads/list  

欢迎微博求关注 http://weibo.com/sdywcd

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值