利用ANNOTATION替代XML配置文件

  1.  利用SPRING ANNOTATION 替代业务层的XML配置文件,需要在XML中配置注解扫描;
    <?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:context="http://www.springframework.org/schema/context"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    		   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
               http://www.springframework.org/schema/aop 
               http://www.springframework.org/schema/aop/spring-aop-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/tx 
               http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    	<!-- 配置Hibernate事务特性 -->
    	<tx:advice id="txAdvice" transaction-manager="transactionManager">
    		<tx:attributes>
    			<tx:method name="save*"   propagation="REQUIRED"/> 
    			<tx:method name="update*" propagation="REQUIRED"/> 
    			<tx:method name="remove*" propagation="REQUIRED"/>
    			<tx:method name="get*"  read-only="true"  /> 
    		</tx:attributes>
    	</tx:advice>
    	
    	<aop:config>
    		<aop:pointcut id="allManagerMethod"
    			expression="execution(* com.*.*.service.*.*(..))" />
    		<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" />
    	</aop:config>
    	
    	<!--  加入 aop annotation 注释 -->
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    	
    	<!-- 加入 component annotation 注释 -->
        <context:component-scan base-package="ims" ></context:component-scan>
     
  2.  利用JPA ANNOTATION替代持久层中的XML配置文件,需要替换SPRING配置文件中的SessionFactoryBean,并开启注解扫描;
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
        "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    	
    	<!-- Hibernate SessionFactory -->
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="dataSource">
    			<ref bean="dataSource"/>
    		</property>
    		<property name="mappingDirectoryLocations">
    			<list>
    				<value>WEB-INF/classes/com/</value>
    			</list>
    		</property>
     		<property name="packagesToScan" > 
      			<list>
    				<value>com.**.*</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
    				<prop key="hibernate.show_sql">true</prop>
    				<prop key="hibernate.use_outer_join">true</prop>
    				<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>
    				<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
    				<prop key="hibernate.connection.pool_size">10</prop>
    				<prop key="hibernate.jdbc.fetch_size">100</prop>
    			</props>
    			
    		</property>
    		<property name="lobHandler">
    			<ref bean="oracleLobHandler"/>
    		</property>
    	</bean>
    </beans>
     
  3. 利用SPRING MVC ANNOTATION 替代控制层的配置文件,需要在SPRING MVC 的 servlet配置文件中开启注解扫描<context:component-scan base-package="com" ></context:component-scan>
    <?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:p="http://www.springframework.org/schema/p"
    	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/context 
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    	<!-- 扫描ANNOTATION 注入的ACTION -->
    	<context:component-scan base-package="com" />
    
    	<!-- 异常捕获 -->
    	<bean id="exceptionResolver"
    		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    		<property name="exceptionMappings">
    			<props>
    				<prop key="java.lang.Exception">error</prop>
    				<!--<prop key="java.sql.SQLException">showDBError</prop>
    				<prop key="java.lang.RuntimeException">showError</prop>
    				<prop
    					key="org.springframework.web.multipart.MaxUploadSizeExceededException">maxUploadExceeded</prop>
    				-->
    			</props>
    		</property>
    	</bean>
    
    	<!-- URL 映射处理 -->
    	<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
    		<property name="interceptors">
    			<list>
    				<bean class="test.common.servlet.handler.DefaultViewNameInterceptor" />
    			</list>
    		</property>
    		<property name="caseSensitive" value="true" />
    	</bean>
    
    	<!-- 视图解析-->
    	<bean id="viewResolver"
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="viewClass"
    			value="org.springframework.web.servlet.view.JstlView" />
    		<property name="prefix" value="/" /> <!-- /WEB-INF/jsp/ -->
    		<property name="suffix" value=".jsp" />
    	</bean>
    
    </beans>
     MVC基类
    public class BaseActionController extends MultiActionController {
    	protected final Log log = LogFactory.getLog(getClass());
    
    	private static String RESULT_PAGE = "/common/resultPage"; // .jsp
    	private static String JSON_PAGE = "/common/ajaxJson"; // .jsp
    
    	private String jspPkg;
    
    	public String getJspPkg() {
    		return jspPkg;
    	}
    
    	public void setJspPkg(String jspPkg) {
    		this.jspPkg = jspPkg;
    	}
    
    	public BaseActionController() {
    		super();
    		if (this.getClass().isAnnotationPresent(JspPkg.class)) {
    			JspPkg jspPkgAnnotation = this.getClass().getAnnotation(JspPkg.class);
    			jspPkg = jspPkgAnnotation.value();
    		}
    	}
    
    	public BaseActionController(Object delegate) {
    		super(delegate);
    	}
    
    
    	public final ModelAndView getDefaultView() {
    		return new ModelAndView();
    	}
    
    }
    MVC ANNOTATION使用实例: 
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    
    @Retention(RetentionPolicy.RUNTIME)
    public @interface JspPkg {
    	
    	public String value();
    
    }
    @Controller
    @JspPkg(value="/test/annotation/")
    public class Jbpm4DeploymentController  extends BaseActionController {
    
    	@Resource (name="jbpm4DeploymentManager")
    	private	Jbpm4DeploymentManager jbpm4DeploymentManager;
    	
    // http://localhost:8001/test/jbpm4Deployment/initJbpm4DeploymentMag.mvc
    	public ModelAndView initJbpm4DeploymentMag(HttpServletRequest request,
    			HttpServletResponse response) throws Exception {
    		
    		return getDefaultView();
    	}
      
  4. 以上开源框架版本是SPRING 2.5.6; HIBERNATE 3.5.5;
  5. 单元测试JUNIT4使用annotation简化配置
    package test.annotation.test;
    
    import test.common.test.ImsJunit4Test;
    
    import javax.annotation.Resource;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.junit.Test;
    import org.springframework.test.context.transaction.TransactionConfiguration;
    
    import test.annotation.model.Expr;
    import test.annotation.service.ExprManager;
    
    @TransactionConfiguration(defaultRollback = false)
    public class ExprTest extends baseJunit4Test {
    	private transient final Log log = LogFactory.getLog(getClass());
    
    	@Resource(name = "exprManager")
    	private ExprManager exprManager;
    
    	/**
    	 * 获取一条记录信息并测试其修改操作
    	 */
    	@Test
    	public void testSaveExpr() {
    		System.out.println(exprManager);
    
    	}
    
    }
     利用SPRING容器和JUNIT4加上ANNOTATION配置,测试单元代码变得相当简洁;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值