tags:

views:

47

answers:

4

I have a bean which extends other Java file. When I create a Spring bean all public methods (from my and from extended files) are exposed. How can I hide not needed methods in bean XML config, so they are not exposed?

Added explanation:

I expose my beans via RDS for Flex application. So, my beans are available over network. With unneeded methods I have two problems:

  1. Security - anybody can call my setDataSource inherited from JdbcDaoSupport or anything else.. I think it is just crazy :-)
  2. Since I use RDS for Flex Builder, which automatically creates remote objects for my Flex app. All methods like setDataSource are available in my Flex app. Which is not good. Of course I can cut them off, but point one still in place.
A: 

I am a little confused why you care that spring sees them? My initial thought is "make them private or protected".

bwawok
A: 

You could try turning off autowiring. It's only a convenience after all. Doing it "by hand" in your bean configuration file will provide a much better record of what connects to what…

Donal Fellows
+3  A: 

If the inherited methods should not be accessed, perhaps you should use aggregation instead of inheritance?

As any code can invoke public inherited methods on that object, this is not specific to spring.

Edit: As you found out yourself, the remoting framework can be configured to not expose all methods. If that hadn't been possible, you could have used:

import java.lang.reflect.Proxy;

public static <I> I restrictToInterface(final I instance, Class<I> publicInterface) {
    Object proxy = Proxy.newProxyInstance(
        publicInterface.getClassLoader(), 
        new Class<?>[] {publicInterface}, 
        new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return method.invoke(instance, args);
            }
        }
    );
    return publicInterface.cast(proxy);
}

Test code:

interface MyRemoteInterface {
    void foo(Object bar);
}

class MyBeanImpl implements MyRemoteInterface {
    @Override
    public void foo(Object bar) { 
        System.out.println(bar);
    }

    public void dangerousMethodThatMustNotBeInvoked() {
        // launch missiles
    }
}

public static void main(String[] args) {
    MyBeanImpl beanImpl = new MyBeanImpl();
    MyRemoteInterface remotableBean = restrictToInterface(beanImpl, MyRemoteInterface.class);
    System.out.println("Remoteable Methods are:");
    for (Method m : remotableBean.getClass().getMethods()) {
        if (!Modifier.isStatic(m.getModifiers())) {
            System.out.println("\t" + m.getName());
        }
    }

    remotableBean.foo("Hello world!");
}
meriton
That is a great idea! I found simpler solution for my problem, but your explanation is definitely earned to be best answer. Thank you!
MinimeDJ
+2  A: 

Actually I had a wrong question. My problem has solution which is shown here: http://static.springsource.org/spring-flex/docs/1.0.x/reference/html/ch03s03.html

MinimeDJ