Get config setting in custom taglib using Spring "context-property-placeholder" - spring

I'm creating a custom taglib, and would like to use some config options that are loaded via the underlying Spring framework using:
<context:property-placeholder location="classpath:config.properties" />
How would I get access to these variables in my taglib?
Thanks,
James.

The JSP taglibs have nothing in common with the Spring context's lifecycle, they're managed by the servlet container. This can complicate things a bit, for example: inject-dependency-into-a-taglib-class, how-to-write-tag-in-my-spring-project.
Since you're only mentioning the need for contents of the properties file, you could use plain old java.util.ResourceBundle (or, if you need more flexibility, Apache Commons' org.apache.commons.configuration.PropertiesConfiguration).
(One could also argue that requiring access to configuration in your tags indicates a design problem...)

Related

What is the best way to read application-context.xml in Spring MVC application, Is it by adding in init-param tag or by adding in context-param tag?

In few applications I see application-context file being read via init-param and in few via context-param. My intention is to know which one to use when.
after all it dependence on you project requirement
1) if you want to set the access scope of data then use the .if you declare anything inside is only accessible only for that particular servlet. The init-param is declared inside the tag.
2) if you want to access the data whole application or anywhere in the application then you must go with
if you agree with this hit the up-arrow button

How to make an AJAX call in Magnolia Blossom?

I am new to Magnolia Blossom.
I have to perform an AJAX call in my application in Blossom.
We have a Controller per component. So I am unable to make an AJAX request.
Can anyone suggest how can I achieve this?
you may define a different Servlet (and web application context), you can define a servlet that handles all the requests that start for /rest/* and then below that on your web.xml you can define the blossom servlet.
All the rest is configuration, try to see how to create a webapp with 2 different contexts.
The controllers you're using for rendering content are not accessible to requests coming in to the servlet container. Without content they're pretty useless and won't generate meaningful output. You need a separate DispatcherServlet handling these AJAX requests.
There's two ways to achieve this. You can either add a new DispatcherServlet to web.xml or you can add a servlet to your module that is installed when your module is installed.
The latter is the better choice because you won't need to have two separate ApplicationContexts. The one created in your module on startup will be the parent of both DispatcherServlets so both can access beans within it. You also won't need to update web.xml which makes module install easier and upgrades of Magnolia easier.
In your module descriptor add this snippet:
<servlets>
<servlet>
<name>dispatcher</name>
<class>org.springframework.web.servlet.DispatcherServlet</class>
<mappings>
<mapping>/ajax/*</mapping>
</mappings>
<params>
<param>
<name>contextConfigLocation</name>
<value>classpath:/ajax-servlet.xml</value>
</param>
</params>
</servlet>
</servlets>
This, and other topics, is described in the Magnolia wiki https://wiki.magnolia-cms.com/display/WIKI/Adding+a+DispatcherServlet+to+a+Blossom+module
Depending on what you want to get out, you can also use REST module of Magnolia. To e.g. read a title of website you can just call http://localhost:8080/magnoliaAuthor/.rest/properties/v1/website/demo-project/siteTitle more details at documentation
And you can use REST module to also very easily add your own endpoints by just annotating source code.
HTH,
Jan

Create beans with property names as element names?

I am new to spring and happy to see that following works as expected:
<bean id="..." class="server.Shell">
<property name="usableCommands" value="cat"/>
</bean>
The above is in the client code, where I have provided the server.Shell. Now I would like for the clients to be able to use the following:
<shell id="...">
<usableCommands value="cat"/>
</shell>
Is there anything in springframework that I can use to map say an xsd to bean classes? Any other suggestion for easily creating a simple xml based domain language?
You can register a custom XML Namespace in Spring that would allow you to customize your configuration XML. If you're looking to create a sort of DSL in your Spring configuration XML, that might be a good place to start.
UPDATE:
Check out this link for a general example of how custom namespaces in Spring work. This pattern should hold in OSGi as well -- check out Section 6.4 of the Spring OSGi docs for an explanation. If you're new to OSGi, it can be daunting in general. SpringDM can help. Try here for some background and here for an example. Hope that helps.

Spring - usage of alias vs names

I am confused on the usage of alias. I do understand what alias is and how it is being used but i don't see how it can be any different than using names on a bean definition.
<bean id="xyx" name="abc,def" .. />
<alias name="xyx" alias="pqr"/>
Why the alias when i can use abc or def?
In my mind bean aliasing can be helpful in large system, where you can not manipulate bean names. You have option to create your own name (alias) specific for your part of the system...
from Spring documentation (3.0.x)
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/
...it is sometimes desirable to give a single bean multiple names,
otherwise known as bean aliasing...
therefore creating multiple names or/and aliasing are the same thing.
A use case maybe when you want to customize some beans that are already defined somewhere in a modular application (each module is a spring project for example), the bean maybe defined by a third-party framework/API or even your team. In that case you want that only inside your spring project call the customized version without altering other modules (projects), to do that just add the alias in your spring configuration which is indeed a powerful feature:
<alias alias="globalBeanService" name="customizedBeanService" />
Hence, whenever spring find a call to the globalBeanService, it will inject customizedBeanService for you inside your specific module.
Without this feature, you should go through all classes and modify the bean manually!!
An aliased bean will always have higher priority over a non-aliased one, and in case of having different beans with the same alias then the last one declared will have the priority. In other words, the aliased bean will override the non-aliased beans.
This can be particularly useful when creating big projects or when you are building extensions to your project and don't want to touch the original bean definition.
Alias has a specific using scenario which multiple names don't have:
Imagine multiple config xml files in your project, most of which are authored by your colleagues, and you need to add your own config.xml file. Using you'll be able to refer to a bean defined in another config file with a different name that's maybe more meaningful to your config, without having to touch your colleagues' config files.
I recently found another use case where alias easily solved a problem.
When auto configuration is active, Spring Boot provides the bean serverProperties which can be used to access information about the server currently running the web app.
In integration tests (i.e. when #SpringBootTest annotation is present) the same bean is available under the name org.springframework.boot.autoconfigure.web.ServerProperties.
Of course it is possible to use a different profile for integration testing, but that would require manual change of configuration at multiple places. However, simply by adding
<alias name="serverProperties" alias="org.springframework.boot.autoconfigure.web.ServerProperties"/>
the same configuration files can be used for integration tests and in production.
This might be a bug in Spring Boot, however alias easily solve the problem without waiting for a new release. And most certainly I have no possibility to alter the Boot configuration myself.

How to retrieve context parameters in Spring 3.1 xml context

It seems like there's been a few iterations of property support in spring it's hard to tell what's best practice and the manuals are written from the point of view of someone who is familiar with every other iteration. I feel like this should be a simple and common requirement but given how hard it's been please correct me if there's a more idiomatic way.
What I want is to pass an additional properties file to my spring web app based on a context property which the client is setting using a tomcat descriptor like so
<Context path="/foo" reloadable="true">
<Parameter name="foo.config" value="file:${catalina.base}/conf/foo.properties"/>
</Context>
In spring for the live profile I have this
<beans profile="live">
<context:property-placeholder location="classpath:timetabling.live.properties,${timetabling.config}"
ignore-resource-not-found="true" />
</beans>
So I'd assumed this doesn;t work because I'm trying to configure placeholder suppport with a placeholder. If I use a system property however then this works fine. I know that spring 3.1 has baked in support for system and environment properties so I guess my question is how can I augment this support with something context aware before the placeholder is resolved?
--Update--
looking at http://blog.springsource.org/2011/02/15/spring-3-1-m1-unified-property-management/ particularly at footnote 1, I would expect to have a DefaultWebEnvironment which should already have aceess to context init params. Now I am more confused, can someone provide me with a concrete example of context property retrieval? At this point I feel like I've read every javadoc available and they are just not helpful.
<context:property-placeholder /> sets up a PropertyPlaceholderConfigurer which reads from .properties, system properties and environment variables. A Tomcat context.xml however sets up a servlet context init parameter. So what you need is a ServletContextPropertyPlaceholderConfigurer.

Resources