Announcement Announcement Module
Collapse
No announcement yet.
(Advanced) Bean Configuration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • (Advanced) Bean Configuration

    I would like to use the BeanFactory for a class implementing this interface (I know it wouldn't be a Bean):

    interface MyInterface {

    public void setName(String name) ;

    public void add(DataSource datasource);

    public String put(String key, String value);

    }

    The first one is a property and is no problem.
    But what about the method 2 and 3.
    Is it even possible to do it with a BeanFactory?

    Kindest regards,
    Victor

  • #2
    What do you want to do exactly ?
    just create a bean of this interface ? or set all the values in the configuration of the beanFactory / applicationContext ? In this case, indeed, it is not directly doable and I'd suggest to switch to a real bean with a setDataSourceList(List) and setMap(Map) or eventually only as backup of your real add(DataSource) and put(String,String) to be used at init time

    HTH

    Olivier

    Comment


    • #3
      E.g. if I have the class
      org.apache.commons.dbcp.BasicDataSource
      and I would like to use the method
      addConnectionProperty(String name, String value) .
      How would I do that?
      Is it really necessary to extend this class to implement convenience methods.
      Somehow this would defeat the purpose to ease my development.

      Kindest regards,
      Victor

      Comment


      • #4
        Do you really need this method for the BasicDataSource ?
        You can set up a complete one like thid :
        Code:
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
          <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
          <property name="url"><value>jdbc&#58;//my_database</value></property>
          <property name="username"><value>joe</value></property>
          <property name="password"><value>smith</value></property>
        </bean>
        And you have a handful of other setting accessible by normal setters which may cover your needs. Can you describe your needs some more ?

        Olivier

        Comment


        • #5
          Thanks for the example but what I am looking for is a generic approach to add/put additional properties and this was only an example I found quickly.
          This is a situation that will occur regularly because there are a lot libraries which are not build explicitly out of beans (not necessarily a bad thing).
          Actually this option must exist, at least internally because one can create lists or maps.
          I would like somthing like:
          <bean id="first" class="MyClass">
          <property type="add" name="prop"><value>named add</value></property>
          <property type="add"><value>generic add</value></property>
          <property type="put" name="prop"><value>named</value><value>put</value></property>
          <property type="put"><value>generic</value><value>put</value></property>
          </bean>

          If this or something similar is not possible I would do it on my own but I would hate it to reinvent the wheel.

          Kindest regards,
          Victor

          Comment


          • #6
            Originally posted by kromo
            what I am looking for is a generic approach to add/put additional properties
            Maybe MethodInvokingFactoryBean might help you?

            Regards,
            Andreas

            Comment


            • #7
              That's what I was looking for.
              It's pretty verbose but will do the job.

              Thanks a lot.

              Kindest regards,
              Victor

              Comment


              • #8
                the MethodInvokingFactoryBean creates a bean off a method invocation but isn't made to initialize a bean with arbitrary method call. It could be used but in a contrived way. Eventually, I would prefer an improvement of the configuration to allow arbitrary methods to be called for init rather than perverting the MethodInvokingFactoryBean.

                Olivier

                Comment

                Working...
                X