Sunday, August 9, 2015

Spring basics

Spring - Basics (refreshing the basics)

This scrap note includes: 
  • Configuring beans using @Configuration
  • Creating beans and referencing them
  • Bean scopes

Configuring beans using @Configuration
You can configure actions such as configuring all the beans, reading resources from the property files, establishing connection to database in the @Configuration class.

@Configuration
@ComponentScan(basePackages = "com.deiveehan")
@PropertySource(value = "classpath:application.properties")
public class ApplicationConfig {

  @Inject
  Environment environment;

  @Bean
  public InternalResourceViewResolver configureInternalResourceViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
  }
}

Note: 
  • @Configuration is used to tell that this is a Bean configuration file. 
  • @ComponentScan is used to tell the container where to look for the class files. 
  • @PropertySource is used to define the properties file. 
  • environment.getProperty("host”) is used to get the property value. 
  • @Bean is used to define the beans. 

Creating beans and referencing them

You can create beans in 2 ways - using xml or using bean configuration files. 

Using annotation: 
@Bean is used to define a bean and 

Referencing beans: 
@Autowired is used to reference a bean
You can autowire a bean at the class level and at the method level. 

Example: 
@Configuration
@EnableMongoRepositories(basePackages = {
"com.deivee.framework.mongorepository",
“com.deivee.app.mongorepository" })
public class MongoDBConfig {

@Autowired
Environment environment;

@Bean
public Mongo mongo() throws Exception {
  return new Mongo(environment.getProperty("host"));
}
@Bean
public MongoTemplate mongoTemplate() throws Exception {
  return new MongoTemplate(mongo(),
  environment.getProperty("mongoDBName"));
}
}

and the MongoTemplate can be accessed in any bean class as below

@Autowired MongoTemplate. 

Bean scopes

There are 5 different types of bean scopes in Spring
  • singleton (single bean instance per container)
  • prototype (new bean each time when referred)
  • request (new bean for each request) 
  • session (available for the entire session)
  • globalsession (for a global http session, valid for portal application). 

For example you can create the scope for the bean when you create it. 
@Component
@Scope("prototype")
public class LoginAction {
Misc: 

  • You can use @PostConstruct to perform action after it is constructed and use @PreDestroy to perform action before it is destroyed. 
  • You can use @Profile to create profiles for different environment. 
  • You can detect the type of device that is sending the request using this String userAgent = request.getHeader("User-Agent”); the value of userAgent will be android, iPad, kindle etc., 
  • You can also use spring with other web frameworks such as JSF. 

No comments:

Post a Comment