热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

大数据正式31

Spring练习jar包准备异常:代码结构如图所示PersonServletPersonServicePersonDao用异常通知捕获servlet的所有的方法抛出的

Spring练习

jar包准备

异常:

  • 代码结构如图所示
    • PersonServlet
    • PersonService
    • PersonDao
  • 用异常通知捕获servlet的所有的方法抛出的异常:

    • 目标对象所在的类 com.peng.web.PersonServlet
    • 抛出异常所在的方法 save()
    • 抛出异常的名称 XxxException
    • 异常信息 message
  • 意义:

    • 异常处理类和业务逻辑类完全松耦合。
    • 时刻捕获生产生产环境中所有的错误,实时监控该系统,异常收集。
  • 代码

    • com.peng.aspect

      • MyAspect

        package com.peng.aspect;import org.aspectj.lang.JoinPoint;
        import org.aspectj.lang.ProceedingJoinPoint;
        import org.aspectj.lang.annotation.After;
        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;
        import org.aspectj.lang.annotation.Pointcut;
        import org.springframework.stereotype.Component;/*** 切面* * @author Administrator* */
        @Component
        @Aspect
        public class MyAspect {//// /**// * 给web层加入切面// */// 创建一个切入点表达式的引用@Pointcut("within(com.peng.web.*)")public void pc() {}// 前置事务@Before(value = "pc()")public void beforeSomething(JoinPoint jp) {System.out.println("前置事务");}//// 环绕事务@Around(value = "pc()")public Object aroundSomething(ProceedingJoinPoint pjp) throws Throwable {System.out.println("环绕事务---------aa----------------");Object obj = pjp.proceed();System.out.println("环绕事务----------ww--------------");return obj;}//// 后置事务@AfterReturning(value = "pc()")public void afterSomething(JoinPoint jp) {System.out.println("后置事务");}// 异常事务@AfterThrowing(value = "pc()", throwing = "t")public void exceptionSomething(JoinPoint jp, Throwable t) {System.out.println("异常事务");System.err.println("异常类:" + t.getClass());System.err.println("异常信息:" + t.getMessage());System.out.println("异常事务");}// 最终事务@After(value = "pc()")public void finallySomething(JoinPoint jp) {System.out.println("最终事务");}}

    • com.peng.dao

      • PersonDao

        package com.peng.dao;import org.springframework.stereotype.Repository;import com.peng.pojo.Person;@Repository(value = "personDao")
        public class PersonDao {public void savePerson(Person person) {System.out.println("存储了对象" + person);}}

    • com.peng.pojo

      • Person

        package com.peng.pojo;import org.springframework.beans.factory.annotation.Value;
        import org.springframework.stereotype.Component;@Component(value = "person")
        public class Person {@Value(value = "张三")private String name;@Value(value = "12")private int age;@Overridepublic String toString() {return "姓名:" + name + ",年龄:" + age;}
        }

    • com.peng.service

      • PersonService

        package com.peng.service;import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Qualifier;
        import org.springframework.stereotype.Service;import com.peng.dao.PersonDao;
        import com.peng.pojo.Person;@Service(value = "personService")
        public class PersonService {@Autowired@Qualifier(value = "personDao")private PersonDao personDao;public void savePerson(Person person) {personDao.savePerson(person);}}

    • com.peng.test

      • Test

        package com.peng.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.peng.pojo.Person;
        import com.peng.web.PersonServlet;/*** 测试通用类* * @author Administrator* */
        public class Test {@org.junit.Testpublic void personModelTest() {// 获取Spring容器ClassPathXmlApplicationContext cpxac = new ClassPathXmlApplicationContext("applicationContext.xml");// 获取Person对象Person person = (Person) cpxac.getBean("person");// 获取PersonServlet对象PersonServlet personServlet = (PersonServlet) cpxac.getBean("personServlet");// 执行PersonServlet的savePerson方法personServlet.savePerson(person);}
        }

    • com.peng.web

      • PersonServlet

        package com.peng.web;import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Qualifier;
        import org.springframework.stereotype.Controller;import com.peng.pojo.Person;
        import com.peng.service.PersonService;@Controller(value = "personServlet")
        public class PersonServlet {@Autowired@Qualifier("personService")private PersonService personService;public void savePerson(Person person) {int bug = 1 / 0;System.out.println(bug);personService.savePerson(person);}}

    • 配置文件

      • applicationContext




方法执行的时间

  • 计算servlet的各个类的各个方法的执行时间
    1. 类的名称
    2. 方法的名称
    3. 执行的时间
  • 控制台输出

  • 意义:用来监控程序的性能问题

  • 代码

    • com.peng.aspect

      • MyAspect

        package com.peng.aspect;import org.aspectj.lang.JoinPoint;
        import org.aspectj.lang.ProceedingJoinPoint;
        import org.aspectj.lang.annotation.After;
        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;
        import org.aspectj.lang.annotation.Pointcut;
        import org.springframework.stereotype.Component;/*** 切面* * @author Administrator* */
        @Component
        @Aspect
        public class MyAspect {//// /**// * 给web层加入切面// */// 创建一个切入点表达式的引用@Pointcut("within(com.peng.web.*)")public void pc() {}// 前置事务@Before(value = "pc()")public void beforeSomething(JoinPoint jp) {System.out.println("前置事务");}//// 环绕事务@Around(value = "pc()")public Object aroundSomething(ProceedingJoinPoint pjp) throws Throwable {long startTime = System.currentTimeMillis();System.out.println("环绕事务---------aa--------当前时间(毫秒值):" + startTime);Object obj = pjp.proceed();long endTime = System.currentTimeMillis();System.out.println("环绕事务----------ww------当前时间(毫秒值):" + endTime);System.out.println("方法的执行时间:" + (endTime - startTime));return obj;}//// 后置事务@AfterReturning(value = "pc()")public void afterSomething(JoinPoint jp) {System.out.println("后置事务");}// 异常事务@AfterThrowing(value = "pc()", throwing = "t")public void exceptionSomething(JoinPoint jp, Throwable t) {System.out.println("异常事务");System.err.println("异常类:" + t.getClass());System.err.println("异常信息:" + t.getMessage());System.out.println("异常事务");}// 最终事务@After(value = "pc()")public void finallySomething(JoinPoint jp) {System.out.println("最终事务");}}

    • com.peng.dao

      • PersonDao

        package com.peng.dao;import org.springframework.stereotype.Repository;import com.peng.pojo.Person;@Repository(value = "personDao")
        public class PersonDao {public void savePerson(Person person) {System.out.println("存储了对象" + person);}}

    • com.peng.pojo

      • Person

        package com.peng.pojo;import org.springframework.beans.factory.annotation.Value;
        import org.springframework.stereotype.Component;@Component(value = "person")
        public class Person {@Value(value = "张三")private String name;@Value(value = "12")private int age;@Overridepublic String toString() {return "姓名:" + name + ",年龄:" + age;}
        }

    • com.peng.service

      • PersonService

        package com.peng.service;import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Qualifier;
        import org.springframework.stereotype.Service;import com.peng.dao.PersonDao;
        import com.peng.pojo.Person;@Service(value = "personService")
        public class PersonService {@Autowired@Qualifier(value = "personDao")private PersonDao personDao;public void savePerson(Person person) {personDao.savePerson(person);}}

    • com.peng.test

      • Test

        package com.peng.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.peng.pojo.Person;
        import com.peng.web.PersonServlet;/*** 测试通用类* * @author Administrator* */
        public class Test {@org.junit.Testpublic void personModelTest() {// 获取Spring容器ClassPathXmlApplicationContext cpxac = new ClassPathXmlApplicationContext("applicationContext.xml");// 获取Person对象Person person = (Person) cpxac.getBean("person");// 获取PersonServlet对象PersonServlet personServlet = (PersonServlet) cpxac.getBean("personServlet");// 执行PersonServlet的savePerson方法personServlet.savePerson(person);}
        }

    • com.peng.web

      • PersonServlet

        package com.peng.web;import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Qualifier;
        import org.springframework.stereotype.Controller;import com.peng.pojo.Person;
        import com.peng.service.PersonService;@Controller(value = "personServlet")
        public class PersonServlet {@Autowired@Qualifier("personService")private PersonService personService;public void savePerson(Person person) {personService.savePerson(person);}}

    • 配置文件

      • applicationContext




事务

  • 当方法上有事务的注解,该方法就有事务。写一个切面来完成
  • 代码

    • com.peng.annocation

      • Transaction

        package com.peng.annotation;import java.lang.annotation.ElementType;
        import java.lang.annotation.Retention;
        import java.lang.annotation.RetentionPolicy;
        import java.lang.annotation.Target;@Target(ElementType.METHOD)
        @Retention(RetentionPolicy.RUNTIME)
        public @interface Transaction {}

    • com.peng.aspect

      • MyAspect

        package com.peng.aspect;import org.aspectj.lang.JoinPoint;
        import org.aspectj.lang.ProceedingJoinPoint;
        import org.aspectj.lang.annotation.After;
        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;
        import org.aspectj.lang.annotation.Pointcut;
        import org.springframework.stereotype.Component;import com.peng.annotation.Transaction;/*** 切面* * @author Administrator* */
        @Component
        @Aspect
        public class MyAspect {//// /**// * 给web层加入切面// */// 创建一个切入点表达式的引用@Pointcut("within(com.peng.web.*)")public void pc() {}// 前置事务@Before(value = "pc()")public void beforeSomething(JoinPoint jp) {System.out.println("前置事务");}//// 环绕事务@Around(value = "pc() && @annotation(ann)")public Object aroundSomething(ProceedingJoinPoint pjp, Transaction ann)throws Throwable {System.out.println("环绕事务---------aa----------------");System.out.println("事务Start");Object obj = pjp.proceed();System.out.println("事务End");System.out.println("环绕事务----------ww--------------");return obj;}//// 后置事务@AfterReturning(value = "pc()")public void afterSomething(JoinPoint jp) {System.out.println("后置事务");}// 异常事务@AfterThrowing(value = "pc()", throwing = "t")public void exceptionSomething(JoinPoint jp, Throwable t) {System.out.println("异常事务");System.err.println("异常类:" + t.getClass());System.err.println("异常信息:" + t.getMessage());System.out.println("异常事务");}// 最终事务@After(value = "pc()")public void finallySomething(JoinPoint jp) {System.out.println("最终事务");}}

    • com.peng.dao

      • PersonDao

        package com.peng.dao;import org.springframework.stereotype.Repository;import com.peng.pojo.Person;@Repository(value = "personDao")
        public class PersonDao {public void savePerson(Person person) {System.out.println("存储了对象" + person);}}

    • com.peng.pojo

      • Person

        package com.peng.pojo;import org.springframework.beans.factory.annotation.Value;
        import org.springframework.stereotype.Component;@Component(value = "person")
        public class Person {@Value(value = "张三")private String name;@Value(value = "12")private int age;@Overridepublic String toString() {return "姓名:" + name + ",年龄:" + age;}
        }

    • com.peng.service

      • PersonService

        package com.peng.service;import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Qualifier;
        import org.springframework.stereotype.Service;import com.peng.dao.PersonDao;
        import com.peng.pojo.Person;@Service(value = "personService")
        public class PersonService {@Autowired@Qualifier(value = "personDao")private PersonDao personDao;public void savePerson(Person person) {personDao.savePerson(person);}}

    • com.peng.test

      • Test

        package com.peng.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.peng.pojo.Person;
        import com.peng.web.PersonServlet;/*** 测试通用类* * @author Administrator* */
        public class Test {@org.junit.Testpublic void personModelTest() {// 获取Spring容器ClassPathXmlApplicationContext cpxac = new ClassPathXmlApplicationContext("applicationContext.xml");// 获取Person对象Person person = (Person) cpxac.getBean("person");// 获取PersonServlet对象PersonServlet personServlet = (PersonServlet) cpxac.getBean("personServlet");// 执行PersonServlet的savePerson方法(实验组)personServlet.savePerson(person);System.out.println("=8=8==8=8=8=8=8==8=8=8=8=8=8=8=8=8=8=8=8==8=8=8=8=8==88=8=8=");// 执行PersonServlet的delPerson方法(参照组)personServlet.delPerson();}
        }

    • com.peng.web

      • PersonServlet

        package com.peng.web;import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Qualifier;
        import org.springframework.stereotype.Controller;import com.peng.annotation.Transaction;
        import com.peng.pojo.Person;
        import com.peng.service.PersonService;@Controller(value = "personServlet")
        public class PersonServlet {@Autowired@Qualifier("personService")private PersonService personService;@Transactionpublic void savePerson(Person person) {personService.savePerson(person);}public void delPerson() {System.out.println("del person ~~");}}

    • 配置文件

      • applicationContext




权限控制

  • 代码

    • com.peng.annotation

      • Right

        package com.peng.annotation;import java.lang.annotation.ElementType;
        import java.lang.annotation.Retention;
        import java.lang.annotation.RetentionPolicy;
        import java.lang.annotation.Target;/*** 权限控制* * @author Administrator* */
        @Target(ElementType.METHOD)
        @Retention(RetentionPolicy.RUNTIME)
        public @interface Right {String right() default "游客";
        }

    • com.peng.aspect

      • MyAspect

        package com.peng.aspect;import org.aspectj.lang.JoinPoint;
        import org.aspectj.lang.ProceedingJoinPoint;
        import org.aspectj.lang.annotation.After;
        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;
        import org.aspectj.lang.annotation.Pointcut;
        import org.springframework.stereotype.Component;import com.peng.annotation.Right;/*** 切面* * @author Administrator* */
        @Component
        @Aspect
        public class MyAspect {//// /**// * 给web层加入切面// */// 创建一个切入点表达式的引用@Pointcut("within(com.peng.web.*)")public void pc() {}// 前置事务@Before(value = "pc()")public void beforeSomething(JoinPoint jp) {System.out.println("前置事务");}//// 环绕事务@Around(value = "pc() && @annotation(right)")public Object aroundSomething(ProceedingJoinPoint pjp, Right right)throws Throwable {System.out.println("环绕事务---------aa----------------");System.out.println("当前用户权限:" + right.right());if (!"管理员".equals(right.right())) {System.out.println("e,你不是管理员,不能进行该操作");return null;}Object obj = pjp.proceed();System.out.println("环绕事务----------ww--------------");return obj;}//// 后置事务@AfterReturning(value = "pc()")public void afterSomething(JoinPoint jp) {System.out.println("后置事务");}// 异常事务@AfterThrowing(value = "pc()", throwing = "t")public void exceptionSomething(JoinPoint jp, Throwable t) {System.out.println("异常事务");System.err.println("异常类:" + t.getClass());System.err.println("异常信息:" + t.getMessage());System.out.println("异常事务");}// 最终事务@After(value = "pc()")public void finallySomething(JoinPoint jp) {System.out.println("最终事务");}}

    • com.peng.dao

      • PersonDao

        package com.peng.dao;import org.springframework.stereotype.Repository;import com.peng.pojo.Person;@Repository(value = "personDao")
        public class PersonDao {public void savePerson(Person person) {System.out.println("存储了对象" + person);}}

    • com.peng.pojo

      • Person

        package com.peng.pojo;import org.springframework.beans.factory.annotation.Value;
        import org.springframework.stereotype.Component;@Component(value = "person")
        public class Person {@Value(value = "张三")private String name;@Value(value = "12")private int age;@Overridepublic String toString() {return "姓名:" + name + ",年龄:" + age;}
        }

    • com.peng.service

      • PersonService

        package com.peng.service;import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Qualifier;
        import org.springframework.stereotype.Service;import com.peng.dao.PersonDao;
        import com.peng.pojo.Person;@Service(value = "personService")
        public class PersonService {@Autowired@Qualifier(value = "personDao")private PersonDao personDao;public void savePerson(Person person) {personDao.savePerson(person);}}

    • com.peng.test

      • Test

        package com.peng.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.peng.pojo.Person;
        import com.peng.web.PersonServlet;/*** 测试通用类* * @author Administrator* */
        public class Test {@org.junit.Testpublic void personModelTest() {// 获取Spring容器ClassPathXmlApplicationContext cpxac = new ClassPathXmlApplicationContext("applicationContext.xml");// 获取Person对象Person person = (Person) cpxac.getBean("person");// 获取PersonServlet对象PersonServlet personServlet = (PersonServlet) cpxac.getBean("personServlet");// 执行PersonServlet的savePerson方法personServlet.savePerson(person);}
        }

    • com.peng.web

      • PersonServlet

        package com.peng.web;import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Qualifier;
        import org.springframework.stereotype.Controller;import com.peng.pojo.Person;
        import com.peng.service.PersonService;@Controller(value = "personServlet")
        public class PersonServlet {@Autowired@Qualifier("personService")private PersonService personService;// @Right(right = "管理员")@Rightpublic void savePerson(Person person) {personService.savePerson(person);}}

    • 配置文件

      • applicationContext




缓存

  • 过程
    1. 需要缓存的的数据进行在内存中存放一份
    2. 再次用到时,先去内存中找,找到以后直接用,找不到就从数据库中找,并且往内存中存一份
    3. 如此反复进行12操作
  • 代码

    • com.peng.annotation

      • CacheData

        package com.peng.annotation;import java.lang.annotation.ElementType;
        import java.lang.annotation.Retention;
        import java.lang.annotation.RetentionPolicy;
        import java.lang.annotation.Target;/*** 数据缓存自定义注解* * @author Administrator* */
        @Target(ElementType.METHOD)
        @Retention(RetentionPolicy.RUNTIME)
        public @interface CacheData {String name();
        }

    • com.peng.aspect

      • DataCacheAspect

        package com.peng.aspect;import java.util.HashMap;
        import java.util.Map;import org.aspectj.lang.ProceedingJoinPoint;
        import org.aspectj.lang.annotation.Around;
        import org.aspectj.lang.annotation.Aspect;
        import org.aspectj.lang.annotation.Pointcut;
        import org.springframework.stereotype.Component;import com.peng.annotation.CacheData;
        import com.sun.istack.internal.logging.Logger;/*** 数据缓存切面* * @author Administrator* */
        @Component
        @Aspect
        public class DataCacheAspect {private Logger logger = Logger.getLogger(DataCacheAspect.class);private Map cache = new HashMap();// 创建一个切入点表达式的引用@Pointcut("execution(* *(..))")public void pc() {}// 环绕事务@Around(value = "pc() && @annotation(data)")public Object aroundSomething(ProceedingJoinPoint pjp, CacheData data)throws Throwable {System.out.println("================start=====================");logger.info("Execution of Cacheable method catched");// 产生缓存数据的key值,像是这个样子caching.aspectj.Calculator.sum(Integer=1;Integer=2;)StringBuilder keyBuff = new StringBuilder();// 增加类的名字keyBuff.append(pjp.getTarget().getClass().getName());// 加上方法的名字keyBuff.append(".").append(pjp.getSignature().getName());keyBuff.append("(");// 循环出方法的参数for (final Object arg : pjp.getArgs()) {// 增加参数的类型和值keyBuff.append(arg.getClass().getSimpleName() + "=" + arg + ";");// TODO:这里的数据可以缓存哦~.~}keyBuff.append(")");String key = keyBuff.toString();System.out.println(("Key = " + key));Object result = cache.get(key);if (result == null) {logger.info("Result not yet cached. Must be calculated...");result = pjp.proceed();logger.info("Storing calculated value '" + result + "' to cache");cache.put(key, result);} else {logger.info("Result '" + result + "' was found in cache");}System.out.println("==================end===================");return result;}
        }

      • MyAspect

        package com.peng.aspect;import org.aspectj.lang.JoinPoint;
        import org.aspectj.lang.ProceedingJoinPoint;
        import org.aspectj.lang.annotation.After;
        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;
        import org.aspectj.lang.annotation.Pointcut;
        import org.springframework.stereotype.Component;import com.peng.annotation.Right;/*** 切面* * @author Administrator* */
        @Component
        @Aspect
        public class MyAspect {//// /**// * 给web层加入切面// */// 创建一个切入点表达式的引用@Pointcut("within(com.peng.web.*)")public void pc() {}// 前置事务@Before(value = "pc()")public void beforeSomething(JoinPoint jp) {System.out.println("前置事务");}//// 环绕事务@Around(value = "pc() && @annotation(right)")public Object aroundSomething(ProceedingJoinPoint pjp, Right right)throws Throwable {System.out.println("环绕事务---------aa----------------");Object obj = pjp.proceed();System.out.println("环绕事务----------ww--------------");return obj;}//// 后置事务@AfterReturning(value = "pc()")public void afterSomething(JoinPoint jp) {System.out.println("后置事务");}// 异常事务@AfterThrowing(value = "pc()", throwing = "t")public void exceptionSomething(JoinPoint jp, Throwable t) {System.out.println("异常事务");System.err.println("异常类:" + t.getClass());System.err.println("异常信息:" + t.getMessage());System.out.println("异常事务");}// 最终事务@After(value = "pc()")public void finallySomething(JoinPoint jp) {System.out.println("最终事务");}}

    • com.peng.dao

      • PersonDao

        package com.peng.dao;import org.springframework.stereotype.Repository;import com.peng.pojo.Person;@Repository(value = "personDao")
        public class PersonDao {public void savePerson(Person person) {System.out.println("存储了对象" + person);}}

    • com.peng.pojo

      • Person

        package com.peng.pojo;import org.springframework.beans.factory.annotation.Value;
        import org.springframework.stereotype.Component;@Component(value = "person")
        public class Person {@Value(value = "张三")private String name;@Value(value = "12")private int age;@Overridepublic String toString() {return "姓名:" + name + ",年龄:" + age;}
        }

    • com.peng.service

      • PersonService

        package com.peng.service;import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Qualifier;
        import org.springframework.stereotype.Service;import com.peng.dao.PersonDao;
        import com.peng.pojo.Person;@Service(value = "personService")
        public class PersonService {@Autowired@Qualifier(value = "personDao")private PersonDao personDao;public void savePerson(Person person) {personDao.savePerson(person);}}

    • com.peng.test

      • Test

        package com.peng.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.peng.pojo.Person;
        import com.peng.web.PersonServlet;/*** 测试通用类* * @author Administrator* */
        public class Test {@org.junit.Testpublic void personModelTest() {// 获取Spring容器ClassPathXmlApplicationContext cpxac = new ClassPathXmlApplicationContext("applicationContext.xml");// 获取Person对象Person person = (Person) cpxac.getBean("person");// 获取PersonServlet对象PersonServlet personServlet = (PersonServlet) cpxac.getBean("personServlet");// 执行PersonServlet的savePerson方法personServlet.savePerson(person);}
        }

    • com.peng.web

      • PersonServlet

        package com.peng.web;import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Qualifier;
        import org.springframework.stereotype.Controller;import com.peng.pojo.Person;
        import com.peng.service.PersonService;@Controller(value = "personServlet")
        public class PersonServlet {@Autowired@Qualifier("personService")private PersonService personService;// @Right(right = "管理员")@Rightpublic void savePerson(Person person) {personService.savePerson(person);}}

    • 配置文件

      • applicationContext





推荐阅读
author-avatar
谢云虎
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有