javassis在spring初始化前修改class文件。

来源:互联网 发布:苹果绿色上网软件注册 编辑:程序博客网 时间:2024/06/06 00:20

需求:定时任务。
spring task可以实现,用注解很简单。(因为不是本文重点,所以放在文章后面)。
我现在要做这么一件事,通过一个配置,可以给某一些类的方法加上一个注解。在spring初始化前完成。于是用到了javassis。大概就是这样。

第一步:

web.xml文件修改。
假如如下内容

 <listener> 因为这个类需要比spring更早实例化,放在webxml最前面        <listener-class>faicm.MyTime</listener-class></listener>

实现代码如下:

public class MyTime implements ServletContextListener {    public MyTime() {//构造方法,此代码最早执行。        try {            doJavassis();        } catch (Exception e) {            e.printStackTrace();        }    }    private void doJavassis() throws NotFoundException, ClassNotFoundException {    //获取常量池        ClassPool pool = ClassPool.getDefault();     //因为classloader的原因,这里假如需要改变类的classpath,不然会找不到、     ###20161月更新下下下下下下     这里添加了一个ClassClassPath(javassist 3.18.1-GA)在该类中通过这么一句话获取类.class文件       public InputStream openClassfile(String classname) {//这里的classname就是查询的类全名。        String jarname = "/" + classname.replace('.', '/') + ".class";        return thisClass.getResourceAsStream(jarname);//这里的thisClass就是下面那句this.getClass(),这里和ClassLoader无关。这里的类被修改后,加载到ClassLoader后,下次就不需要加载了。     }     ###20161月更新上上上上上上        pool.insertClassPath(new ClassClassPath((this.getClass())));        // Class cc = Class.forName();        System.out.println(pool.getClassLoader());        // 获取需要修改的类,获取到该类、        CtClass ct = pool.get("faicm.QuartzJob");        // 获取类里的所有方法        CtMethod[] cms = ct.getDeclaredMethods();        CtMethod cm = null;// cms[0];        for (CtMethod m : cms) {//通过遍历,找到我需要的work方法            if ("work".endsWith(m.getName())) {                cm = m;                break;            }        }        System.out.println("方法名称====" + cm.getName());        //获取方法的信息        MethodInfo minInfo = cm.getMethodInfo();       //获取信息的常量池        ConstPool cp = minInfo.getConstPool();        // 获取注解信息        AnnotationsAttribute attribute2 = new AnnotationsAttribute(cp,                AnnotationsAttribute.visibleTag);                //新建一个注解        Annotation annotation = new Annotation(                "org.springframework.scheduling.annotation.Scheduled", cp);        // 修改上面这个注解的名称为cron的内容为  StringMemberValue里面的字符串        annotation.addMemberValue("cron", new StringMemberValue(                "15,30,45 * * * * ?", cp));        attribute2.setAnnotation(annotation);        minInfo.addAttribute(attribute2);        try {//这一步很重要,把新的类加载进去。            ct.toClass();        } catch (CannotCompileException e1) {            // TODO Auto-generated catch block            e1.printStackTrace();        }        //反射获取类,这个已经不重要了,主要是为了获取类,获取注解,验证是否成功。        Class ccc = Class.forName("faicm.QuartzJob");        try {        //下面就是基本的反射。            Method mmm = ccc.getMethod("work", null);            java.lang.annotation.Annotation[] aa = mmm.getAnnotations();            for (java.lang.annotation.Annotation a : aa) {                System.out.println(a);            }        } catch (NoSuchMethodException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (SecurityException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    //这句话貌似已无用。    //this.getClass().getClassLoader().loadClass("faicm.QuartzJob");    }

再看看被改变的类。

package faicm;import org.springframework.stereotype.Component;@Componentpublic class QuartzJob {     public void work() {        System.out.println("Quartz在工作");    }}

这一这里没有任何注解,第一段代码运行的时候会加上注解。



与上文无关的内容。

Quartz与spring结合用注解很简单,这样就可以

<task:annotation-driven />  
package faicm;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Component//加一个这实例化beanpublic class QuartzTest {    @Scheduled(cron = "15,30,45 * * * * ?")//加一个这 cron里面是表达式,关于时间的    public void work() {        System.out.println("定时任务走起了。。。。");    }}

但是我想的事不要在代码中加,因为代码多的时候 ,定时任务不好统一管理,于是有了文章第一段的内容。


###########20150807########↓↓↓↓↓↓↓↓↓↓
注意:
这里遇到了一个问题,代码本地ok,线上linux环境就会跑两边。
解决办法:
①:把这个 《task:annotation-driven /》(用<替换《) 从applicationContext.xml中取出来,放到quartz.xml中。
②:把quartz.xml和applicationContext.xml同一个级别放在web.xml里面。
###########20150807#######↑↑↑↑↑↑↑↑↑↑


疑惑。

如果是spring配置的话会是这样的。(代码来自网络)

<?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:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"    xmlns:tx="http://www.springframework.org/schema/tx">    <!-- 要调用的工作类 -->    <bean id="quartzJob" class="faicm.QuartzJob"></bean>    <!-- 定义调用对象和调用对象的方法 -->    <bean id="jobtask"        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">        <!-- 调用的类 -->        <property name="targetObject">            <ref bean="quartzJob" />        </property>        <!-- 调用类中的方法 -->        <property name="targetMethod">            <value>work</value>        </property>    </bean>    <!-- 定义触发时间 -->    <bean id="doTime"        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">        <property name="jobDetail">            <ref bean="jobtask" />        </property>        <!-- cron表达式 -->        <property name="cronExpression">            <value>10,15,20,25,30,35,40,45,50,55 * * * * ?</value>        </property>    </bean>    <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->    <bean id="startQuertz" lazy-init="false" autowire="no"        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">        <property name="triggers">            <list>                <ref bean="doTime" />            </list>        </property>    </bean></beans>

我想把这个代码翻译成java的,不用xml的配置,已实现动态的效果。于是

public class Y extends SchedulerFactoryBean {    public void aa() throws Exception {        QuartzJob job = new QuartzJob();        // //        MethodInvokingJobDetailFactoryBean jobtask = new MethodInvokingJobDetailFactoryBean();        jobtask.setTargetObject(job);        jobtask.setGroup("t1");        jobtask.setName("jobtask");        jobtask.setTargetMethod("work");        jobtask.afterPropertiesSet();        //        CronTriggerFactoryBean doTime = new CronTriggerFactoryBean();        doTime.setJobDetail(jobtask.getObject());        doTime.setBeanName("doTime");        doTime.setGroup("t1");        doTime.setName("doTime");        doTime.setCronExpression("15,30,45 * * * * ?");        doTime.afterPropertiesSet();        //        // SchedulerFactoryBean ss = new SchedulerFactoryBean();        // ss.setTriggers();        super.setTriggers(new Trigger[]{doTime.getObject()});    }}

但是这样也失败了。

0 0
原创粉丝点击