applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd "> -->
<!-- 定义UserDao对象,并指定id为userDao -->
<!-- <bean id="userDao" class="dao.impl.UserDao" />
定义UserBiz对象,并指定id为userBiz
<bean id="userBiz" class="biz.impl.UserBiz">
为userBiz的dao属性赋值,需要注意的是,这里要调用setDao()方法
<property name="dao">
引用id为userDao的对象为userBiz的dao属性赋值
<ref bean="userDao" />
<bean class="dao.impl.UserDao"></bean>
</property>
<property name="num">
<value>10</value>
</property>
</bean> --> <!-- <bean id="userDao1" class="com.demo.dao.UserDaoImpl"></bean>
<bean id="userService" class="service.UserServiceImpl">
<property name="userDao">
<ref bean="userDao1"/>
</property>
</bean> --> <!-- <bean id="userDao" class="com.demo.dao.UserDaoImpl"></bean>
<bean id="userService" class="service.UserServiceImpl">
<constructor-arg>
<ref bean ="userDao"/>
</constructor-arg>
</bean> -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<bean id="userService" class="com.aop.service.UserService"/>
<bean id="servicelogging" class="com.aop.service.ServiceLogging"/>
<aop:config>
<aop:pointcut expression="execution(public * com.aop.service.*.*Service(..))" id="servicePointcut"/>
<aop:aspect ref="servicelogging">
<aop:before method="before" pointcut-ref="servicePointcut"/>
<aop:after method="after" pointcut-ref="servicePointcut"/>
<aop:after-returning method="afterReturing" pointcut-ref="servicePointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="servicePointcut"/>
<aop:around method="around" pointcut-ref="servicePointcut"/>
</aop:aspect>
</aop:config>
</beans>

  User.java

package com.aop.entity;

public class User {
private int id;
private String username;
private String password;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public User(int id, String username, String password, String email) {
super();
this.id = id;
this.username = username;
this.password = password;
this.email = email;
}
public User() {
super();
// TODO Auto-generated constructor stub
} }

  UserService

package com.aop.service;

import com.aop.entity.User;

public class UserService {

	/**
* @param args
*/
public void addUserService(User user){ System.out.println("业务方法被执行");
System.out.println(user.getUsername()); } }

  ServiceLogging

package com.aop.service;

import org.aspectj.lang.ProceedingJoinPoint;

public class ServiceLogging {
public void before(){
System.out.println("前置增强处理被执行");
}
public void after(){
System.out.println("最终增强处理被执行");
}
public void afterReturing(){
System.out.println("后置增强处理被执行");
}
public void afterThrowing(){
System.out.println("抛异常增强处理被执行");
} public void around(ProceedingJoinPoint pjp){
System.out.println("环绕前置增强处理被执行");
try {
pjp.proceed();
} catch (Throwable e) { e.printStackTrace();
}
System.out.println("环绕后置增强处理被执行");
}
}

  AopTest.java

package com.aop.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.aop.entity.User;
import com.aop.service.UserService; public class AopTest { /**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service=(UserService)ctx.getBean("userService");
User user = new User(10,"Tom","123456","504177380@qq.com");
service.addUserService(user); } }

  

Spring的本质是什么?

1.对象的创建

new  抽象类 工厂模式

工厂模式,以及其他模式像抽象工厂,

Builder模式提供的都是创建对象的方法。
这背后体现的都是“封装变化”的思想。

这些模式只是一些最佳实践而已: 起了一个名称、描述一下解决的问题、使用的范围和场景,在项目中还得自己去编码实现他们。

2.解除依赖

面向接口编程

3.Spring依赖注入

在Java 世界里,如果想描述各种逻辑关系, XML是不二之选

这个xml 挺容易理解的, 但是仅仅有它还不够, 还缺一个解析器(假设叫做XmlAppContext)来解析,处理这个文件,

基本过程是:0. 解析xml, 获取各种元素

      1. 通过Java反射把各个bean 的实例创建起来:

      2. 还是通过Java反射调用 的两个方法:

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service=(UserService)ctx.getBean("userService");

4.IOC VS DI

JAVAWEB 一一 Spirng(AOP面向切面)的更多相关文章

  1. AOP 面向切面编程, Attribute在项目中的应用

    一.AOP(面向切面编程)简介 在我们平时的开发中,我们一般都是面对对象编程,面向对象的特点是继承.多态和封装,我们的业务逻辑代码主要是写在这一个个的类中,但我们在实现业务的同时,难免也到多个重复的操 ...

  2. AOP面向切面编程的四种实现

     一.AOP(面向切面编程)的四种实现分别为最原始的经典AOP.代理工厂bean(ProxyFacteryBean)和默认自动代理DefaultAdvisorAutoProxyCreator以及Bea ...

  3. Javascript aop(面向切面编程)之around(环绕)

    Aop又叫面向切面编程,其中“通知”是切面的具体实现,分为before(前置通知).after(后置通知).around(环绕通知),用过spring的同学肯定对它非常熟悉,而在js中,AOP是一个被 ...

  4. Method Swizzling和AOP(面向切面编程)实践

    Method Swizzling和AOP(面向切面编程)实践 参考: http://www.cocoachina.com/ios/20150120/10959.html 上一篇介绍了 Objectiv ...

  5. [转] AOP面向切面编程

    AOP面向切面编程 AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  6. C# AOP 面向切面编程之 调用拦截

    有时候我们需要在代码中对方法调用进行拦截,并修改参数和返回值,这种操作叫做AOP(面向切面编程) 不过需要注意的是,AOP的效率很慢,在需要高效率场合慎用. 以下是C#的AOP方法: 首先建立一个控制 ...

  7. 【原创】Android AOP面向切面编程AspectJ

    一.背景: 在项目开发中,对 App 客户端重构后,发现用于统计用户行为的友盟统计代码和用户行为日志记录代码分散在各业务模块中,比如在视频模块,要想实现对用户对监控点的实时预览和远程回放行为进行统计, ...

  8. 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十 || AOP面向切面编程浅解析:简单日志记录 + 服务切面缓存

    代码已上传Github+Gitee,文末有地址 上回<从壹开始前后端分离[ .NET Core2.0 Api + Vue 2.0 + AOP + 分布式]框架之九 || 依赖注入IoC学习 + ...

  9. 论AOP面向切面编程思想

    原创: eleven 原文:https://mp.weixin.qq.com/s/8klfhCkagOxlF1R0qfZsgg [前言] AOP(Aspect-Oriented Programming ...

  10. 学习笔记: AOP面向切面编程和C#多种实现

    AOP:面向切面编程   编程思想 OOP:一切皆对象,对象交互组成功能,功能叠加组成模块,模块叠加组成系统      类--砖头     系统--房子      类--细胞     系统--人    ...

随机推荐

  1. Ribbon Workbench The plug-in execution failed because the Sandbox Client encountered an error during initialization

    使用 Ribbon Workbench打开解决方案时,出现The plug-in execution failed because the Sandbox Client encountered an ...

  2. JQuery 删除子元素

    删除元素/内容 如需删除元素和内容,一般可使用以下两个 jQuery 方法: remove() - 删除被选元素(及其子元素) empty() - 从被选元素中删除子元素 $("#div1& ...

  3. C#截取字符串按字节截取SubString

    public static string CutByteString(string str,int len)     {       string result=string.Empty;// 最终返 ...

  4. pthread线程特定数据

    举个栗子 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/t ...

  5. Linux coredump 的打开和关闭

    (转载自 http://blog.sina.com.cn/s/blog_6b3765230100lazj.html) ulimit -c 输出如果为0,则说明coredump没有打开 ulimit - ...

  6. Django-models的字段类型

    model的field类型 1.models.AutoField   ---自增列 = int(11)    如果没有的话,默认会生成一个名称为 id 的列,如果要显示的自定义一个自增列,必须将给列设 ...

  7. Java - 29 Java 序列化

    Java 提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据.有关对象的类型的信息和存储在对象中数据的类型. 将序列化对象写入文件之后,可以从文件中读取 ...

  8. Motherboard Chipsets and the Memory Map.主板芯片组与内存映射

    原文标题:Motherboard Chipsets and the Memory Map 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限,只好挑一些国外 ...

  9. hive lateral view 与 explode详解

    ref:https://blog.csdn.net/bitcarmanlee/article/details/51926530 1.explode hive wiki对于expolde的解释如下: e ...

  10. 零基础学习python_列表和元组(10-13课)

    一时兴起今天又回过头来补一下列表和元组,先来说说列表哈,列表其实是python最经常用到的数据类型了,不仅经常用还很强大呢,这个跟C语言里面的数组是类似的,列表当然也可以增删改查,不过我可没打算用之前 ...