1、IOC注解

  1.1 IOC和DI的注解

   IOC:

      @Component:实现Bean组件的定义

      @Repository:用于标注DAO类,功能与@Component作用相当

      @Service:用于标注业务类

      @Controller:用于标注控制器

DI:

      @Resource(name="userService")默认ByName方式,如果name确实默认按照ByType方式注入

      @Autowired,默认ByType方式,如果出现同名类,则不能按照Type进行注入,需要使用@Qualifier 指明ID

  1.2 IOC注解实现添加用户案例

  (1)实体类

package cn.spring.ioc.entity;

import java.io.Serializable;

public class UserInfo implements Serializable {
private Integer user_id;
private String user_name; public Integer getUser_id() {
return user_id;
} public void setUser_id(Integer user_id) {
this.user_id = user_id;
} public String getUser_name() {
return user_name;
} public void setUser_name(String user_name) {
this.user_name = user_name;
}
}

  (2)ApplicationContext.xml

    (最好先去完成ApplicationContext配置文件,以免忘记)

<!--扫描注解:包扫描器-->
<context:component-scan base-package="cn.spring"></context:component-scan>

    (3)IUserInfoMapper接口

package cn.spring.ioc.mapper;

import cn.spring.ioc.entity.UserInfo;

public interface IUserInfoMapper {
public int addUser(UserInfo info);
}

  (4)IUserInfoMapperImpl接口实现类

package cn.spring.ioc.mapper.impl;

import cn.spring.ioc.entity.UserInfo;
import cn.spring.ioc.mapper.IUserInfoMapper;
import org.springframework.stereotype.Repository; /**
* dao层标识 @Repository
*/
@Repository
public class IUserInfoMapperImpl implements IUserInfoMapper { @Override
public int addUser(UserInfo info) {
System.out.println("添加成功!");
return 1;
}
}

  (5)IUserInfoService

package cn.spring.ioc.service;

import cn.spring.ioc.entity.UserInfo;

public interface IUserInfoService {
public int addUser(UserInfo info);
}

  (6)IUserInfoServiceImpl实现类

package cn.spring.ioc.service;

import cn.spring.ioc.entity.UserInfo;

public interface IUserInfoService {
public int addUser(UserInfo info);
}

  (7)测试类

package cn.spring;

import cn.spring.ioc.entity.UserInfo;
import cn.spring.ioc.service.IUserInfoService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class IocTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); IUserInfoService bean = (IUserInfoService)context.getBean("iUserInfoServiceImpl");
bean.addUser(new UserInfo());
}
}

  (8)控制台

      

2、AOP注解

  2.1 实现AOP的注解

    @Aspect 声明切面

    @Ponitcut 声明公共的切点表达式

    @Before 前置增强

    @AfterReturning 后置增强

    @Around 环绕增强

    @AfterThrowing 异常抛出增强

    @After 最终增强

  2.2  AOP注解实现前后置增强

  (1)IdoSomeService层

package cn.spring.aop;

import org.springframework.stereotype.Service;

/**
* 业务类
*/
@Service("idoSomeService")
public class IdoSomeService {
public void doSome(){
System.out.println("业务类当中的doSome方法");
}
public void say(){
System.out.println("业务类当中的say方法");
}
}

  (2)MyAdvice增强类

package cn.spring.aop;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; /**
* 增强类,把增强类注入到Spring容器
*/
@Aspect
@Component
public class MyAdvice {
//定义一个空方法,为了可以应用切点表达式
@Pointcut("execution(* *..aop.*.*(..))")
public void pointCut(){} //自定义增强方法
@Before("pointCut()")
public void before(){
System.out.println("=====前置增强=====");
} //自定义增强方法
@AfterReturning("pointCut()")
public void after(){
System.out.println("=====后置增强=====");
}
}

  (3)applicationContext.xml配置文件

<!--扫描注解:包扫描器-->
<context:component-scan base-package="cn.spring"></context:component-scan> <!--开启AOP注解支持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

  (4)测试类

package cn.spring;

import cn.spring.aop.IdoSomeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AopTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
IdoSomeService idoSomeService = (IdoSomeService)context.getBean("idoSomeService");
idoSomeService.doSome();
idoSomeService.say();
}
}

      (5)控制台

   

  

Spring中注解方式实现IOC和AOP的更多相关文章

  1. 用通俗的语言解释 Spring 中的 DI 、IOC 和AOP概念

    DI 所谓依赖,从程序的角度看,就是比如A要调用B的方法,那么A就依赖于B,反正A要用到B,则A依赖于B.所谓倒置,你必须理解如果不倒置,会怎么着,因为A必须要有B,才可以调用B,如果不倒置,意思就是 ...

  2. (转)使用Spring的注解方式实现AOP入门

    http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...

  3. Spring 使用纯注解方式完成IoC

    目录 创建一个简单的Person类 使用xml方式配置Spring容器并获取bean的过程 创建xml配置文件 进行测试 使用纯注解方式配置Spring容器并获取bean的过程 创建spring配置类 ...

  4. (转)使用Spring的注解方式实现AOP的细节

    http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...

  5. 【Spring】XML方式实现(无参构造 有参构造)和注解方式实现 IoC

    文章目录 Spring IoC的实现方式 XML方式实现 通过无参构造方法来创建 1.编写一个User实体类 2.编写我们的spring文件 3.测试类 UserTest.java 4.测试结果 通过 ...

  6. Spring 框架的概述以及Spring中基于XML的IOC配置

    Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...

  7. 【SSH系列】spring中为什么要使用IOC

    开篇前言 在前面的博文中,小编主要简单的介绍了spring的入门知识,随着学习的深入,我们知道spring最核心的两大技术,IOC和AOP,这两个技术也是spring最耀眼的地方,在后续的博文中小编将 ...

  8. mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类

    相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...

  9. spring中注解式事务不生效的问题

    常用的解决方法可以百度,我针对我的问题描述一下 Mysql中InnoDB引擎才支持事务, MyISAM不支持事务. 当你尝试了各种方法解决spring中注解式事务不生效时, 一定要查看一下数据库中表的 ...

随机推荐

  1. pgwSlideshow.js

    <!DOCTYPE html> <html> <head id="Head"> <meta http-equiv="Conten ...

  2. OGG For Oracle To PostgreSQL

    本文档描述OGG(Oracle goldengate)为Oracle同步到PostgreSQL数据库配置.在目前去“IOE”潮流.PostgreSQL确实是Oracle最好的替代品之一. 实验环境如下 ...

  3. 2019/12/13学习内容摘要(Linux磁盘管理①)

    一,查看磁盘或目录容量 1.命令df  查看已挂载磁盘的总容量,使用容量,剩余容量等,可以不加任何参数,默认以KB为单位 选项[-i] 表示查看inodes的使用情况 [-h] 表示用合适的单位显示 ...

  4. 对numpy.meshgrid()理解

    一句话解释numpy.meshgrid()——生成网格点坐标矩阵.关键词:网格点,坐标矩阵 网格点是什么?坐标矩阵又是什么鬼?看个图就明白了: 图中,每个交叉点都是网格点,描述这些网格点的坐标的矩阵, ...

  5. InnoSetup跨脚本传参数

    需求:在a.iss脚本传递参数给b.iss 举例: a.iss:传程序安装路径给b.iss Parameters: /Path={app} b.iss:接收a.iss传过来的安装路径 DefaultD ...

  6. C#中实现文件重命名的方式

    场景 在C#中如果是删除文件的话可以直接使用 if (System.IO.File.Exists(fileName)) { System.IO.File.Delete(fileName); } 但是如 ...

  7. 通俗易懂,什么是.NET/.NET Framework/.NET Core/.Net Standard?

    什么是.NET?什么是.NET Framework?本文将从上往下,循序渐进的介绍一系列相关.NET的概念,先从类型系统开始讲起,我将通过跨语言操作这个例子来逐渐引入一系列.NET的相关概念,这主要包 ...

  8. SSM框架之spring(1)

    spring(1) 1.spring概述 Spring是分层的 Java SE/EE应用 full-stack 轻量级开源框架,以 IoC(Inverse Of Control:反转控制)和 AOP( ...

  9. Cesium专栏-气象雷达动图(附源码下载)

    Cesium Cesium 是一款面向三维地球和地图的,世界级的JavaScript开源产品.它提供了基于JavaScript语言的开发包,方便用户快速搭建一款零插件的虚拟地球Web应用,并在性能,精 ...

  10. 2. 移动安全渗透测试-(Android安全基础)

    2.1 Android系统架构 1.应用程序层 平时所见的一些java为主编写的App 2.应用程序框架层 应用框架层为应用开发者提供了用以访问核心功能的API框架 android.app:提供高层的 ...