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. 一条简单的更新语句,MySQL是如何加锁的?

    看如下一条sql语句: # table T (id )) delete : MySQL在执行的过程中,是如何加锁呢? 在看下面这条语句: : 那这条语句呢?其实这其中包含太多知识点了.要回答这两个问题 ...

  2. Go语言系列:(2)go get 命令介绍

    Go语言的代码被托管于 Github.com 网站,该网站是基于 Git 代码管理工具的,很多有名的项目都在该网站托管代码.其他类似的托管网站还有 code.google.com.bitbucket. ...

  3. VSCode+C++环境搭建

    date: 2019-10-05 VSCode+C++环境搭建 其实并不完整,毕竟我也只是一个OIer,并不会很高深的东西.(众所周知,OIer主业是软件开发) 安装VSCode 下载安装包 这个很简 ...

  4. OpenGL 之 Compute Shader(通用计算并行加速)

    平常我们使用的Shader有顶点着色器.几何着色器.片段着色器,这几个都是为光栅化图形渲染服务的,OpenGL 4.3之后新出了一个Compute Shader,用于通用计算并行加速,现在对其进行介绍 ...

  5. 信号处理函数陷阱:调用malloc导致死锁[转]

    概览 因malloc是加锁的,上网了解的相关信息,额外了解到信号处理规范使用,mark 正文 在执行malloc的过程中,跳转到了信号处理函数中.而信号处理函数在调用某个系统api时,内部又调用了ma ...

  6. SpringBoot开发准备工作,保存备用,

    application.properties server.port=8080 spring.thymeleaf.prefix = classpath:/static/ spring.thymelea ...

  7. 剑指Offer-44.翻转单词顺序列(C++/Java)

    题目: 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思.例如,“student. ...

  8. 第04组 Beta冲刺(3/4)

    队名:斗地组 组长博客:地址 作业博客:Beta冲刺(3/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.分配展示任务 2.收集各个组员的进度 3.写博客 展示GitHub当日代码/文档 ...

  9. python 读取ini 配置文件

    安装 pip install configparser 1 配置文件 config.ini: [MysqlDB]user=rootpasswd=123456sport=3306db_name=my_d ...

  10. python是什么?python能做什么?

    人生苦短,我用python. python是什么? Python是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. python语言有以下特点: 易于学习.Python有相对较少的关键字 ...