2024: Domino Containers - The Next Step. News from the Domino Container commu...
Spring essentials 2 Spring Series 02)
1. SPRING ESSENTIALS 2
Bean Scopes, Profiles, Testing.
(Spring Series 02)
Presenter: Heartin Jacob Kanikathottu
2. TOPICS
• Bean Scopes
• Initialization and Destruction Code for a Spring Bean
• Spring Profiles
• JUNIT Testing of code with Profiles
• Introduction to Spring Boot
• What Next?
3. BEAN SCOPES IN SPRING
• Beans in Spring can be either singletons or prototypes:
• Singleton: one instance of the bean will be maintained by the Spring and will be returned
every time you call the getBean() method.
• Prototype: A new bean will be created every time you call the getBean() method.
• The default is singleton.
• In Java configuration, you can specify that a bean scope is prototype by specifying:
• @Scope("prototype") over the bean definition.
4. BEAN SCOPE EXAMPLE
• Example Bean Class:
• class JJWriter {
private String content="default";
//getters and setters
• }
• TODO:
• Create a Java configuration class.
• Test below with and without @Scope("prototype") over the bean definition
• Get bean, change content, get again and verify (in the test method or main method).
5. INITIALIZATION AND DESTRUCTION CODE
• Using initMethod and destroyMethod attributes of the @Bean annotation
• Advantage: We do not need source code access to component classes.
• Limitation: We cannot specify methods that accept any parameters
• Using PostConstruct and PreDestroy from javax.annotation package
• Advantage: These are standard Java annotations
• Limitation: We need to add annotations to the source code and hence need source code
access
6. EXAMPLE: INITIALIZATION AND
DESTRUCTION CODE
• Example 1: initMethod and destroyMethod attributes of @Bean
• @Bean (name = "jjwriter", initMethod="initMethod", destroyMethod="destroyMethod")
public JJWriter getJJWriter()
…
• Example 2: PostConstruct and PreDestroy
• @PostConstruct
public void initMethod() {
…
@PreDestroy
public void destroyMethod() {
…
7. SPRING PROFILES
• Can have @Profile at bean level or a @Configuration class level
• Can specify @Profile along with @Component
• You can specify the profile name as "default" to enable it as default:
• @Profile({"default","dbprofile"})
• Profiles can be activated using different ways such as system property, annotation,
web application context parameter etc.
• With xml configuration, you can use the beans element along with its attributes
profile and primary, and enclose the beans to add them to that scope.
8. EXAMPLE: SPRING PROFILES
• Config Class:
• @Configuration
public class DemoConfig {
@Bean (name = "writer")
@Profile({"default","dbprofile"})
…
@Bean (name = "writer")
@Profile("fileprofile")
public JJWriter getJJFileWriter()
…
• Test Class:
• AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("fileprofile");
context.register(DemoConfig.class);
context.refresh();
9. DIFFERENT WAYS TO ACTIVATE PROFILES
• setActiveProfiles method
• context.getEnvironment().setActiveProfiles("fileprofile");
• System property
• System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME,
"dbprofile, fileprofile");
• spring.profiles.active context parameter
• spring.profiles.active property
• @ActiveProfiles annotation (From within JUnit tests)
10. JNUIT TESTING
• You should place your test classes inside the src/test/java folder in a package similar to
your original classes.
• Any files that needs to be read from classpath has to be placed in src/test/resources
folder.
• We can enable profiles from within a JUNIT test using @ActiveProfiles annotation.
• Test specific config class and properties file is preferred:
• @Configuration
@PropertySource("classpath:/test.properties")
@ComponentScan(basePackages="com.javajee.spring")
public class TestConfig {
}
11. JNUIT TESTING
• Dependencies: spring-test and junit
• Example Test Class:
• @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { TestConfig.class })
@ActiveProfiles("dbprofile")
public class JJDatabaseWriterTest {
@Autowired
JJWriter jjwriter;
@Test
public void testJJDatabaseWriterToString()
{
Assert.assertEquals("Writing to Database!!!", jjwriter.toString());
}
}
12. INTRO TO SPRING BOOT
• Spring boot takes an opinionated view of the Spring platform and third-party libraries,
for building production-ready Spring applications. Spring Boot favors convention over
configuration and is designed to get you up and running as quickly as possible.
• Important features, as listed, are:
• Create stand-alone Spring applications
• Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
• Provide opinionated 'starter' POMs to simplify your Maven configuration
• Automatically configure Spring whenever possible
• Provide production-ready features such as metrics, health checks and externalized
configuration
• Absolutely no code generation and no requirement for XML configuration
13. WHAT NEXT?
• Please complete doing examples for all concepts mentioned.
• We will learn about Spring MVC, Spring REST, Spring Data and eventually go deep
with Spring Boot.