返回顶部

收藏

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. Ray 发表 2014-12-11 04:45:00 hbase Java API操作实例
  3. shangxuetang 发表 2015-01-05 01:41:28 解决java常规乱码问题
  4. infoplat 发表 2014-05-21 06:03:58 JAVA调用DB2存储过程方法
  5. Calarence 发表 2014-12-30 16:00:27 Java中有关Null的9件事
  6. 博主 发表 2015-01-12 16:00:00 C/C++,Java,Python内存管理
  7. 进林 发表 2015-01-19 16:00:28 Java高效读取大文件
  8. importnewzz 发表 2015-01-21 15:20:26 Java线程的5个使用技巧
  9. 博主 发表 2014-04-16 07:18:00 java安全那些事之四(无状态安全认证)
  10. 博主 发表 2015-02-05 13:48:14 Java 8:当重载遇上lambda
  11. 阿里技术保障 发表 2014-09-23 12:32:34 在Java中如何伪造数据
  12. 博主 发表 2015-02-12 00:00:00 Java中异常处理

发表评论