spring in action 2.0读书笔记(三)
<bean id="baseSaxophonist" ref="saxophone" /><property name="song" value="Jingle Bells" /></bean><bean id="kenny" parent="baseSaxophonist" /><bean id="david" parent="baseSaxophonist" />
Spring inheritance(different from Java inheritance): abstract and parent, is commonly used to reduce the amount of redundant XML. e.g: TransactionProxyFactoryBean.
Spring提供了method injection功能,使你能够在runtime期间把method注入到class里.Spring提供了2种方式的method injection:
1.Method replacement—Enables existing methods (abstract or concrete) to be replaced at runtime with new implementations.
<bean id="magicBox" replacer="tigerReplacer" /></bean> <bean id="tigerReplacer" /> import org.springframework.beans.factory.support.MethodReplacer;public class TigerReplacer implements MethodReplacer { public Object reimplement(Object target, Method method, Object[] args) throws Throwable { return "A ferocious tiger"; }}2.Getter injection—Enables existing methods (abstract or concrete) to be replaced at runtime with a new implementation that returns a specific bean from the Spring context. (这种方式是for"使用get开头的,而且返回值是在spring里有bean定义类型"的方法)
<bean id="stevie" bean="guitar" /></bean>
There’s no need to write a MethodReplacer class.
Injecting non-Spring beans
对于在spring里有定义bean,但又不是通过spring来创建的class,又应该如何使用spring呢?这种情况是可能的:例如,一个由ORM创建的object则不是spring创建的。
第一步:把bean定义成abstract:
<bean id="pianist" /> </property></bean>
第二步:如果把上面的定义(注意上面设置的id)和class联系起来
@Configurable("pianist")public class Instrumentalist implements Performer {…}@Configurable的作用有2个:
1> 它表明Instrumentalist的实例会被纳入和配置到spring container里,即使它是在outside of spring创建。
2> 它把Instrumentalist class与id为pianist联系在一起,当spring在配置一个实例时,它会寻找pianist bean作为模板来进行配置(包—I)
第三步:在spring configure file里添加下列代码
<aop:spring-configured />
它表示有一些在外部创建的beans,需要被配置和纳入进spring container.
请注意:<aop:spring-configured/>是用到aspectJ,这意味着你的APP必须在一个AspectJ-enabled JVM里运行。
The best way to AspectJ-enable a Java 5 JVM is to start it with the following JVM argument:
-javaagent:/path/to/aspectjweaver.jar
Registering custom property editors
extends java.beans.PropertyEditorSupport
public class PhoneEditor extends java.beans.PropertyEditorSupport { //text value example “111-111-111” public void setAsText(String textValue) { String areaCode = textValue.substring(0,3); String prefix = textValue.substring(4,7); String number = textValue.substring(8); PhoneNumber phone = new PhoneNumber(areaCode, prefix, number); setValue(phone); }}<bean name="code"><bean id="propertyConfigurer" value="jdbc.properties" /></bean><bean id="dataSource"value="${database.url}" /> <property name="driverClassName" value="${database.driver}" /> <property name="username" value="${database.user}" /> <property name="password" value="${database.password}" /></bean>Resolving resource messages
<bean id="messageSource"name="code">class HeartbeatForwarder implements ApplicationListener { public void onApplicationEvent(ApplicationEvent event) { if (event instanceof HeartbeatEvent) { System.out.println("Received heartbeat event: " + event.getTimestamp()); } }}第三步:在spring里注册该listener。
第四步: 用来publish event的bean,因为publish event需要用到applicationcontext,所以implements ApplicationContextAware
class HeartbeatTask extends TimerTask implements ApplicationEventPublisherAware { private ApplicationEventPublisher eventPublisher; public void run() { HeartbeatEvent event = new HeartbeatEvent(this); eventPublisher.publishEvent(event); } public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) { this.eventPublisher = eventPublisher; }}Scripting beans
把script bean注入到java class bean
<lang:jruby id="lime" script-source="classpath:com/springinaction/scripting/Lime.rb" script-interfaces="com.springinaction.scripting.Lime" />
把java class bean注入到script bean
<lang:groovy id="coconut"script-source="classpath:com/springinaction/scripting/Coconut.groovy"> <lang:property name="lime" ref="lime" /></lang:groovy>
Refreshing scripted beans
<lang:jruby id="lime"script-source="classpath:com/springinaction/scripting/Lime.rb"script-interfaces="com.springinaction.scripting.Lime"refresh-check-delay="5000"/>
Writing scripted beans inline
<lang:bsh id="lime" script-interfaces="com.springinaction.scripting.Lime"><lang:inline-script><![CDATA[void drink() {System.out.println("Called the doctor woke him up!");}]]></lang:inline-script></lang:bsh>