DAO null in spring bean class with xml config - java

I'm trying to inject the uploadDao in the bean DemoHandler (XML below). But as expected it is Null. The question is how do I inject the DAO correctly.
Right now, there is a method call from WebUploadServiceImpl to DemoHandler and I can add 1 more parameter and send the value I need from uploadDao, but then that method is reused for multiple java classes and all those methods need to be refactored and that new param will not be relevant to the other classes.
Is there a way to get uploadDao to inject in DemoHandler? Any help is much appreciated. Thanks
Spring XML:
<bean id="wuService" class="com.abcd.service.WebUploadServiceImpl">
<property name="uploadDao" ref="uploadDao" />
<property name="webHandlers">
<set>
<bean class="com.abcd.service.handler.DemoHandler" />
</set>
</property>
</bean>
Code:
public class DemoHandler {
private UploadDao uploadDao;
public List handleM(...) {
...
Period period = uploadDao.anyMethod(..); --- Null here
}
}

If DemoHandler needs to set a dao, why don't you do something like this ...
<bean id="uploadDao" class="whatever.dao.UploadDaoImpl"/>
<bean id="wuService" class="com.abcd.service.WebUploadServiceImpl">
<property name="uploadDao" ref="uploadDao" />
<property name="webHandlers">
<set>
<bean class="com.abcd.service.handler.DemoHandler">
<property name="uploadDao" ref="uploadDao" />
</bean>
</set>
</property>
</bean>
You just need to replicate the injection mechanism already used in WebUploadServiceImpl. You haven't shown that code, I donĀ“t know if you use #Autowired, a set method or any other possibility.

Related

Custom function for Spring ApplicationContext

I'm heavily using Spring to develop an application and I was wondering if there's a way to register custom method which can be used when a bean is defined. Currently I'm using ClassPathXmlApplicationContext to load the bean definitions from a XLM file, then in the XML file I would like to do something like
<bean id="bean1" class="..." scope="prototype"
p:some_property='"hello" + #getCorrelativeNumber()/>
Here some_property should then be evaluated to hello0. I know I can register custom functions using StandardEvaluationContext of SpEL, however I can't figure how to make that work in the context of the ApplicationContext.
Any help is greatly appreciated.
Please refer to this blog, look at the section which shows to create an application context in which expressions are used to inject values
<bean id="engine" class="com.baeldung.spring.spel.Engine">
<property name="capacity" value="3200"/>
<property name="horsePower" value="250"/>
<property name="numberOfCylinders" value="6"/>
</bean>
<bean id="someCar" class="com.baeldung.spring.spel.Car">
<property name="make" value="Some make"/>
<property name="model" value="Some model"/>
<property name="engine" value="#{engine}"/>
<property name="horsePower" value="#{engine.horsePower}"/>
</bean>

Spring declaration for interfaces with the same implementation

I have a problem with Spring.
I have two separate interfaces, findUnconditionalDiscountValuesStrategy and findConditionalDiscountValuesStrategy. These interfaces are implemented in the same class MmfgFindPricingWithCurrentPriceFactoryStrategy.
This is declared in the spring configuration file like this:
<alias name="mmfgFindPricingWithCurrentPriceFactoryStrategy" alias="currentFactoryFindPricingStrategy"/>
<bean id="mmfgFindPricingWithCurrentPriceFactoryStrategy" class="com.mmfg.mmfgacceleratorcore.order.strategies.calculation.impl.MmfgFindPricingWithCurrentPriceFactoryStrategy" parent="abstractBusinessService">
<property name="findPricingWithCurrentPriceFactoryStrategy" ref="original-currentFactoryFindPricingStrategy"/>
<property name="configurationService" ref="configurationService"/>
<property name="mmfgSessionService" ref="mmfgSessionService"/>
<property name="mmfgUtilsDao" ref="mmfgUtilsDao" />
</bean>
Now, I use the two interfaces in a class
private FindUnconditionalDiscountValuesStrategy findUnconditionalDiscountValuesStrategy;
private FindConditionalDiscountValuesStrategy findConditionalDiscountValueStrategy;
with getter and setter (getter protected and setter #Required).
Now I have declared this class in this way in the spring configuration file:
<bean id="mmfgOrderCalculationStrategy" class="com.mmfg.mmfgacceleratorcore.order.strategies.calculation.impl.DefaultMmfgOrderCalculationStrategy">
<property name="findUnconditionalDiscountValuesStrategy" ref="currentFactoryFindPricingStrategy"/>
<property name="findConditionalDiscountValuesStrategy" ref="currentFactoryFindPricingStrategy"/>
<property name="modelService" ref="modelService" />
<property name="commonI18NService" ref="commonI18NService" />
<property name="calculationService" ref="calculationService"/>
</bean>
the ref is the same for the two interfaces, because the implementation class is the same.
But at runtime I obtain this error:
org.springframework.beans.factory.BeanInitializationException: Property 'findConditionalDiscountValueStrategy' is required for bean 'mmfgOrderCalculationStrategy'
How I have to declare the ref in the two properties?
There is a typo error. My property is called findConditionalDiscountValueStrategy. But in the class the name is findConditionalDiscountValuesStrategy.

How do I actually use a Spring pooled object?

I am working through an example of spring pooling described here.
http://docs.spring.io/spring/docs/2.0.0/reference/aop-api.html#aop-ts-pool
I have read through a very similar question here, but still don't have an answer.
How to use Pooled Spring beans instead of Singleton ones?
Assuming I have the following beans defined in my SpringConfig file, how do I actually use the pooled objects?
<bean id="businessObjectTarget" class="com.mycompany.MyBusinessObject" scope="prototype">
... properties omitted
</bean>
<bean id="poolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource">
<property name="targetBeanName" value="businessObjectTarget"/>
<property name="maxSize" value="25"/>
</bean>
<bean id="businessObject" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetSource" ref="poolTargetSource"/>
</bean>
I am doing something like this but I have no idea how to tell if it working.
#Resource(name = "businessObject")
private MyBusinessObject businessObject;
...
method() {
...
businessObject.method();
...
}
How am I actually supposed to use the pooled object?
Thanks.
PS - I am even less sure of it working now. I made the following change and submitted two threads. The first one worked, the second one got an error indicative of simultaneous use:
<bean id="poolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource">
<property name="targetBeanName" value="businessObjectTarget" />
<property name="maxSize" value="1" />
<property name="whenExhaustedActionName" value="WHEN_EXHAUSTED_FAIL" />
</bean>
I am very skeptical.

Java Spring IOC bean creation value

I need a bean like this
<bean id="studentWithSchool" class="com.model.Student" scope="prototype">
<property name="school">
<bean class="com.model.School" scope="prototype"/>
</property>
</bean>
This is OK.
My problem is I have the student returning from a method from a different bean.
I usually load the bean like this when is a property.
<property name='beanProperty' value='#{anotherBean.getBeanProperty()}'/>
But in this case I need the new bean itself being set from the other bean method (School object is returned from another bean method).
This is what I try, and of course this is wrong:
<bean id="studentWithSchool" class="com.model.Student" scope="prototype" value='#{anotherBean.getBeanProperty()}'>
<property name="school">
<bean class="com.model.School" scope="prototype"/>
</property>
</bean>
Is there any workaround?
If I understand you correctly, the studentWithSchool is created and returned by a method in anotherBean. If that's the case, you can use a factory-method:
<bean id="studentWithSchool" factory-bean="anotherBean" factory-method="getBeanProperty" scope="prototype" />
I believe you are trying to use factory patter with Spring . For that you can use factory bean from spring.
<bean id="studentWithSchool" factory-bean="anotherBeanStaticFactory" factory- method="createBeanProperty" scope="prototype"
<property name="school">
<bean class="com.model.School" scope="prototype"/>
</property>
For more detail you can use below link :-
http://docs.spring.io/spring-framework/docs/2.5.6/api/org/springframework/beans/factory/BeanFactory.html

spring: set property of one bean by reading the property of another bean?

Is it possible to set the property of one bean by reading the property of another bean? For instance, suppose I had:
class A {
void setList(List list);
}
class B {
List getList();
}
I would like Spring to instantiate both classes, and call A's setList method, passing in the result of calling B's getList method. The Spring configuration might look something like:
<bean id="b" class="B"/>
<bean id"a" class="A">
<property name="list" ref="b" ref-property="list"/>
</bean>
Alas, this made-up XML does not work.
Why not just inject B into A? Because I do not want to introduce the extra dependency. A is only dependent List, not on B.
in addition to #Kevin's answer if you are using spring 3.0 it is possible to do this with the new spring expression language
<bean id="a" class="A">
<property name="list"
value="#{b.list}"/>
</bean>
spring 3.0 documentation
There are a couple of ways. Here is one:
<bean id="b" class="B"/>
<bean id="a" class="A">
<property name="list">
<bean class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetObject" ref="b"/>
<property name="propertyPath" value="list"/>
</bean>
</property>
</bean>
Also see the <util:property-path/> element
If you are trying to do the same for a constructor then do this.
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg type="javax.sql.DataSource" value="#{jdbc.dataSource}">
</constructor-arg>
</bean>
Here "jdbc" is as mentioned below that has property "dataSource" with getter and setter and initilized as:
<bean id="jdbc" class="com.la.activator.DataSourceProvider">
<property name="myDataSourcePool" ref="dsPoolService"/>
</bean>

Categories