Announcement Announcement Module
No announcement yet.
Session Scoped String Bean for Injection Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Session Scoped String Bean for Injection

    I'm building an application in which I'd like to declare string beans, and scope them to session. This way, I can have a single string object I pass to all the controllers for each session. I want to do this for shared pieces such as a componentID. Each page needs the component ID, and I cannot pass the attribute in a get method. (The data cannot be displayed in the url bar.) I have some pages that are views, and others that are forms, so using post everywhere simply will not work.

    Here is the relevant code:
    Bean Def XML Note: actual class names changed to protect the strange and bewildered
    <!-- Dependencies to inject into Controllers -->
        <bean name="componentId" class="java.lang.String" scope="session">
        <bean name="sillyObjectId" class="java.lang.String" scope="session">
        <bean name="sillyObject" scope="session"
    <!-- Controller Beans -->
        <bean name="abstractSillyObjectPageController" abstract="true"
              p:lkTimeZones-ref="lkTimeZones" <!-- defined in another bean file -->
              p:sillyObjects-ref="SillyObjectCollection" <!-- defined in another bean file-->
              p:selectedSillyObject-ref="SillyObject" />
        <bean name="sillyObjectFormController" parent="abstractSillyObjectPageController"
              p:validator-ref="sillyObjectValidator" />
        <bean name="relatedSillyObjectViewController" parent="abstractSillyObjectPageController"
              p:appSchedules-ref="relatedSillyObjectCollection" <!--defined in another file -->
    So, as you can see, the intent is for all the controllers to share the data. When the app hits the entry page, they are set, and then we don't have to worry about it.

    The problem is, when I configured (and finally got working thanks to the forums) the aop:scoped-proxy on the string declarations, I recieved the following error:

    nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class java.lang.String

    I realize that I can create container classes for the strings, (SillyObject has the attribute of sillyObjectId) but I'd really rather not do that. (There are more of these than what I have shown here.)

    Any thoughts?

  • #2

    The reason it is giving you this error of illegalArgumentException is since you want to make this bean as a session scoped, and your use of aop:scoped-proxy. A proxy cannot be created for a Final class.

    Will method injection be useful in the scenario that you are describing?



    • #3
      nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class java.lang.String
      It's pretty obvious from the error what the problem is. Perhaps I was not clear in that I understood the error, I just wanted to know if there was a way around it.

      Method injection was/is not an option for what I need to do. I need the value to be unique to the session, not global to the application context. I did find a solution that works, but is more kludgey than I wanted. I was hoping for something elegant.

      Here's what I did:
      I created a wrapper object for my string, and made the wrapper object session scoped.

      Here's why I don't like it:
      Now, if I want the string I have to call wrapperObject.getStringValue();
      (or in jsp wrapper.stringValue). It's just not how I wanted to do it.


      • #4
        You can't scope String objects for two reasons :

        1- String class is final (and so can't be proxied)
        2- String objects are immutable

        You can't create an empty String object and set a value later :

        String a = new String();
        a = "Hello";
        is equivalent to :

        String a = new String();
        a = new String("Hello");
        So 'a' references a different object than the initial one.


        • #5
          Would this be the source of the problem I am having then? Please refer to my post: