IOC(控制反转)/DI(依赖注入)
同一概念,不同理解。
IOC:将创建对象、赋值属性的方式进行了反转,由用户自己创建反转为通过SpringIOC容器创建,用户通过getBean()获取
DI:将属性值注入给属性,将属性注入给Bean,将Bean注入给IOC容器.
Web项目获取Spring容器中的Bean对象:
重写Servlet的init()方法
@Override
public void init() throws ServletException{
ApplicationContext context = WebApplicationContextUnitls.getWebApplicationContext(this.getServletContext);
testService = (TestService)context.getBean("testService");
}
setter注入
通过Java反射调用无参构造函数
application-config.xml配置bean,赋值时如果是简单类型用value='',如果是依赖对象类型用ref='',或者配置autowire实现自动装配,byName自动寻找与该类属性名匹配的bean的id,当IOC容器中只有一个与类的ref属性类型匹配的bean的class时才能使用byType自动寻找,constructor自动寻找bean的类型与该类的构造方法参数的类型一致,可在xml配置文件的xmlns中配置default-wire="byName" 设置全局自动装配,bean标签中再设置可覆盖,自动装配降低可读性.
<bean id='' class='对应的实体类类路径' autowire='byName|byType|constructor'>
<property name='类的成员变量名' value='' />
</bean>
构造器注入
application-config.xml配置bean
<constructor-arg value='' index='' type='' name=''/>
在对应类中新建带参数的构造器
p命名空间注入
在application-config.xml开头引入p命名空间
xmlns:p="http://www.springframework.org/schema/p"
<bean id='' class='' p:属性名='属性值' p:对象属性名-ref='属性值'/>
注解注入
在application-config.xml开头引入context,xmlns:context="http://www.springframework.org/schema/context" 然后加入<context:component-scan base-package="要扫描的包"/>
通过在类上加@Component/@Repository/@Serivce/@Controller自动将该类加入IOC容器
事务
1、在application-config.xml中加入tx的命名空间 xmlns:tx="http://www.springframework.org/schema/tx"
2、在配置连接数据库的bean
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value=""/>
<property name="url" value=""/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>
3、配置事务管理器txManager
<bean id="txManager" class="org.springframework.jdbc.dataSource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
4、对事务的支持<tx:annotation-driven transaction-manager="txManager"/>
5、在需要配置为事务的方法前加上@Transactional注释
AOP面向切面编程
切面
实现增强的方法
切入点
增强方法切入目标类方法的位置
通知类型
前置通知,后置通知,异常通知,最终通知,环绕通知
实现接口实现AOP
1、application-config.xml开头引入aop,xmln:aop="http://www.springframework.org/schema/aop"
2、编写切面
前置通知实现接口org.springframework.aop.MethodBeforeAdvice中的before()方法
后置通知org.springframework.aop.AfterReturningAdvice中的afterreturning()方法
异常通知org.springframework.aop.ThrowsAdvice
环绕通知 前后异常最终都包括 org.aopalliance.intercept.Methodinterceptor中的invoke()方法,通过拦截器实现,用invoke()执行方法,并获取方法的各项属性
3、把切面和要增强的方法配置为bean
4、在xml中配置切面和切入点
<aop:config>
<aop:pointcut expression="execution(要增强的方法1的全类路径(..)) or execution(要增强的方法2的全类路径(..))" id="pointCutId"/>
<aop:advice-ref="增强方法beanId" pointcut-ref="pointCutId"/>
</aop:config>
注解实现AOP
1、application-config.xml中开启对AOP支持,aop:aspectj-autoproxy/
2、在通知类上加@Component("beanId")声明该类是一个Bean,@Aspect注释声明该类是通知类,在具体方法上加注释@Before(execution(要增强的方法1的全类路径(..))声明该方法是前置通知,@AfterReturning后置,@AfterThrowing异常,@After最终,@Aroud环绕,通过切面方法中的JoinPoint对象获取执行方法的各项属性,环绕通知用ProceedingJoinPoint
@Component("beanId")
@Aspect
public class AspectTest(){
@Before("execution(public * service(..))")
public void aspectBefore(JoinPoint jp){
System.out.println(jp.getTarget());
}
}
schema实现AOP
1、编写切面,与注释实现类似,区别在于无注释
public class AspectTest(){
public void aspectBefore(JoinPoint jp){
System.out.println(jp.getTarget());
}
}
2、通过bean标签将切面纳入IOC容器
3、在xml中配置切面和切入点
<aop:config>
<aop:pointcut expression="execution(要增强的方法1的全类路径(..)) or execution(要增强的方法2的全类路径(..))" id="pointCutId"/>
<aop:aspect ref="beanId">
<aop:before method="aspectBefore" pointcut-ref="pointCutId"/>
<aop:after-returning method="后置通知方法名" pointcut-ref="pointCutId"/>
</aop:aspect>
</aop:config>
浙公网安备 33010602011771号