【上机】实现工厂设计模式
详见-23GOF种设计模式之工厂模式
【上机】Spring 的 helloworld程序。
【上机】无参、有参构造方法管理对象
【上机】工厂方法管理对象
dao
/** * 账户的持久层接口 */ public interface IAccountDao { /** * 模拟保存账户 */ void saveAccount(); }
/** * 账户的持久层实现类 */ public class AccountDaoImpl implements IAccountDao { public void saveAccount(){ System.out.println("保存了账户"); } }
service
/** * 账户业务层的接口 */ public interface IAccountService { /** * 模拟保存账户 */ void saveAccount(); }
/** * 账户的业务层实现类 */ public class AccountServiceImpl implements IAccountService { // private IAccountDao accountDao = new AccountDaoImpl(); private IAccountDao accountDao = (IAccountDao)BeanFactory.getBean("accountDao"); // private int i = 1; public void saveAccount(){ int i = 1; accountDao.saveAccount(); System.out.println(i); i++; } }
vo
/** * 模拟一个表现层,用于调用业务层 */ public class Client { public static void main(String[] args) { //IAccountService as = new AccountServiceImpl(); for(int i=0;i<5;i++) { IAccountService as = (IAccountService) BeanFactory.getBean("accountService"); System.out.println(as); as.saveAccount(); } } }
factory
/** * 一个创建Bean对象的工厂 * * Bean:在计算机英语中,有可重用组件的含义。 * JavaBean:用java语言编写的可重用组件。 * javabean > 实体类 * * 它就是创建我们的service和dao对象的。 * * 第一个:需要一个配置文件来配置我们的service和dao * 配置的内容:唯一标识=全限定类名(key=value) * 第二个:通过读取配置文件中配置的内容,反射创建对象 * * 我的配置文件可以是xml也可以是properties */ public class BeanFactory { //定义一个Properties对象 private static Properties props; //定义一个Map,用于存放我们要创建的对象。我们把它称之为容器 private static Map<String,Object> beans; //使用静态代码块为Properties对象赋值 static { try { //实例化对象 props = new Properties(); //获取properties文件的流对象 InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties"); props.load(in); //实例化容器 beans = new HashMap<String,Object>(); //取出配置文件中所有的Key Enumeration keys = props.keys(); //遍历枚举 while (keys.hasMoreElements()){ //取出每个Key String key = keys.nextElement().toString(); //根据key获取value String beanPath = props.getProperty(key); //反射创建对象 Object value = Class.forName(beanPath).newInstance(); //把key和value存入容器中 beans.put(key,value); } }catch(Exception e){ throw new ExceptionInInitializerError("初始化properties失败!"); } } /** * 根据bean的名称获取对象 * @param beanName * @return */ public static Object getBean(String beanName){ return beans.get(beanName); } /** * 根据Bean的名称获取bean对象 * @param beanName * @return public static Object getBean(String beanName){ Object bean = null; try { String beanPath = props.getProperty(beanName); // System.out.println(beanPath); bean = Class.forName(beanPath).newInstance();//每次都会调用默认构造函数创建对象 }catch (Exception e){ e.printStackTrace(); } return bean; }*/ } service /** * 账户业务层的接口 */ public interface IAccountService { /** * 模拟保存账户 */ void saveAccount(); }
【上机】实现自动装配与自动扫描
【上机】练习注入集合 list set map
【上机】实现静态代理模式
【上机】jdk动态代理实现
【上机】cglib动态代理实现
【上机】AOP通知
不同类实现不同接口
<?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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 通过AOP方式给操作的方法加日志 --> <!-- 调用AOPInform01ServiceImpl中showInform方法 --> <bean id="show1" class="renjiale.service.impl.AOPInform01ServiceImpl"></bean> <!-- 配置前置通知的对象 --> <bean id="beforelogger" class="renjiale.util.BeforeLogger"></bean> <!-- 配置后置通知的对象 --> <bean id="afterlogger" class="renjiale.util.AfterReturingLogger"></bean> <!-- 配置环绕通知的对象 --> <bean id="aroundlogger" class="renjiale.util.AroundIntercepter"></bean> <!-- 配置异常抛出通知的对象 --> <bean id="throwlogger" class="renjiale.util.ThrowLogger"></bean> <aop:config> <aop:pointcut expression="execution(public void showInform())" id="AllInform"/> <!-- 前置通知 --> <aop:advisor advice-ref="beforelogger" pointcut-ref="AllInform"/> <!-- 后置通知 --> <aop:advisor advice-ref="afterlogger" pointcut-ref="AllInform"/> <!-- 环绕通知 --> <aop:advisor advice-ref="aroundlogger" pointcut-ref="AllInform"/> <!-- 异常通知 --> <aop:advisor advice-ref="throwlogger" pointcut-ref="AllInform"/> </aop:config> </beans>
package renjiale.service; public interface AOPInform01 { void showInform(); }
package renjiale.service.impl; import renjiale.service.AOPInform01; public class AOPInform01ServiceImpl implements AOPInform01{ public void showInform() { System.out.println("调用showInform()方法......"); } }
package renjiale.util; import java.lang.reflect.Method; import org.apache.log4j.Logger; import org.springframework.aop.AfterReturningAdvice; public class AfterReturingLogger implements AfterReturningAdvice { private static Logger logger = Logger.getLogger(AfterReturningAdvice.class.getName()); public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { logger.debug("后置通知,指定目标方法之后的操作......"); } }
package renjiale.util; import org.aopalliance.intercept.Invocation; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.apache.log4j.Logger; public class AroundIntercepter implements MethodInterceptor { private static Logger logger = Logger.getLogger(AroundIntercepter.class.getName()); public Object invoke(MethodInvocation invocation) throws Throwable { /* * 环绕 :前面和后面 */ logger.debug("环绕之前,指定目标方法执行..."); Object result = invocation.proceed(); logger.debug("环绕之后,指定目标方法执行..."); return result; } }
package renjiale.util; import java.lang.reflect.Method; import org.apache.log4j.Logger; import org.springframework.aop.MethodBeforeAdvice; public class BeforeLogger implements MethodBeforeAdvice { private static Logger logger = Logger.getLogger(BeforeLogger.class.getName()); public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { logger.debug("前置通知,指定目标方法之前的操作........"); } }
package renjiale.util; import java.lang.reflect.Method; import org.apache.log4j.Logger; import org.springframework.aop.ThrowsAdvice; public class ThrowLogger implements ThrowsAdvice { //private static Logger logger = Logger.getLogger(ThrowLogger.class); private static Logger logger = Logger.getLogger(ThrowLogger.class); public void afterThrowing(Method method,Object [] os,Object obj,Exception e){ logger.debug("异常抛出通知--e:"+e); } }
package renjiale.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import renjiale.service.AOPInform01; public class TestAOPInform1 { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("mySpringConfig2.xml"); AOPInform01 aop = ac.getBean("show2",AOPInform01.class); aop.showInform(); //需求: 在aop.showInform()方法调用时添加日志! //AOP通知(增强)的执行顺序 } }
AOP通知注解实现:
依赖项目环境, 需要注解,包!!!
package renjiale.service; public interface AOPInform01 { //实现接口中的方法 : 前置通知,后置通知,环绕通知,异常抛出通知 void showInform(); }
package renjiale.util; import org.apache.log4j.Logger; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class LoggerHandler { private static Logger logger = Logger.getLogger(LoggerHandler.class); //@Before("execution(public void showInform())") //表示该接口下所有的方法 前置实现 @Before("execution(* renjiale.service.AOPInform01.*(..))") public void before() { logger.debug("前置通知,指定目标方法的调用执行了....."); } @AfterReturning("execution(* renjiale.service.AOPInform01.*(..))") public void afterReturning() { logger.debug("后置通知,指定目标方法的调用执行了....."); } @Around("execution(* renjiale.service.AOPInform01.*(..))") public void aroundLogger(ProceedingJoinPoint jp) { try { logger.debug("环绕通知,指定目标方法的调用执行了....."); jp.proceed(); logger.debug("环绕通知,指定目标方法的调用执行了....."); } catch (Throwable e) { e.printStackTrace(); } } @AfterThrowing(value="execution(* renjiale.service.AOPInform01.*(..))",throwing="e") public void throwLogger() { logger.debug("异常抛出通知,指定目标方法的调用执行了....."); } }
<?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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 通过AOP方式给操作的方法加日志 --> <bean id="show" class="renjiale.service.impl.AOPInform01ServiceImpl"></bean> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 需要调用对象的方法 --> <bean id="beforelogger" class="renjiale.util.LoggerHandler"></bean> </beans>
schema,XML中配置
package renjiale.util;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
public class LoggerHandler {
private static Logger logger = Logger.getLogger(LoggerHandler.class);
public void before() {
logger.debug("前置通知,指定目标方法的调用执行了.....");
}
public void afterReturning() {
logger.debug("后置通知,指定目标方法的调用执行了.....");
}
public void aroundLogger(ProceedingJoinPoint jp) {
try {
logger.debug("环绕通知,指定目标方法的调用执行了.....");
jp.proceed();
logger.debug("环绕通知,指定目标方法的调用执行了.....");
} catch (Throwable e) {
e.printStackTrace();
}
}
public void throwLogger() {
logger.debug("异常抛出通知,指定目标方法的调用执行了.....");
}
}
<?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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 通过AOP方式给操作的方法加日志 --> <bean id="show" class="renjiale.service.impl.AOPInform01ServiceImpl"></bean> <bean id="loggerhandler" class="renjiale.util.LoggerHandler"></bean> <aop:config> <aop:pointcut expression="execution(* renjiale.service.AOPInform01.*(..))" id="manageAll"/> <!-- 注意 ref="loggerhandler"指向日志记录对象--> <aop:aspect ref="loggerhandler"> <aop:before method="before" pointcut-ref="manageAll"/> <aop:after-returning method="afterReturning" pointcut-ref="manageAll"/> <aop:around method="aroundLogger" pointcut-ref="manageAll"/> <aop:after-throwing method="throwLogger" pointcut-ref="manageAll"/> </aop:aspect> </aop:config> </beans>