Spring IOC操作(注解方式)和AOP(二)

本文介绍Spring框架中注解的使用方法,包括对象创建、属性注入等,并详细解析了面向切面编程(AOP)的基本概念及其实现方式。

Spring的bean 管理(注解)

注解:代码里面特殊的标记,使用注解可以完成功能。注解写法:@注解名称(属性名称=属性值)。注解可以用在方法,类,属性上。

Spring注解开发
1,导入依赖,创建类方法

@Component(value="bookDao")
public class BookDao {
	public void add(String bname) {
		System.out.println(bname);
	}
}
@Service("bookService")
public class BookService {
    @Autowired
    private BookDao bookdao;
    public void add(String bname){
        bookdao.add(bname);
    }
}

2,创建配置文件,开启注解扫描,到包里面扫描类,方法,属性上面是否有注解

<context:component-scan base-package="dao,service"></context:component-scan>

3,测试注解开发能否得到对象

public class BookServlet {
	public static void main(String[] args) {
	ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	BookService bs=(BookService) ctx.getBean("bookService");
	bs.add("天龙八部");
 }
}

 注解创建对象
1,在创建对象的类上面加个注解@Component(value="user")   //相当于<bean id="user" class="">
2,创建对象有4 个注解:
(1)@controller:web层(2)@service :业务层(3)@Repository:持久层(4)@Component
目前这个四个注解作用都一样。

单例和多例注解表示?   @Scope(value="prototype"):prototype多例,单例是默认的,可以不用写注解。


注解注入属性(1)(2)

@Service("bookService")
public class BookService {
    @Autowired
    private BookDao bookdao;
    public void add(String bname){
        bookdao.add(bname);
    }
}

(1)@Autowired  //自动注入属性,如上面例子:private BookDao bookdao;//根据BookDao根据类名找到类对应的对象。
(2)@Resource(name="userDao")  //自动注入属性,name属性写的是注解里面name的值。


Spring中配置文件和注解混合使用
一般创建对象操作使用配置文件实现:<bean></bean>,而属性的注入会使用注解:@Autowired 实现。如:

<context:component-scan base-package="dao,service"></context:component-scan>
<bean id="studentDao" class="dao.StudentDao"></bean>
<bean id="personDao" class="dao.PersonDao"></bean>
@Service
public class StudentDao {
	public void pre_stu(){}
}
@Service
public class PersonDao {
	@Autowired
	StudentDao studentDao;
	public void perAdd(){
	}
}

Spring AOP概述

1,aop:面向切面编程,扩展功能不修改源代码能实现,aop采取横向抽取机制,取代了传统纵向基础体系重复代码。

2,底层原理

3,aop操作相关术语

public interface BookDao{
    public void add()
    public void delete();
}

连接点:类里面那些方法可以被只能去,这些方法称为连接点
切入点:在类里面可以有很多方法被增强,如上Book类只想增强类里面的add方法和delete方法,实际增强的方法称为切入点。
通知/增强:增强的逻辑,比如扩展加入日志功能。
前置通知:在方法之前执行
后置通知:在方法之前执行
异常:在方法出现异常执行
最终:在方法之后执行
环绕:在方法之前和之后执行
切面:把增强应用到具体方法上的过程叫切面,即把增强用到切入点的过程。

 

4,aop的操作

在spring里面进行aop操作,使用aspectj实现,
(1)aspectj不是spring一部分,但和spring一起使用进行aop操作
(2)spring2.0以后才增加了对aspectj的支持
使用aspectj实现aop有两种方式:(1)基于aspectj的xml配置 (2)基于aspectj的注解方式


aop操作(xml方式)

public class StudentDao {
	public void pre_stu(){
		System.out.println("i am a stu...pre..");
	}
}
public class PersonDao {
	public void perAdd(){
		//在这之前要加入一个方法输出,但是不改变本方法代码
		System.out.println("i am a person...");
	}
}

 

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
        
<context:component-scan base-package="dao,service"></context:component-scan>
<bean id="studentDao" class="dao.StudentDao"></bean>
<bean id="personDao" class="dao.PersonDao"></bean>

<aop:config>
	<!-- 切入点,被增强的方法,表示person类的preAdd方法被增强 -->
	<aop:pointcut expression="execution(* dao.PersonDao.perAdd(..))" id="pc"/>
	<!-- 增强类 -->
	<aop:aspect ref="studentDao">
		<!-- method:增强类里面使用哪个方法作为前置。 -->
			<aop:before method="pre_stu" pointcut-ref="pc"/>
			<aop:before method="stu_add" pointcut-ref="pc"/>
	</aop:aspect>
</aop:config>
</beans>

切入点 (实际增强的方法)execution()函数,通过该函数可以定义切点的方法切入。

语法:
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
例子:
匹配所有类的public方法 execution(public * *(..))
匹配指定包下所有类的方法 execution(* com.abc.doa.*(..)) //不包含子包
execution(* cn.abc.dao..*(..)) // ..* 表示包,子孙包下所有类
匹配指定类所有方法 execution(* com.abc.service.BookService.*(..))
匹配所有save开头的方法 execution(* save*(..))
匹配所有方法 execution(* *.*(..))


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值