Retrieval of session factory object from .xml - spring

Is there a way to get sessionFactory object from spring-hibernate combined XML file without using it (SessionFac..) as a instance variable?

If you are using pure XML and define a SessionFactory bean, you can inject it into another XML defined bean as follows:
<bean id="myService" class="com.company.service.MyService">
<!-- reference the sessionFactory bean by value -->
<property name="sessionFactory" value-ref="sessionFactory"/>
</bean>
If you have defined the SessionFactory bean in XML and then want to inject it using annotations, you simply need to add an #Autowired annotation to the field, setter, or the constructor where you are passing a SessionFactory variable.

Related

using property-placeholder with beanfactory in spring

With below XML configuration
<context:property-placeholder location="constants.properties"/>
<bean id="MyDBDetails"
class="itsmine.springcore.MyDBDetails" scope="singleton">
<property name="fName" value="${fName}" />
<property name="lName" value="${lName}" />
<property name="age" value="${age}" />
</bean>
I created a bean in my main using below 2 options:
1) using ClassPathXmlApplicationContext
2) using bean factor
The dynamic values got set when bean created using ClassPathXmlApplicationContext, but not when created using bean factory.
Could you please suggest how to make it work using bean factory ?
Thanks in advance.
When using XML declaration for bean properties, the property-placeholder is registered as a Bean Factory Post Processor for the IoC container, thus you will have the properties values available.
However, when instantiating beans programatically through a BeanFactory, this factory won't be aware of the properties values unless your configure a `` as its post processor:
BeanFactory factory = /* your factory initialized */;
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource("constants.properties"));
cfg.postProcessBeanFactory(factory);
EDIT
Note that the org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#setLocation accepts a org.springframework.core.io.Resource as its argument, i.e. you can use any of the its concrete sub-implementation depending on your needs:
org.springframework.core.io.FileSystemResource: An implementation supporting file handles.
org.springframework.core.io.ClassPathResource: An implementation for classpath resources.
org.springframework.core.io.InputStreamResource: An implementation for an InputStream...

Autowiring same class instance in two beans

I have the following code inside the beans xml configuration file:
<bean class="com.cisco.nms.discovery.test.persister.PersistenceServiceMock" id="persistenceService"/>
<bean class="com.cisco.nms.discovery.test.persister.PersistenceServiceMock" id="emsLocator"/>
<bean class="com.cisco.nms.discovery.test.persister.PersistenceServiceMock" id="navigator"/>
as a result I get 3 different instances of classes autowired.
I want to have the same class instance autowired to all these autowired variables.
How can I do it??

Spring bean creation

Is it possible to create to bean with same id with same class with different property in spring ? Like:
<bean id ="a" class= "com.tofek.A"
<property message = "khan"/>
</bean>
<bean id = "a" class = "com.tofek.A"
<property message="tofek"/>
</bean>
As per my understanding it will create, but while fetching the bean using getBean() method it will give exception like NoBeanDefinitionFoundException.
Please correct my understanding if I'm wrong?
Make sure your spring context is loaded sucessfully.
Answering your question. You can have two identical bean definitions in two different sprintContext configurations.
The bean from second context will override bean created by first one.
For example :
context1.xml
<bean id="bean1" class="org.springframework.beans.TestBean"/>
context2.xml
<bean id="bean1" class="org.springframework.beans.TestBean"/>
then, the bean from context2.xml will override bean created by contex1.xml.
It of course depends on order of creating spring contexts. The laters overrides the ones made before.
You can use getBean() to fetch bean by type or name. In this case, both bean have same id's and types, the spring wouldn't know which one you want to fetch.

Spring - List references a DOA method to get its values

I am brand new to spring and I am trying to write my first spring application.
I have set up a DOA class that accesses the DB and pulls a list of values. I would like to reference those values in a bean definition.
For Example:
I have DAO class called "JdbcDataDAO" that contains a method getValues() - I would like to reference the values in a standalone list in my bean definitions
Here is what I have:
<bean id="dataDAO" class="dao.impl.JdbcDataDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
<util:list id="myList" list-class="java.util.List">
<value>#{dataDAO.values}</value>
</util:list>
But when I retrieve the bean "myList", it contains "#{dataDAO.values}" and not the values
Any help would be appreciated - Thanks
Note sure if you can do this with SpEL. And it doesn't look good anyway - you are mixing infrastructure/configuration with business logic.
You can have a factory-bean or a #Configuration class with #Bean methods where you can inject the DAO and programatically populate the list.
You can also have a BeanPostProcessor that takes all List beans an fills them with whatever you want.

Create bean of type Set<Class<?>>

How can I create a bean of type Class?
I found a way using getClass() but that requires an instance and cannot be used via factory-method since it is not static. It also requires an extraneous bean be created for this express purpose:
<bean id="foo" class="Foo" />
<bean id="fooClass" factory-bean="foo" factory-method="getClass" />
This isn't so bad if the Foo class is easy to construct, but what if the constructor has required parameters?
I then need to create a Set of Class to wire into another bean via a property. I would create the Set such as:
<util:set id="classSet">
<ref local="fooClass"/>
</util:set>
If you really wanted to do what you describe, then you can do it like this:
<bean id="myClass" class="java.lang.Class" factory-method="forName">
<constructor-arg value="com.MyClass"/>
</bean>
But as #ChssPly76 said, if you want to inject it into another bean, you only need inject the class name, and Spring will auto-convert it into a class instance for you.
Why would you? Can you provide an example where that's actually needed?
If you only need this as a dependency (e.g. some other bean has a property of type Class), Spring's built-in ClassEditor property editor would convert a regular string into a Class instance with that name for you:
<property name="someClass" value="java.lang.String"/>
The above would result in setSomeClass(Class clazz) setter being called on the bean whose property that is.

Resources