this.messageProvider = provider;
}
public MessageProvider getMessageProvider() {
return this.messageProvider;
}
}
In Listing 5-66, we used @Named to define that it's an injectable bean. Notice the @Singleton
annotation. It's worth noting that in the JSR-330 standard, a bean's default scope is nonsingleton, which
is like Spring's prototype scope. So, in a JSR-330 environment, if you want your bean to be a singleton,
you need to use the @Singleton annotation. However, using this annotation in Spring actually doesn't
have any effect, because Spring's default scope for bean instantiation is already singleton. We just put it
here for demonstration, and it's worth noting the difference between Spring and other JSR-330
compatible containers.
For the messageProvider property, we use @Inject for setter injection this time and specify that a
bean with the name messageProvider should be used for injection.
Listing 5-67 defines a simple Spring XML configuration for the application (jsr330/jsr330.xml).
Listing 5-67. Spring XML Configuration (JSR-330)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.apress.prospring3.ch5.jsr330"/>
<bean id="message" class="java.lang.String">
<constructor-arg value="You are running JSR330!"/>
</bean>
</beans>
You don't need any special tags to use JSR-330; just configure your application like a normal
Spring application. We use <context:component-scan> to instruct Spring to scan for the DI-related
annotations, and Spring will recognize those JSR-330 annotations. We also declared a Spring bean
called message for Constructor Injection into the ConfigurableMessageProvider class. Listing 5-68
shows the testing program.
Listing 5-68. JSR-330 Example
package com.apress.prospring3.ch5.jsr330;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Jsr330Example {
public static void main(String[] args) {
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home