Tag Archives: Programming

How to use JavaConfig Bean in Spring XML

Our current project is at the first stage to wire all the components together and do a simple integration test. When I took on this task, I found that all beans were defined in XML. Given the number of beans I have to create, it would be tedious to write them all in XML. Personally I prefer using JavaConfig to the XML files as the navigation is easier for me in JavaConfig. But I don’t want to change the XML configurations into JavaConfig all at once. Can I define JavaConfig Beans and use them in the XML?

A bit of search revealed a simple way. Now assume that we have a provider class as follows:

package com.example.xyz;

@Configuration
public class ResourceProvider{
    @Bean
    public SQSWrapper sqsWrapper() {
        return new SQSWrapper();
    }
}

Assume that we have an application.xml file and we want to use the SQSWrapper Bean in a bean definition in the file:

<bean id="SQSConsumer" class="com.example.xyz.SQSConsumerImpl">
    <constructor-arg ref="THE_ID_OF_THE_SQSWRAPPER_BEAN">
</bean>

To do that we need to add two extra lines to the file and then we specify the id of the SQSWrapper bean by using the method name sqsWrapper. The complete xml file looks like this:

<context:annotation-config/>

<!-- The following line brings in the beans defined in the ResourceProvider -->
<bean class="com.example.xyz.ResourceProvider" />

<bean id="SQSConsumer" class="com.example.xyz.SQSConsumerImpl">
    <constructor-arg ref="sqsWrapper">
</bean>

The first line “annotation-config” is crucial as noted in this stackoverflow answer: “while annotation-config is switched on, the container will recognize the @Configuration annotation and process the @Bean methods declared in JavaConfig properly”.

Now that saved me from creating more xml files!

Tips for Plot in R (1) — inconsistent type of coordinate parameters

The plot function in R seems really simple. But I ran into the following problem and it took me some time to figure it out.

# suppose you have two vectors v1 and v2
v1 &lt;- c(1,2,3)
v2 &lt;- c(3,4,5)
# we also create a data frame using v1 and v2
df &lt;- data.frame(v1=v1, v2=v2)
# to plot v1 against v2 (1)
plot(v1, v2)
# or we can do
plot(df$v1, df$v2)
# BUT we cannot use plot(v1, df["v2"])
# This will throw an error that 'v1' and 'df["v2"]' 
# have different length

This error confused me a bit since I think for sure v1 and df[“v2”] have the same length 3. Well it turns out they don’t.

# if you check the type of v1 and df["v2"]
class(v1) # this returns a "numeric" vector with length 3
class(df["v2"]) # this returns a "data.frame" with length 1

Ops, we got two different types of variables. We need to convert our “data.frame” to a numeric vector we can use.

# instead of using df["v2"], we could use either df$v2 or df[,"v2"].
plot(v1, df[,"v2"])

grep in R

Sometimes we need to find the indices of columns in a data frame that match a pattern and here comes grep:

v1 <- c(1,2,3)
v2 <- c(3,4,5)
tt2 <- c(5,6,7)
tt3 <- c(9,0,8)
df <- data.frame(v1=v1, v2=v2, tt2=tt2, tt3=tt3)
# suppose you want to find the columns with the pattern "tt"
ttIndices <- grep("tt", colnames(df))