1、定制任务。2、active MQ。 3、邮件。 4、配置。 5、整合
spring 包括IOC和AOP
IOC
IOC就是控制反转(DI依赖注入),IOC可以提高重用性和重构性。在applicationContext.xml中每个bean都是单例的
如果不想使用单例可以在bean中加入 scope="prototype" 属性。
依赖注入的方式:
1.setter方法注入 在bean中加入property
2.构造方法注入在bean中加入<constructor-arg ref=""/>
3.接口注入
在xml的模版配置加入default-autowire="byName",在一个bean中有一个对象的话可以不用重新配置,但是在这个
bean对应的类中必须有一个属性,该属性的名称应与xml中该对象对应的bean 的id名称一致。
1: BeanFactory factory = new XmlBeanFactory(new ClassPathResource("org/ioc3/cowry.xml"));2:3:4:5: ApplicationContext factory = new ClassPathXmlApplicationContext(
6: "applicationContext.xml");
7:8:
AOP
aop是面向切面的编程,提高了代码的重构性,降低了耦合性。
通知分为
前置通知
1: public class LoginMethodBeforeAdvice implements MethodBeforeAdvice {2:3: public void before(Method m, Object[] args, Object target) throws Throwable {4: System.out.println("[LoginMethodBeforeAdvice] User " + args[0]
5: + " try to " + m.getName() + "...");6: }7: }后置通知
1: public class LoginAfterReturningAdvice implements AfterReturningAdvice {2:3: public void afterReturning(Object returnValue, Method method,4: Object[] args, Object target) throws Throwable {
5: System.out.println("[LoginAfterReturningAdvice] User " + args[0] + " "6: + method.getName() + " successfully.");
7: }8:9: }环绕通知
1: public class LoginAroundAdvice implements MethodInterceptor {2:3: public Object invoke(MethodInvocation invocation) throws Throwable {4: Object[] args = invocation.getArguments();5: if (args[0].equals("admin"))6: throw new SecurityException("Rejected");7: return invocation.proceed();
8: }9:10: }异常通知
1: public class LoginThrowsAdvice implements ThrowsAdvice {2:3: public void afterThrowing(Method m, Object[] args, Object target,4: Throwable subclass) {5: System.out.println("[LoginThrowsAdvice] An exception occur: "
6: + target.getClass().getSimpleName() + "‘s " + m.getName()
7: + " method has exception---"
8: + subclass.getClass().getSimpleName());9: }10: }advisor通知
1: <bean id="loginBeforeAdvisor"2: class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">3: <property name="mappedName" value="login" />4: <property name="advice">5: <bean class="org.aop.LoginMethodBeforeAdvice" />6: </property>7: </bean>8:9: <bean id="createAfterAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">10: <property name="advice" >11: <bean class="org.aop.LoginAfterReturningAdvice" />12: </property>13: <property name="pattern" value=".*create.*" />14: </bean>
将目标织入到目标对象返回一个和目标对象拥有相同接口的代理对象,如果没有接口默认用CGLIB方式进行代理
CGLIB代理
1: <bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean">2: <property name="interfaces">3: <list>4: <value>org.aop.UserService</value>5: </list>6: </property>7: <property name="interceptorNames">8: <list>9: <value>loginAroundAdvice</value>10: </list>11: </property>12: <property name="target" ref="userServiceTarget" />13: </bean>
事务
ACID:原子性,一致性,隔离性,持久性