SpringAop的依赖注入方式
作者:#0080FF
文章介绍了Spring框架的依赖注入(DI)和面向切面编程(AOP)的基本概念和实现方式,Spring通过IOC容器管理Bean的创建和依赖注入,并提供了多种AOP实现方式,包括代理机制和配置选项
SpringAop依赖注入
1.Spring
它是一个轻量级和IOC(DI)和AOP的 容器框架:
- IOC:控制反转 现在创建bean的方式,是交给spring帮我们创建。(理解)
- DI:依赖注入 spring它是容器框架,是一个bean(装对象的)容器框架,spring帮我创建好对象之后,在把我们创建好的对象 注入到相应的类里面
- AOP:面向切面编程,Spring框架的AOP机制可以让开发者把业务流程中的通用功能抽取出来,单独编写功能代码。在业务流程执行过程中,Spring框架会根据业务流程要求,自动把独立编写的功能代码切入到流程的合适位置。
AOP使用场景:
- 事务管理
- 日志管理
- 性能控制
2.spring的DI依赖注入
2.1.根据构造器注入:
- index索引注入
<bean id="MyBean" class="cn.itsource._01_.MyBean"> <constructor-arg index="0" value="666" /> <constructor-arg index="1" value="石娃娃" /> </bean>
- name 参数名字注入
<bean id="MyBean" class="cn.itsource._01_.MyBean"> <constructor-arg name="id" value="1" /> <constructor-arg name="name" value="石大娃" /> </bean>
- type类型注入
<bean id="MyBean" class="cn.itsource._01_.MyBean"> <constructor-arg type="java.lang.Long" value="1" /> <constructor-arg type="java.lang.String" value="石二娃" /> </bean>
2.2类里面属性注入
<property name="id" value="20"></property> <!-- name --> <property name="name" value="冯增昕"></property> <!-- String数组--> <property name="arrys" value="1999,12,05"></property> <!--list set--> <property name="list"> <list> <value>air jordan 1</value> <value>黑曜石</value> </list> </property> <property name="set"> <set> <value>adiads</value> <value>Yeezy750</value> </set> </property> <property name="helloBeanList"> <list> <ref bean="hb"></ref> <bean class="cn.itsource._01di.HelloBean"></bean> </list> </property> <property name="helloBeanSet"> <set> <ref bean="hb"></ref> <bean class="cn.itsource._01di.HelloBean"></bean> </set> </property> <!-- map--> <property name="hp"> <map> <entry key="star" value="星星"></entry> <entry key="sunshine " value="阳光"></entry> </map> </property> <!--Properties 不支持中文 --> <property name="pro1"> <value> driverClassName=com.mysql.jdbc.Driver username=yu玉 </value> </property> <!-- 支持中文--> <property name="pro2"> <props> <prop key="driverClassName">com.mysql.jdbc.Driver</prop> <prop key="username">杰伦</prop> </props> </property>
2.3XML版本的自动注入
default-autowire="byType" default-autowire="byName"
byType根据类型,byName根据名字
2.4XML版本的注解引入
<context:component-scan base-package="cn.itsource._03_autoxml"></context:component-scan> <!-- 扫描注解: @Component, @Repository, @Service,@Controller -->
如果一个接口有两个实现情况下:
- 方案一:一个接口有多个实现的情况下面 通过名字去区分
- 方案二:通过Resource这个注解
区别:
- @Autowired和@Qualifier 都是属于spring的注解
- Resource使用jdk的注解 推荐使用Autowired和Qualifier,可以和spring进行无缝衔接
- autowired默认是根据类型匹配,如果类型匹配不上在根据名字匹配
- 而Resource默认根据名字匹配,名字匹配不上就匹配类型
如果你用的是Spring的注解,尽量全部使用Spring的标签
- AutoWired与Resource都表示注入对象
- AutoWired他是SPring提供的注解,@Resource是sun公司提供注解
- AutoWired默认是根据类型匹配,如果类型匹配不上在根据名字匹配
- 建议大家使用Autowried 因为他是Spring提供的注解,他能和Spring进行无缝衔接集成,
- 而Resource默认根据名字匹配,名字匹配不上就匹配类型
3.Spring里面的AOP功能
3.1 AOP简述
- 定义: 面向切面编程(面向方面编程)
- Spring使用动态代理织入。
- Spring支持我们在方法的前后添加增强代码。
- Aop的功能作用Spring的两个核心之一,也可以是Spring非常重要的功能。
3.2 Spring中aop的实现
1.若目标对象实现了若干接口,spring使用JDK的java.lang.reflect.Proxy类代理。
2.若目标对象没有实现任何接口,spring使用CGLIB库生成目标对象的子类。
3.使用该方案时需要注意:
- 对接口创建代理优于对类创建代理,因为会产生更加松耦合的系统。对类代理是让遗留系统或无法实现接口的第三方类库同样可以得到通知,这种方案应该是备用方案。
- 标记为final的方法不能够被通知。spring是为目标类产生子类。任何需要被通知的方法都被复写,将通知织入。final方法是不允许重写的。
3.3 Springaop的配置
<context:component-scan base-package="cn.itsource._04_aopxml"></context:component-scan> <aop:config> <aop:pointcut id="pointcut" expression="execution(* cn.itsource._04_aopxml.service.I*Service.*(..))"></aop:pointcut> <!--配置通知--> <aop:aspect ref="txManager"> <!--前置通知--> <!--<aop:before method="begin" pointcut-ref="pointcut"></aop:before>--> <!--<!–后置通知–>--> <!--<aop:after-returning method="commit" pointcut-ref="pointcut"></aop:after-returning>--> <!--<!–关闭通知–>--> <!--<aop:after method="close" pointcut-ref="pointcut"></aop:after>--> <!--<!–异常–>--> <!--<aop:after-throwing method="rollback" throwing="e" pointcut-ref="pointcut"></aop:after-throwing>--> <!--环绕通知--> <aop:around method="surround" pointcut-ref="pointcut"></aop:around>
3.4 注解方式的配置
@Component @Aspect public class TxManager { @Pointcut("execution(* cn.itsource._05_aopanno.service.I*Service.*(..))") public void pointcut(){}
4.Bean的创建方式
4.1 Bean创建方式一
通过无参数的构造方法来创建bean --最常用方式
配置
<bean id="mybean" class="cn.itsource._06_bean.Mybean"></bean>
4.2 Bean创建方式二
FactoryBean
public class MybeanFactoryBean implements FactoryBean<Mybean> { public Mybean getObject() throws Exception { return new Mybean("石楚玉", "仙桃之光"); } public Class<?> getObjectType() { return Mybean.class; } //是否单例 public boolean isSingleton() { return true; }
4.3 Bean的创建方式三
通过类里面定义静态方法 来获取bean
public class MybeanFactory { public static Mybean getBean(){ return new Mybean(); } }
bean class="cn.itsource._06_bean.MybeanFactory" factory-method="getBean"></bean>
4.4 Bean的创建方式四
public class MybeanFactory1 { //普通方式 public Mybean getBean1(){ return new Mybean(); } }
<bean class="cn.itsource._06_bean.MybeanFactory1" id="mybeanFactory1"></bean> <bean factory-bean="mybeanFactory1" factory-method="getBean1"></bean>
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。