By passing this argument in, we can take away the line that sets the active profile and runs the
program. The results are the same as the programmatic way. You can change the JVM argument to
"highschool" and run the program again.
In Chapter 21, we will discuss how to use and activate profiles in web applications.
You can also use profiles when using a Java configuration instead of XML. Let's see how to do it. Like
with the XML version, we develop two different Java configuration classes and define the profile by using
the @Profile annotation. Listings 5-56 and 5-57 show the Java configuration file for the kindergarten and
high school profiles, respectively.
Listing 5-56. Profile Java Configuration for Kindergarten
package com.apress.prospring3.ch5.profile;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import com.apress.prospring3.ch5.profile.kindergarten.FoodProviderServiceImpl;
@Configuration
@Profile(value="kindergarten")
public class KindergartenConfig {
@Bean
public FoodProviderService foodProviderService() {
return new FoodProviderServiceImpl();
}
}
Listing 5-57. Profile Java Configuration for High School
package com.apress.prospring3.ch5.profile;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import com.apress.prospring3.ch5.profile.highschool.FoodProviderServiceImpl;
@Configuration
@Profile(value="highschool")
public class HighschoolConfig {
@Bean
public FoodProviderService foodProviderService() {
return new FoodProviderServiceImpl();
}
}
You can see the two classes just mimic the two XML files that we presented earlier, with the @Profile
annotation specifying the corresponding application profile to which it belongs. Let's implement another
testing program to see the Java profile configuration in action. Listing 5-58 shows the testing program.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home