返回顶部

收藏

javassist生成类, spring配置生成类

更多

运行时使用javassist定义类,然后再spring中动态添加bean定义。需要实现spring的BeanDefinitionRegistryPostProcessor接口,在此接口中定义类,并注册spring的bean定义。

涉及pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>hello-javassist</groupId>
    <artifactId>hello-javassist</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.15.0-GA</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>
    </dependencies>
</project>

DynamicServiceRegistryPostProcessor实现spring的BeanDefinitionRegistryPostProcessor接口

package cn.outofmemory.hello.javassist.config;

import cn.outofmemory.hello.javassist.IService;
import javassist.*;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;

import java.io.IOException;

/**
 * Created by outofmemory.cn on 2015/12/14.
 */
public class DynamicServiceRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        ClassPool pool = new ClassPool(true);//ClassPool.getDefault();

        // create the class
        String className = "cn.outofmemory.hello.service.Hello1";
        CtClass cc = pool.makeClass(className);

        try {
            cc.addInterface(pool.getCtClass("cn.outofmemory.hello.javassist.IService"));
            cc.addMethod(CtNewMethod.make("public void doSomething(){System.out.println(\"do Something\");}", cc));
            //Class<?> clazz = cc.getClass();
            cc.writeFile("./target/classes");
            BeanDefinitionBuilder definitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(className);
            beanDefinitionRegistry.registerBeanDefinition("hello1", definitionBuilder.getBeanDefinition());
        } catch (CannotCompileException e) {
            throw new RuntimeException(e);
        } catch (NotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }
}

上面定义类实现的接口:

package cn.outofmemory.hello.javassist;

/**
 * Created by outofmemory.cn on 2015/12/14.
 */
public interface IService {
    void doSomething();
}

这里定义接口是为了后面更方便的使用动态生成类。

下面是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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean class="cn.outofmemory.hello.javassist.config.DynamicServiceRegistryPostProcessor"/>

</beans>

配置文件中只有一个bean,是我们上面定义动态生成类,并注册到spring容器中的类。

最后是main类:

package cn.outofmemory.hello.javassist;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by outofmemory.cn on 2015/12/14.
 */
public class SpringWithJavassistApp {

    public static  void main(String[] args) throws ClassNotFoundException {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring.xml");

        Class clazz = Class.forName("cn.outofmemory.hello.service.Hello1");
        IService service1 = (IService) applicationContext.getBean("hello1");
        service1.doSomething();
    }
}

标签:java,javassist

收藏

0人收藏

支持

1

反对

0

相关聚客文章
  1. admin 发表 2013-11-13 04:38:59 javassist这货到底干嘛的?
  2. 博主 发表 2015-06-06 00:00:00 Java Attach API
  3. importnewzz 发表 2015-06-14 07:08:08 关于 Java 对象序列化您不知道的 5 件事
  4. 博主 发表 2015-06-13 14:59:00 说说依赖注入
  5. 博主 发表 2015-04-06 07:02:00 java中的 a = b = c
  6. 博主 发表 2012-09-14 01:57:00 java Play框架入门 ( simple tutorial for play framework
  7. 方 腾飞 发表 2015-06-22 09:44:13 Adopt OpenJDK 翻译计划
  8. erenzone@gmail.com 发表 2015-06-24 10:23:22 JAXB & Collection Properties
  9. 四火 发表 2015-06-26 00:16:09 从Java和JavaScript来学习Haskell和Groovy(元编程)
  10. 毛毛 发表 2015-07-04 13:48:18 JAVA性能优化调查结果(第二部分)
  11. erenzone@gmail.com 发表 2015-07-06 04:31:03 100 High-Quality Java Developers’ Blogs
  12. 泥瓦匠BYSocket 发表 2015-07-16 11:59:13 图解 Java IO : 一、File源码

发表评论