Results 1 to 5 of 5

Thread: Private beans?

  1. #1
    Join Date
    Sep 2004
    Posts
    3

    Default Private beans?

    I am currently wiring several classes together that I would normally make package private and hide behind a facade. It appears that Spring requires these classes and their methods to be public.

    Is there a way to make classes/methods private and still use them with Spring? The idea is to minimize the classes that I am exposing to clients in a package, as the majority of the classes are specific to the implementation.

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default Default accessible class

    It appears you have to have public classes for IoC, although I'm not sure if this is by design.

    I'm just looking at adding a JIRA bug for this. Patching BeanUtils to change line 68:

    if (!Modifier.isPublic(constructor.getModifiers())) {
    to
    if (!Modifier.isPublic(constructor.getDeclaringClass( ).getModifiers()) || !Modifier.isPublic(constructor.getModifiers())) {

    will at least allow creation of the bean, but there are a whole lot of regression test cases to check. Will get back with JIRA link if I add it.

  3. #3
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    The reason for the classes to be public is, that they are not instantiated from within their package but from Spring's factories. So they have to be public.

    I also had the issue with "normally package visible" types before. However I didn't find a solution either (except making the classes public of course).

  4. #4
    Join Date
    Aug 2004
    Location
    Linz, Austria
    Posts
    391

    Default

    It's strongly recommended to use public classes with public accessors for IoC: An important point about IoC is that you should be able to use the same classes outside an IoC container too, via direct instantiation - which requires the class and relevant methods to be public.

    That said, we do support protected/private constructors etc to some degree already, so I wouldn't mind adding support for package-level classes etc. Even if this is not recommended, I guess that Spring shouldn't constrain the choices for application developers here.

    Juergen

  5. #5
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    Here's the issue on JIRA
    http://opensource.atlassian.com/proj...browse/SPR-339

    Re: The reason for the classes to be public is, that they are not instantiated from within their package but from Spring's factories

    Actually, with reflection you can bypass all OO access restrictions (you can even directly modify private fields outside the class), but Spring is designed not to violate OO. Access to package level classes and members is quite reasonable for a container however. In a way it's more intrusive to force all public classes/methods. I think this is a good move.

Similar Threads

  1. Domain Object Model and Hibernate detached object
    By yfmoan in forum Architecture
    Replies: 11
    Last Post: Oct 14th, 2005, 11:05 PM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. Public / Private beans
    By mattinger in forum Container
    Replies: 7
    Last Post: Jul 27th, 2005, 03:22 AM
  4. Building complex forms
    By Scott Tavares in forum Swing
    Replies: 10
    Last Post: Jun 7th, 2005, 09:32 AM
  5. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •