标签:ali 切面 mit npoi private location post ice c中
一、使用依賴
<!--1。mysql 5.1.38--> <!--2. mybatis 3.4.6--> <!--3。spring整合mb:spring-context 5.2.6--> <!-- spring-jdbc 5.2.6--> <!-- mybatis-spring 2.0.3--> <!--4. mvc使用的 spring-web 5.2.6--> <!-- spring-mvc 5.2.6--> <!-- 5.數據傳輸格式jackson-databind 2.11.0--> <!-- ackson-core 2.11.0--> <!-- 6. servlet 3.1.0--> <!-- 7. aop需要的:aop-aspect-runtime 1.9.4 --> <!-- aop-aspect-weaver 1.9.4 --> <!-- 8.日志需要的:log4j 1.2.17 -->
二、目錄
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <context:component-scan base-package="control"/> <context:annotation-config/> <mvc:annotation-driven/>
<bean class="com.fasterxml.jackson.databind.ObjectMapper"/> <aop:aspectj-autoproxy/> <!--依赖倒置 DI--> <!--切面编程--> <!--拦截器--> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <context:component-scan base-package="model"/> <context:annotation-config/> <context:property-placeholder location="classpath:datasource.properties"/> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${mysql.driver}"/> <property name="url" value="${mysql.url}"/> <property name="username" value="${mysql.username}"/> <property name="password" value="${mysql.password}"/> </bean> <!--声明式(注解)事务--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="typeAliasesPackage" value="entity"/> <property name="mapperLocations" value="classpath:mapping/*Mapper.xml"/> </bean> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="model.mapper"/> </bean> </beans>
@Service public class StudentService{ @Autowired private StudentMapper sm;
package control; import entity.StudentInfo; import model.service.StudentService; import org.aspectj.lang.annotation.Aspect; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import pageutil.Result; import java.sql.SQLException; @CrossOrigin // 跨域訪問,結合RestController一起用,Rest代表ajax,前後端分離 @RestController // 如果只是jsp頁面訪問,使用Controller就行了 public class StudentControl{ @Autowired private StudentService ss; /** * <!--声明式(注解)事务--> * 事务: 应用场景:多条修改级语句共同完成一个业务 * 事务特性:原子性,隔离性,一致性 * 隔离级别 isolation = Isolation.READ_COMMITTED * 超时:timeout = int (second) * 开始事务,提交事务,回滚事务 * spring框架:propagation = Propagation.REQUIRED (默认值:有事务则用,无则加事务) * 回滚机制:rollbackFor = 异常类型:SqlException.class * @param stu * @return */ @PostMapping("/as.do")
// 如果超過10秒就回滾,如果出現sql異常就回滾 @Transactional(timeout = 10,rollbackFor = SQLException.class) public Result addStu(StudentInfo stu) { return ss.addStu(stu); } @GetMapping("/rs.do") public Result removeStu(int sid) { return ss.removeStu(sid); } @GetMapping("/msf.do") public Result modifyStuFind(int sid) { return ss.modifyStuFind(sid); } @PostMapping("/ms.do") public Result modifyStu(StudentInfo stu) { return ss.modifyStu(stu); } @GetMapping("/fsp.do")
// 查詢操作,只讀不改 @Transactional(readOnly = true) public Result findStu(int pageNo) { final int pageSize = 5; return ss.findStu(pageNo, pageSize); } }
1) pom中需要有
aspect-runtime:aspectjrt &&
aspect-weaver:aspectjweaver
2)mvc中配置
<bean class="com.fasterxml.jackson.databind.ObjectMapper"/>
<aop:aspectj-autoproxy/>
3)controller層中添加Handler
package control; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.log4j.Logger; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import pageutil.Result; @Component @Aspect public class ControlLogAspectHandler { private static Logger logger = Logger.getLogger(ControlLogAspectHandler.class); @Autowired private ObjectMapper mapper; // 在哪裏做: *表示返回類型;control包下的以Control結尾的類的*方法名的無參或多個參數的方法 @Pointcut(value = "execution(* control.*Control.*(..))") public void pointcut(){} private StringBuilder parse(JoinPoint jp) throws JsonProcessingException { StringBuilder builder = new StringBuilder(); String t = jp.getTarget().getClass().getName(); String m = jp.getSignature().getName(); Object[] r = jp.getArgs(); builder.append(t); builder.append("."); builder.append(m); builder.append("("); builder.append(mapper.writeValueAsString(r)); builder.append(")"); return builder; } // 做什麽 @AfterReturning(value = "pointcut()",returning = "rtn") public void afterReturning(JoinPoint jp, Result rtn) throws JsonProcessingException { StringBuilder builder = parse(jp); builder.append(" RETURN "); builder.append(mapper.writeValueAsString(rtn)); logger.info(builder.toString()); } @AfterThrowing(value = "pointcut()",throwing = "ex") public void afterThrowing(JoinPoint jp,Exception ex){ try { StringBuilder builder = parse(jp); builder.append(" THROW "); builder.append(ex.toString()); logger.error(builder.toString()); } catch (JsonProcessingException e) { logger.error(e.getMessage()); } } }
标签:ali 切面 mit npoi private location post ice c中
原文地址:https://www.cnblogs.com/sabertobih/p/13442050.html