开发者

How to hide method from the Spring bean?

开发者 https://www.devze.com 2023-01-05 07:21 出处:网络
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

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.


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!");
}


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


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


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…

0

精彩评论

暂无评论...
验证码 换一张
取 消