spring入门

1.第一个例子

1)定义接口

ServiceBean.java

public interface ServiceBean {

 public void addUser();

 public void deleteUser();

 public void updateUser();

 public void findUser();
}

2)实现接口

MyServiceBean.java


public class MyServiceBean implements ServiceBean {

 public void addUser() {
  System.out.println("addUser()");
 }

 public void deleteUser() {
  System.out.println("deleteUser()");
 }

 public void findUser() {
  System.out.println("findUser()");
 }

 public void updateUser() {
  System.out.println("updateUser()");
 }

}

 

3)编写spring配置文件beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <bean id="serviceTarget" class="com.dexter.service.MyServiceBean" />
 </beans>

 

4)请求spring生成servicebean

public class Main {

 public static void main(String[] args) {
  XmlBeanFactory xb = new XmlBeanFactory(new ClassPathResource("beans.xml"));
  ServiceBean sb = (ServiceBean)xb.getBean("service");
  sb.addUser();
  xb.destroySingletons();
 }
}

 

2.初步spring aop

1)编写一个LogAdvisor类,这个类是实现MethodBeforeAdvisor接口,实现其中的方法,这个方法在主业务方法 被调用前调用。

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class LogAdvisor implements MethodBeforeAdvice {

 @Override
 public void before(Method m, Object[] args, Object target)
   throws Throwable {
  System.out.println("[Log] " + target.getClass().getName() + "." + m.getName() + "()");
 }
}

 

2)编写beans.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <bean id="serviceTarget" class="com.dexter.service.MyServiceBean" />
 <bean id="logAdvisor" class="com.dexter.service.LogAdvisor"></bean>

 

<!--请求ProxyFactoryBean类生成代理对象-->

 <bean id="service"
  class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="proxyInterfaces">
   <value>com.dexter.service.ServiceBean</value>
  </property>
  <property name="target">
   <ref local="serviceTarget" />
  </property>
  <property name="interceptorNames">
   <list>
    <value>logAdvisor</value>
   </list>
  </property>
 </bean>
</beans>

 

3)调用

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Main {

 public static void main(String[] args) {
  XmlBeanFactory xb = new XmlBeanFactory(new ClassPathResource("beans.xml"));
  ServiceBean sb = (ServiceBean)xb.getBean("service");
  sb.addUser();
  xb.destroySingletons();
 }
}

 

3.编写方法过滤器,编写MethodAdvisor类

1) 编写MethodAdvisor.java


public class MethodAdvisor implements MethodInterceptor {

 @Override
 public Object invoke(MethodInvocation m) throws Throwable {
  m.proceed();
  System.out.println("MethodAdvisor invoked,"+m.getMethod().getName());
  return "MethodAdvisor invoked";
 }

}


2)写配置文件,请求RegexpMethodPointcutAdvisor类过滤指定的方法

<bean id="methodAdvisorTarget"
  class="com.dexter.service.MethodAdvisor">
 </bean>

<bean id="methodAdvisor"
  class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
  <property name="advice">
   <ref local="methodAdvisorTarget" />
  </property>
  <property name="patterns">
   <list>

   <!--过滤addUser这个指定的方法-->
    <value>.*addUser</value>
   </list>
  </property>
 </bean>

 

<bean id="service"
  class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="proxyInterfaces">
   <value>com.dexter.service.ServiceBean</value>
  </property>
  <property name="target">
   <ref local="serviceTarget" />
  </property>
  <property name="interceptorNames">
   <list>
    <value>logAdvisor</value>
    <value>methodAdvisor</value>
   </list>
  </property>
 </bean>

 

4.异常aop

1)编写ExceptionAdvisor类

import org.springframework.aop.ThrowsAdvice;

public class ExceptionAdvisor implements ThrowsAdvice {
 public void afterThrowing(RuntimeException re) throws Throwable {
  System.out.println("[Exception] " + re.getMessage());
 }
}

 

2)配置beans.xml

<bean id="service"
  class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="proxyInterfaces">
   <value>com.dexter.service.ServiceBean</value>
  </property>
  <property name="target">
   <ref local="serviceTarget" />
  </property>
  <property name="interceptorNames">
   <list>
    <value>logAdvisor</value>
    <value>passwordAdvisor</value>
    <value>exceptionAdvisor</value>
   </list>
  </property>
 </bean>

 

3)如果在业务方法执行时候有异常,那么ExceptionAdvisor的afterThrowing方法被执行

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值