简体   繁体   English

使用Maven插件提取使用Spring注入的参数

[英]Using a Maven plugin to extract parameters injected using Spring

I have an application which uses Spring to wire up some beans in various combinations. 我有一个应用程序,该应用程序使用Spring以各种组合方式连接一些bean。 Below are simplified classes which help to illustrate the the relevant part of the application. 下面是简化的类,它们有助于说明应用程序的相关部分。

class A {
   private List<B> fieldB;

    public void setFieldB(List<B> fieldB) { this.fieldB = fieldB; }
    public List<B> getFieldB() { return this.fieldB; }
}

class B {
    private String name;
    private String field1;
    private String field2;

    public void setName(String name) { this.name = name; }
    public void setField1(String value1) { this.field1 = value1; }
    public void setField2(String value2) { this.field2 = value2; }

    public String getName() { return this.name; }
    public String getField1() { return this.field1; }
    public String getField2() { return this.field2; }
}

spring-context-file1.xml 弹簧上下文文件1.xml

<?xml ...>
<beans ...>
    <bean id="a" class="com.example.A">
        <property name="fieldB">
           <list> <ref bean="b1"/> <ref bean="b2"/> </list>
        </property>
    </bean>

    <bean id="b1" class="com.example.B">
        <property name="name">
           <value>b1</value>
        </property>
        <property name="field1">
           <value>fieldOneValueOne</value>
        </property>
        <property name="field2">
           <value>fieldOneValueTwo</value>
        </property>
    <bean>

    <bean id="b2" class="com.example.B">
        <property name="name">
           <value>b2</value>
        </property>
        <property name="field1">
           <value>fieldTwoValueOne</value>
        </property>
        <property name="field2">
           <value>fieldTwoValueTwo</value>
        </property>
    <bean>
</beans>

Given the above what I would like to do is have the following information extracted at build time: 鉴于以上所述,我想在构建时提取以下信息:

spring-context-file1.cfg  
b1 => {fieldOneValueOne, fieldOneValueTwo}  
b2 => {fieldTwoValueOne, fieldTwoValueTwo} 

I have opted to develop a Maven plugin to do this requisite processing. 我选择开发一个Maven插件来执行此必要的处理。 Basically the plugin will load up the Spring context file and use the getBean("...") method to get the beans of interest. 基本上,该插件将加载Spring上下文文件并使用getBean(“ ...”)方法来获取感兴趣的bean。 I have however run into a problem. 但是,我遇到了一个问题。 In order to extract the information from the beans the plugin code needs to know what type of object it's manipulating. 为了从bean中提取信息,插件代码需要知道它正在处理哪种类型的对象。 This means the plugin code will need to be compiled against the main project code. 这意味着插件代码将需要针对主要项目代码进行编译。 This seems wrong to me. 对我来说这似乎是错误的。 Does any know of any means for a Maven plugin to extract this sort of information? 是否知道Maven插件提取此类信息的任何方法?

I think instead of extracting the information from the Spring config file, you should create a common file that the Spring context can load bean properties from. 我认为,与其从Spring配置文件中提取信息,不如创建一个通用文件,Spring上下文可以从中加载Bean属性。 Then you can parse this file quite easily at build time (hopefully without writing a custom Maven plugin - this will complicate your build horribly). 然后,您可以在构建时非常轻松地解析此文件(希望无需编写自定义Maven插件-这会使您的构建复杂得多)。 Have a look at the new property management in Spring 3.1: http://blog.springsource.com/2011/02/15/spring-3-1-m1-unified-property-management/ 看看Spring 3.1中的新属性管理: http : //blog.springsource.com/2011/02/15/spring-3-1-m1-unified-property-management/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM