I’m trying to understand a piece of Spring code that I need to adapt.
I have:
<bean id="…" class="…">
<property name="expr"
value="teams.contains(member.team) and not empty(member.projects)" />
</bean>
The corresponding class has a field
private Expression expr;
of type
org.apache.commons.jexl2.Expression
Now I am trying to find the appropriate Spring annotation to get rid of the XML file. But I cannot even understand how a simple String property can be injected as a jexl2.Expression object. How does this work?
A friend found the answer:
There was another XML file with this:
<bean id="bean_for_ExprConverter" class="package.of.custom.ExpressionConverter">
<constructor-arg ref="bean_for_JexlEngine"/>
</bean>
and also, in the project’s properties:
application.spring.converters = #{{\
#'bean_for_ExprConverter'\
}}
Thus, as long as the converter bean is defined, it should be enough to simply inject the expression string with the #Value annotation.
Related
I need to add OAuth client to a Spring 3 project. I need to use xml based configuration. And I want to know the xml equivalent of the following bean configuration I found in another Spring project. Note that there is an input parameter in the bean definition where an object of type OAuth2ClientContext is being passed (OAuth2ClientContext is an Interface) and is named clientContext. But no bean definition is written anywhere for clientContext. What does this mean? And how would you write this in xml?
#Bean
public OAuth2RestTemplate oauth2RestTemplate(OAuth2ClientContext clientContext){
return new OAuth2RestTemplate(oauth2Resource(), clientContext);
}
The configuration should be like this
<bean id="client" class="package.OAuth2ClientContext" />
<bean id="resource" class="package.Oauth2Resource" />
<bean id="restTemplate" class="package.Oauth2Resource">
<property name="nameOfPropertyResource" ref="resource" />
<property name="nameOfPropertyClient" ref="client" />
</bean>
are you sure that the bean client is not declared? Maybe it’s declared in some jar? If yes you should find it’s name and use the name in the ref
I have some doubt about what exactly do these 3 Spring SpEL example:
1) FIRST EXAMPLE:
<bean id="rewardsDb" class="com.acme.RewardsTestDatabase">
<property name="keyGenerator" value="#{strategyBean.databaseKeyGenerator}" />
</bean>
It seems to me that this code snippet injet an inner property named databaseKeyGenerator (that is inside the strategyBean bean). So in this case SpEL is used to access to a specific bean property in the classica OO logic. Is it true?
2) SECOND EXAMPLE:
<bean id="strategyBean" class="com.acme.DefaultStrategies">
<property name="databaseKeyGenerator" ref="myKeyGenerator"/>
</bean>
It seems to me that SpEL is not used, or am I missing something?
3) THIRD EXAMPLE:
<bean id="taxCalculator" class="com.acme.TaxCalculator">
<property name="defaultLocale" value="#{ systemProperties['user.region'] }"/>
</bean> Equivalent
It is used to inject a property value taken from a property file
Is it correct or I a missing something or am I misinterpreting the SpEL logic?
The first and second examples come together. The second actually uses no SpEL at all. Its sole purpose is to help understand the first one. So you are not missing something regarding the first two.
As for the third one, systemProperties is a predefined variable and you use it to access system properties. Except from the standard VM system properties you can also access those that you pass with -D when starting the application.
You can access a property file the same way, after creating a bean to reference them, by using the bean id instead of systemProperties. For example
<util:properties id="appProps" location="classpath:application.properties" />
and then
<property name="propOne" value="#{appProps['some.property'] }"/>
I am developing a project and using 3rd party libraries. Let's say I use a library which gives me the object ExtObj. In my project I have a class MyObj, which uses ExtObj. How can I configure spring 3 to inject ExtObj in MyObj?
I tried to research the topic on the internet, but I didn't find a straight answer. I would like to use xml configuration and maybe (?) #Autowired, not #EJB or #Inject
Thanks in advance!
UPDATE
my guess was:
<bean id="myObj" value="me.MyObj">
<property name="extObj" value=" ... ??? ...">
</bean>
So, I don't know what I should put into value. I guess that's where the reference to the external object goes. But spring can only reference objects that have been already defined/configured in spring. So:
<bean id="extObj" value="ext.lib.ExtObj">
<bean id="myObj" value="me.MyObj">
<property name="extObj" value="extObj">
</bean>
Is that configuration right?
First you need to define a bean for your ExtObj in your application context (an xml file or a #Configuration class). Eg. if ExtObj has a constructor taking a String you can define the bean this way:
<bean id="extObj" class="ext.lib.ExtObj">
<constructor-arg value="SomeString"/>
</bean>
To define MyObj you can use constructor injection:
<bean id="myObj" class="me.MyObj">
<constructor-arg ref="extObj"/>
</bean>
or setter injection:
<bean name="myObj" class="me.MyObj">
<property name="extObj" ref="extObj"/>
</beans>
If you use setter injection then MyObj needs to have a setter setExtObj. If you use constructor injection MyObj needs to have a constructor taking an instance of the ExtObj class.
Of course you can inject a 3rd party library, as long as it has constructors that Spring can access.
You can use either XML or annotations - your choice.
You need to ask Spring to instantiate the instance(s) of the library class and then inject that into your objects that need them.
You do this every time you create a Spring data source that uses a JDBC driver. That's a 3rd party library.
In all the examples of autowiring that I have found, the example is about one <bean> autowire attribute which is set for example to byName, and the has only one property value which is supposed to be set through autowiring.
My question is what if a <bean> has multiple properties that you want to set through autowiring? No one seems to explain that situation. Can someone explain if I can or if I should use autowire to set multiple properties in a bean? The following is an example of such a situation where I want to set the account and credit properties of the customer bean by autowiring:
<beans>
<bean name="customer" class="ultratech.com.Customer" autowire="byName">
<bean name="account"/>
<bean name="credit>
</beam>
<bean name="account" class="ultratech.com.Account"/>
<bean name="credit" class="ultratech.com.Credit"/>
</beans>
Also, please correct me if I'm wrong, but if I were to use annotation (#Autowire), then my problem is easily solved, since I would be able to add #Autowire to any property of a bean separately.
[EDIT: edited to reflect on updated question]
Your question is much more clear right now. You seem to think (if I follow your thinking properly), that in the autowire="byName" you are supposed to provide a bean name instead of byName value.
That is not correct. The autowire attribute can take a few possible values and byName is one of those. When you set autowire to byName like here:
<bean name="someBean" class="foo.bar.Baz" autowire="byName />
then Spring will look at all the fields in someBean (foo.bar.Baz class) and attempt to wire all fields of this object on a per name basis. That is, (in your case) if a Customer class has a field account, Spring will look in its context and try to find a bean with name account to inject into the Customer bean.
If you define two such beans:
<bean name="customer" class="ultratech.com.Customer" autowire="byName" />
<bean name="account" class="ultratech.com.Account" />
then you are good to, if Customer is a class along this lines:
public class Customer {
(...)
private Account account;
(...)
}
Here is what your XML code snippet should look like, assuming that your Customer class has fields named account and credit:
<beans>
<bean name="customer" class="ultratech.com.Customer" autowire="byName" />
<bean name="account" class="ultratech.com.Account" />
<bean name="credit" class="ultratech.com.Credit" />
</beans>
Apart from "byName" autowiring, you can autowire:
no - default - no autowiring
byType - looks for a bean of the property type - be wary, though - only one bean of this type is allowed for autowiring byType; if there is more then one an exception is raised
constructor - works just like byType, but looks for constructor parameters only; all of constructor parameters have to be satisfied with exactly one bean of each respective type
See Spring reference for more info:
http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-factory-autowire
I would like to get inner bean by it's name. Is it possible with Spring API?
Right now I'm using such Spring 2.5 API
ConfigurableApplicationContext.getBean(String paramString)
Example of XML:
<bean id="parent" parent="t_Parent">
<property name="items">
<bean id="child" parent="t_Child">
<property name="ABC" value="test"/>
</bean>
</property>
</bean>
I would like to get inner (t_Child) bean by id "child". E.g. ConfigurableApplicationContext.getBean("child"). Spring can't find such bean (because it's inner). At the same time .getBean("parent") works fine.
Any thoughts?
You can't.
From the docs:
A element inside the or elements is used to define a so-called inner bean. An inner bean definition does not need to have any id or name defined, and it is best not to even specify any id or name value because the id or name value simply will be ignored by the container.
If you need it like that, define it as a regular bean.
You can't, but you can create you inner bean outside (so it's no longer an inner bean...) and then reference it inside the property:
<bean id="child" parent="t_Child">
<property name="ABC" value="test"/>
</bean>
<bean id="parent" parent="t_Parent">
<property name="items" ref="child"/>
</bean>
Apart from the other (mostly valid) answers and solutions, I guess the spring way would be to use the BeanWrapper interface:
final BeanWrapper bw =
new BeanWrapperImpl(applicationContext.getBean("parent"));
Object innerBean = bw.getPropertyValue("child");
But I guess that implies that there must be a getter for the property (not only a setter).
Reference:
BeanWrapper (javadoc, 2.5 version)
Bean manipulation and the BeanWrapper (reference, 2.5 version)
If you move up to Spring 3.x, you should be able to do this with the Spring Expression Language. There are examples of directly referencing a bean property from another property (like in link text). The code to do this from Java would be somewhat similar, although I can't find an exact example of this scenario.
However, I would say that if you're trying to use "getBean()", you're doing something wrong. You could just as easily use the SpEL in your context to define a bean or a bean property that references that inner bean.