How spring internally creates object based on configurations - spring

Can some one shed some light on how spring creates objects for the configurations based on namespace. for eg.
<jms:jca-listener-container resource-adapter="myResourceAdapter"
destination-resolver="myDestinationResolver"
transaction-manager="myTransactionManager"
concurrency="10">
<jms:listener destination="queue.orders" ref="myMessageListener"/>
I want to know what goes on internally, how spring instantiates the object. Pointer to the code in the framework which does this would be of great help
Thanks

Here is the brief explanation of the internal workings. You can also look at short tutorial on TSS.

Related

Spring Boot - Where in code can I find this Bean. Is it a Bean?

Hi I'm looking at a Spring Boot application and I'm trying to understand everything that it does. It uses Camel and I am not finding the documentation for Camel especially helpful. Basically I might have a fundamental misunderstanding that a Camel SME would really be able to help with. The piece of code I am looking at is ...
public class SBJobScheduler extends RouteBuilder {
from("direct:alertBatch")
.log(LoggingLevel.INFO, SB_LOGGER, "#The Scheduler is going to start ::sbJob:: batch.# ")
.to("spring-batch:sbJob")
.end();
So I am trying to find how in the heck can I know where "alertBatch" is. I don't see any beans by this name, but maybe I'm missing it. I just want to know what is this value and I'm using the debugger and it doesn't tell me.
alertBatch is the name that uniquely identifies this endpoint. From Camel documentation:
The direct: component provides direct, synchronous invocation of any consumers when a producer sends a message exchange. This endpoint can be used to connect existing routes in the same camel context.
URI format
direct:someName[?options]
Where someName can be any string that uniquely identifies the endpoint.
You can read more about this component here
I suggest that you create a public constant in the same class that creates the route, this way, when you need to call this route, you just refer the created constant. This way you turn your code clean, more readable and allows the call hierarchy functionality from IDEs.

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.

Implementing a singleton bean in Spring

I am trying to create a cache class for my website using Spring and have added the following code to my applicationContect.xml file:
<bean id="SiteCache" class="Cache.SiteCache">
What I am unsure of is how to initialize this class. Do I even need to initialize it myself or does Spring take care of that when the site loads? If so, how would I accept parameters within the constructor?
I would like the class to be used most of the time, as a quicker way of accessing variables to populate the site, but I need a way of checking if there is an instance in the first place, so that I can load an XML file from source otherwise.
What would be the best way to implement a cache in spring?
Many thanks,
What I am unsure of is how to initialize this class.
By default (providing your definition) Spring will create exactly one instance of bean and use it everywhere where other code requires it.
how would I accept parameters within the constructor?
Check out 4.4.1.1 Constructor-based dependency injection:
<bean id="foo" class="x.y.Foo">
<constructor-arg ref="bar"/>
<constructor-arg value="42"/>
</bean>
and 4.4.2.7 XML shortcut with the c-namespace:
<bean id="foo" class="x.y.Foo" c:_0-ref="bar" c:_1-ref="baz">
What would be the best way to implement a cache in spring?
Using built-in Spring cache abstraction is a good start.
What would be the best way to implement a cache in spring?
In terms of implementing a cache, I would recommend using an existing Cache implementation such as EhCache or similar in conjunction with the Spring cache abstraction.
This makes caching as simple as annotating a method which should access the cache with #Cacheable. Spring will attempt to use the cache before executing the method.
Whilst it might seem simple to write your own cache the hardest part is always the cache invalidation.
It seemed all that was necessary was to load the XML file in the constructor of the cache class. I didn't even need to define a bean in the end, simply accepting it in my GET/POST methods for each controller was enough to keep the cache class a singleton. That way the XML file is only loaded once and saved into a cache object when the site gets built. After that the cache object can be used for easier access.
Thanks for the alternative suggestions though, they seem more effective on more complicated systems and it turned out mine didn't really need all that. I also had a rough idea and only needed a bit of reminding!

ZK - inject Spring beans directly into ZK beans

Is it possible to inject Spring beans directly into ZK backing beans?
In tutorials such as this I've found only an example, where application context was extracted manually from web application complex, which very unelegant and unflexible.
In fact, the VariableResolver is working, it is however hard to find, how to use it properly.
First, I had to include header in .zul file:
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
Then, use ${controllerBeanName} in apply attribute, f.e.
<window id="win" title="Typy mieszkaƄ" width="750px" border="normal"
apply="${appartmentTypeController}">
In my case, appartmentTypeController is bean extending GeneralForwardComposer, defined in spring context.
did you try the Variable-Resolver ?? i found an article about it
http://books.zkoss.org/wiki/Small_Talks/2010/December/Integrate_ZK_Spreadsheet2.0.0_with_Spring
May ZK-DL http://zk.datalite.cz/zk-dl library help you? It takes it's own approach to Spring integration, not the original ZK way.

why is jax-ws spring service reference prefixed with #, as in ws:service bean="#myService"

I've developed a web service with jax-ws and Spring using the tutorials at the jax-ws commons website. It shows you how to define and reference your service from your spring applicationContext file (https://jax-ws-commons.dev.java.net/spring/).
What is the reason for the "#" when referencing the web service? I would expect to see something more like
<ws:service name="myEventWS" ref="eventWebService"/>
but following example at the above link I created the following which works.
<bean id="eventWebService" class="com.myws.EventWS">
<property name="model" ref="EventModel"/>
</bean>
<wss:binding url="/EventWS">
<wss:service>
<ws:service bean="#eventWebService"/>
</wss:service>
</wss:binding>
<ws:service> is using a custom configuration namespace, which is a feature of Spring which allow you to express complex bean graphs using simpler namespace. The meaning and interpretation of these custom namespaces is down to the implementation in question, in this case the JAX-WS-Commons project. It seems the authors of that decided that bean=#eventWebService means what you refer to as ref="eventWebService".
I don't know whay they did it that way, maybe they thought it was more readable... maybe they thought that bean=eventWebService (without the hash) means a name, rather than a reference... I don't know. The documentation isn't very clear either.
Either way, I'm pretty sure sure it's not a core Spring syntax, nor a convention that I've seen before.
the "#" tells the bean that it's not a class, but rather a ref.
HTH
#eventWebService refers to the bean of type EventWebService (according to the default Spring naming convention when bean is is not specified).

Resources