<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="domain1"/>
</beans>
@Component(value = "user")
@Scope(value="singleton")
注解有四个,但是却别都不大:
注解的时候 set 方法也无需生成,直接注解就能搞定。 直接在属性上面写@Autowired或者@Resource(name = “”) 第一种方式我们不用去指定对象的 id 值,而第二个需要些 id 值。主要用的就是第二个,因为他更加的严谨。
aop 也叫做面向切面编程,或者面向方面编程。他的主要方式就是横向抽取,与以前的继承方式的纵向抽取不同。 这里就主要说说横向抽取,aop 主要还是用了动态代理的方式来拓展对象的功能。而动态代理分为两种:
增强类:
package aop;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
public class Improve {
public void before_say(){
System.out.println("before");
}
public void after_say(){
System.out.println("after");
}
public void around_say(MethodInvocationProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("before");
joinPoint.proceed();
System.out.println("after");
}
}
被增强类
package aop;
public class User {
public void say(){
System.out.println("hello");
}
}
配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">
<bean id="improve" class="aop.Improve"/>
<aop:config>
<!--配置切入点 方法-->
<aop:pointcut id="point1" expression="execution(* aop.User.say(..))"/>
<!--配置切面 应用增强-->
<aop:aspect ref="improve">
<aop:before method="before_say" pointcut-ref="point1"/>
</aop:aspect>
</aop:config>
<bean id="user" class="aop.User"/>
</beans>
导包: