spring essentials 2 spring series 02)

14
SPRING ESSENTIALS 2 Bean Scopes, Profiles, Testing. (Spring Series 02) Presenter: Heartin Jacob Kanikathottu

Upload: heartin-jacob

Post on 25-Jan-2017

93 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Spring essentials 2 Spring Series 02)

SPRING ESSENTIALS 2Bean Scopes, Profiles, Testing.

(Spring Series 02)Presenter: Heartin Jacob Kanikathottu

Page 2: Spring essentials 2 Spring Series 02)

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?

Page 3: Spring essentials 2 Spring Series 02)

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.

Page 4: Spring essentials 2 Spring Series 02)

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).

Page 5: Spring essentials 2 Spring Series 02)

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

Page 6: Spring essentials 2 Spring Series 02)

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() {    …

Page 7: Spring essentials 2 Spring Series 02)

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.

Page 8: Spring essentials 2 Spring Series 02)

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();

Page 9: Spring essentials 2 Spring Series 02)

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)

Page 10: Spring essentials 2 Spring Series 02)

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 {}

Page 11: Spring essentials 2 Spring Series 02)

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());    }    }

Page 12: Spring essentials 2 Spring Series 02)

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

Page 13: Spring essentials 2 Spring Series 02)

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.

Page 14: Spring essentials 2 Spring Series 02)

RESOURCES & REFERENCES

• https://spring.io/docs • http://javajee.com/spring-framework-4-essentials