Autowiring Spring Method in XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- Beans to retrieve Persistence Manager -->
<!-- Static method -->
<bean id="persistenceManagerFactory" class="javax.jdo.JDOHelper"
factory-method="getPersistenceManagerFactory">
<constructor-arg value="transactions-optional" />
</bean>
<!-- Non static method -->
<bean id="persistenceManager" factory-bean="persistenceManagerFactory"
factory-method="getPersistenceManager" destroy-method="close">
</bean>
</beans>
Labels: Spring
Spring Notes
Autowiring
1. Does setter injection (if an empty constructor was available). Constructor second.
2. By setting a class as @Component, the id is automatically the class name with the first letter in lower case. (e.g. Class Person Spring id is "person").
3. You can overwrite autowiring with xml files
4. Must have "context:component-scan base-package="package name" />" in the xml to tell Spring there are beans to scan
5. @Autowire wires by type, not by name
Labels: Spring
Custom Spring Property Editor
Note: There's only one instance of property editor for each bean. So if you use the same bean (even if it's a prototype), it uses the same instance of that object. Solution: create two beans or don't use PropertyEditor.
Code
package com.blogspot.sourie;
import java.beans.PropertyEditorSupport;
public class SocialSecurityPropertyEditor extends PropertyEditorSupport {
@Override
public void setAsText(String value){
if (value != null){
value = value.replace(" ", "");
if (value.length() == 9){
Integer.parseInt(value);
String area = value.substring(0, 3);
String group = value.substring(3,5);
String serial = value.substring(5);
setValue(new SocialSecurity(area, group, serial));
}
}
}
}
In XML
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer" id="customEditorConfigurer">
<property name="customEditors">
<map>
<entry key="com.blogspot.sourie.SocialSecurity">
<bean class="com.blogspot.sourie.SocialSecurityPropertyEditor">
</bean>
<entry key="com.blogspot.sourie.PhoneNumber">
<bean class="com.blogspot.sourie.PhoneNumberPropertyEditor">
</bean>
</entry></entry></map>
</property>
</bean>
<bean class="com.blogspot.sourie.Person" id="phonePerson">
<property name="phoneNumber" value="111 222 3333"></property>
</bean>
Labels: Spring