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. c++ 拷贝构造函数(重点在内含指针的浅拷贝和深拷贝)

    今天同事问了一个关于拷贝构造函数的问题,类中包含指针的情况,今天就来说说c++的拷贝构造函数. c++的拷贝构造函数是构造函数的一种,是对类对象的初始化,拷贝构造函数只有一个参数就是本类的引用. 注意 ...

  2. 深挖的Java源代码之Integer.parseInt()vs Integer.valueOf()

    Integer.parseInt()和Integer.valueOf()都是用来将String转换为Int的,但是为什么Java会提供两个这样的方法呢,他们如果是同样的操作,岂不是多此一举? 我们来深 ...

  3. Linux_更改时区和利用Crontab同步时间

    一.更改时区 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 二.Crontab时间同步 crontab -e   #crontab编辑 */5 ...

  4. 011.Kubernetes二进制部署kube-scheduler

    一 部署高可用kube-scheduler 1.1 高可用kube-scheduler介绍 本实验部署一个三实例 kube-scheduler 的集群,启动后将通过竞争选举机制产生一个 leader ...

  5. C# 让你解决方案乱七八糟的DLL放入指定文件夹

    嗯,大家的解决方案可能会有许多dll,这样不美观,而且也麻烦. 很多小白都不知道如何将这些dll放到如自己程序的bin文件夹下. 本渣今天来试着将dll复制到指定的文件夹下~ 比如我之前做的一个Win ...

  6. 加权无向图 最小生成树 Prim算法 延迟版和即时版 村里修路该先修哪

    本次要解决的问题是:你们村里那些坑坑洼洼的路,到底哪些路才是主干道? 小明:肯定是哪里都能到得了,并且去哪里都相对比较近,并且被大家共用程度高的路是啊! 具体是哪几条路呢?今天就可以给出准确答案 最小 ...

  7. 《Web Development with Go》Mangodb查询collection内所有记录

    相当于select * from table; package main import ( "fmt" "log" "time" " ...

  8. git 文件补录和别名

    当git当前的版本要有部分忘记提交或新修改的东西包含在已提交(最近一次提交的版本)的版本时,我们可以进行文件补录 命令:git commit --amend -a 1.git log 查看最后一次提交 ...

  9. 一些你不知道的js特性【一】

    关于js 我们知道完整的js包括三个方面ECMAScript.DOM(文档对象模型).BOM(浏览器对象模型). ECMAScript定义了与宿主无关的预言基础,比如:语法(包含正则语法).类型.语句 ...

  10. 最强Linux shell工具Oh My Zsh 指南

    引言 笔者已经使用zsh一年多了,发现这个东东的功能太强大了.接下来,给大家推荐一下. 以下是oh-my-zsh部分功能 命令验证 在所有正在运行的shell中共享命令历史记录 拼写纠正 主题提示(A ...