20170627在东软
来源:互联网 发布:mysql自带可视化界面 编辑:程序博客网 时间:2024/04/24 08:06
AOP
面向方面编程
(Aspect Oriented Programming)
在Spring之前已有的一种编程思想
通过OOD/OOP对系统中的业务对象进行建模,同时通过AOP对实体处理过程中的阶段进行隔离处理
理解AOP:做手术时,主刀医生和助手的分工,可以让主刀医生专注做自己的事情,让辅助工作交给助手做。(分离)
很可能,最终效果会比分离前更好一些!
程序也一样,把程序分成一个个方面
//修改前(“存钱提示”、“存钱”、“更新提示” 堆在一块) public void doSomething(){ try{ System.out.println(new Date() + "--xx账户存入10x"); dao.save(10); }catch (Exception e){ e.printStackTrace(); } System.out.println("更新账户"); }//修改后,把其他的写到另外一个类里,//然后当使用save时,又像之前堆在一起一样工作 public void doSomething(){ dao.save(10); }
(3.1.5)AOP应用范围
事务、安全、日志、错误/异常、性能
(3.2.1)核心概念
1、Advice(通知)
2、Pointcut(切入点)
3、
通知类型:
前置
(接口:MethodBeforeAdvice,
实现方法:before)
后置
(接口:AfterReturningAdvice,
实现方法:afterReturning)
环绕
(接口:MethodInterceptor,
实现方法:invoke)
异常
(ThrowAdvice)
//前置通知:import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class Attend implements MethodBeforeAdvice{ /* * args表示被拦截的方法的参数 * target表示被拦截的目标对象 */ //实现前置通知 public void before(Method method, Object[] args, Object target) throws Throwable{ System.out.println("----上课前开始考勤----"); }}
//环绕通知://com.daopackage com.dao;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class AroundAdvice implements MethodInterceptor{ //可以决定被拦截对象的核心是否执行 @Override public Object invoke(MethodInvocation mi) throws Throwable { System.out.println("--(环绕前置)验证用户名、密码--"); //核心方法的执行 Object returnValue = mi.proceed(); System.out.println("--(环绕后置)写入日志--"); return returnValue; }}//com.entitypackage com.entity;import com.impl.ISearch;public class Search implements ISearch { @Override public void search(String no) { System.out.println("已找到用户名为"+no+"的信息,此处省略显示"); }}//com.implpackage com.impl;public interface ISearch { //到数据库查找该用户 public void search(String no);}//com.testpackage com.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.impl.ISearch;public class SpringAOPTest { public static void main(String[] args) { //Student student = new Student(); ApplicationContext context = new ClassPathXmlApplicationContext("SpringAOP.xml"); ISearch rd = (ISearch)context.getBean("search"); rd.search("2017"); }}SpringAOP.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:beans="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd" > <!-- 环绕通知 --> <bean id="aroundAdvice" class="com.dao.AroundAdvice"> </bean> <!-- 核心对象 --> <bean id="search" class="com.entity.Search"> </bean> <aop:config> <!-- 定义切点 --> <aop:pointcut expression="execution(* com.entity..*.*(..))" id="pc1"/> <!-- 定义向导,引用切点和通知 --> <aop:advisor advice-ref="aroundAdvice" pointcut-ref="pc1"/> </aop:config></beans>
环绕通知步骤
1、新建接口类(com.impl)ISearch.java;
2、新建实体类(com.entity)Search.java,实现上面的接口;
3、新建环绕通知类,实现接口MethodInterceptor,实现invoke方法,里面有一句:
Object returnValue = mi.proceed();
在其前,是环绕的前置通知;在其后,是环绕的后置通知;
4、在SpringAOP.xml引入aop(可以通过Namespaces里勾选),然后注册bean:
<!-- 环绕通知 --> <bean id="aroundAdvice" class="com.dao.AroundAdvice"> </bean> <!-- 核心对象 --> <bean id="search" class="com.entity.Search"> </bean> <aop:config> <!-- 定义切点 --> <aop:pointcut expression="execution(* com.entity..*.*(..))" id="pc1"/> <!-- 定义向导,引用切点和通知 --> <aop:advisor advice-ref="aroundAdvice" pointcut-ref="pc1"/> </aop:config>
5、编写测试类SpringAOPTest.java
ApplicationContext context = new ClassPathXmlApplicationContext("SpringAOP.xml"); ISearch rd = (ISearch)context.getBean("search"); rd.search("2017");
在配置文件引入AOP:
在<beans
里,加上:
xmlns:aop="http://www.springframework.org/schema/aop"
在Namespaces选项卡,勾上aop即可。
然后在下面aop的组装:
<aop:config> <!--定义切点--> <aop:pointcut expresstion="execution(* com.neusoft.dao..*.*(..))" id="pc1"/>//(* com.neusoft.dao..*.*(..)),其中第一个*表示返回值;//com.neusoft.dao..*.*(..)表示dao及其下的任何自包的任何方法的任何参数//方法里的参数的写法://( )无参//(..)任意个参数//(*)任意类型的一个参数//(*,*)任意类型的两个参数//(int)只有int参数//(*,String)第一个参数任意类型,第二个是String <!--定义向导(可以有多个),引用切点和通知--> <aop:advisor advice-ref="引用的通知beanid" pointcut-ref="pc1"/></aop:config>
前置通知
环绕通知的方法前的通知
核心方法
环绕通知的方法后的通知
后置通知
第四章
Spring持久层
导入Spring持久化:
1、在项目右键–MyEclipse–Project Facets – install Spring facets –勾上Spring persisence
2、把Mysql驱动放入WebRoot – WEB-INF – lib里
3、注册数据库到myeclipse
方法①(编辑配置文件):
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/world2"></property> <property name="username" value="root"></property> <property name="password" value=""></property> </bean>
方法②数据源定义
(配置数据库.properties):
1、新建一个数据库配置文件:jdbc.properties;
2、给这个数据库配置文件写入键值对(就是上面本打算写进配置文件的那几组值);
3、还是回到配置文件,
<!-- 定位jdbc属性文件(.properties)的位置 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="location" value="jdbc.properties" /> </bean> <!-- 数据库连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${}" /> <!--其中,property里面的value,就是对应jdbc.properties里面的键值对的“键”--> </bean>
4、用JUnit测试(右键项目 – 构建路径 – 添加库 – JUnit – 下一步 即可);然后对测试类右键 – 运行方式 – JUnit测试
5、准备好数据库,数据库名world2、表名dept
create table dept( deptno int,--主键 dname varchar(20), loc varchar(20))
6、建立实体类和DAO类(即数据访问类):
a)在包com.entity,新建Dept.java,
package com.entity;public class Dept { private int deptno; private String sname; private String loc;//部门位置 public Dept(){ } public Dept(int deptno, String sname, String loc) { this.deptno = deptno; this.sname = sname; this.loc = loc; } public int getDeptno() { return deptno; } public void setDeptno(int deptno) { this.deptno = deptno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getLoc() { return loc; } public void setLoc(String loc) { this.loc = loc; }}
b)在包com.dao新建DeptDao.java类,来实现数据访问
package com.dao;import java.util.List;import org.apache.taglibs.standard.tag.el.sql.UpdateTag;import com.entity.Dept;public interface DeptDao { //查询所有部门 public List<Dept> queryAll(); //根据条件查询部门、例如查询所有在北京的部门 public List<Dept> queryByCondition(Dept condition); //根据部门编号查询某个部门 public Dept queryOne(int deptno); //保存部门 public int save(Dept dept); //根据部门id来更新部门 public int update(int deptno,Dept newDept); //根据部门id来删除部门 public int delete(int deptno);}
c)实现接口的类:
package com.dao.impl;import java.util.List;import com.dao.DeptDao;import com.entity.Dept;public class DeptDaoImpl implements DeptDao{ @Override public List<Dept> queryAll() { // TODO 自动生成的方法存根 return null; } @Override public List<Dept> queryByCondition(Dept condition) { // TODO 自动生成的方法存根 return null; } @Override public Dept queryOne(int deptno) { // TODO 自动生成的方法存根 return null; } @Override public int save(Dept dept) { // TODO 自动生成的方法存根 return 0; } @Override public int update(int deptno, Dept newDept) { // TODO 自动生成的方法存根 return 0; } @Override public int delete(int deptno) { // TODO 自动生成的方法存根 return 0; }}
- 20170627在东软
- 在东软
- 20170628在东软
- 6.29在东软
- 7.3在东软
- 7.4在东软
- 7.6在东软
- 7.24在东软
- 东软在挖沟
- 实训在东软...
- 我在东软的日子
- 在东软实习的日子
- 在东软正式培训的第一周
- 在
- 软实力订阅在这里
- 在软卓的日子
- 在某软的酸甜苦辣
- 刘强东瘦了,京东在膨胀
- 写高质量OC代码52建议总结:36.不要使用retainCount
- Android编译时遇到的问题
- errcode: 41028, errmsg: "invalid form id hint: [60kWOa0285ge25
- <iOS开发>之CocoaAsyncSocket使用
- ECMAScript2018语言规范
- 20170627在东软
- 远程过程调用 (RPC)
- ELK学习10_ELK系列--实时日志分析系统ELK 部署与运行中的问题汇总
- dancing links(舞蹈链)
- 6.3-全栈Java笔记:异常处理方法(上)
- Docker 修改镜像源地址
- fabric
- 使用https://github.com/jaredrummler/AndroidProcesses库遇到的崩溃问题的“探索”
- iOS基石——UIViewController