Thursday, December 24, 2020

Not the best but easily the most talented

Ronaldo Da Lima is possibly the most talented person I've ever seen or read about. It's unbelievable watching him play(When he played). Especially when he was very young(17-22). When he turned on, his movement on the ball would make the most talented footballers look contrived(albeit minutely). It's quite surprising looking at someone as talented as Messi, Ronaldinho or Cruyff move with the ball. You immediately think you're watching the best ever player. But watching Ronaldo Da Lima (when he turned on) is something completely different I haven't seen anywhere else. There is nothing contrived about his movement(zero). 


For example, Messi and Cruyff's movements are(were) very similar and very natural on the ball. They could drive the ball effortlessly through several players on the pitch instinctively. But sometimes when held up in tight areas you could see the training kick in and a rehearsed move from the training pitch would save the day. Not with Ronaldo. Another one is Ronaldinho who's one of if not the best footballer of all time(in my opinion possibly the most entertaining) and although he was natural on the ball, his movement was akin to dancing. Ronaldo Da Lima's on the other hand had ginga(movement) at its purest. It was as if you were watching God's instructions on how a football player should move. 


He's definitely not the best soccer player to grace the game, that's an endless debate where others who've worked towards that goal deserve that title(Messi, Cristiano Ronaldo, Pele, Gerd Muller). But as others have said (Mourinho, Zidane, Seedorf, Zlatan, Sir Bobby, Balotelli,...) or alluded to , he's the most talented. And that to me is unequivocal.

Monday, December 14, 2020

Bean Configuration without XML only JAVA CODE

 We can achieve this by creating a configuration class. The configuration class will have the following syntax


@Configuration

public class SportConfig {


The configuration annotation tells spring that this is a spring config class. We then need to add the component scanning annotation which will tell Spring which package to scan for components


@Configuration

@ComponentScan("com.rogersentongo.springdemo")

public class SportConfig{


With this we can simply replace ClassPathXml.... with AnnotationConfigApplicationContext in our main file when building the configuration object.

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SportConfig.class);


Further more we can configure each bean individually in the configuration file. We do this by eliminating the componentscan annotation(not necessary) and simply creating a bean annotation methods that inject dependencies


@Configuration

//@ComponentScan("com.rogersentongo.springdemo")... we can also leave this in as it wont be used

public class SportConfig{

//Dependency bean

@Bean

public FortuneService happyFortuneService()

{ return new HappyFortuneService();}


//Implementation Bean

@Bean

public Coach swimCoach()

{return new SwimCoach(happyFortuneService());}


In the implementation for Coach, we need to ensure that the constructor initializes the dependency.



Furthermore we can insert data from properties file by adding the @PropertySource annotation:

Given property file containing:

foo.email = rs@gmail.com

foo.team = bearcats


@Configuration

@ComponentScan("com.rogersentongo.springdemo")

@PropertySource("classpath:sport.properties")

public class SportConfig{


With this we can initialize private fields in our implementations like this

@Value("${foo.email}")

private String email;


@Value("${foo.team}")

private String team;





Saturday, December 12, 2020

Post Construction and Pre Destroy

Very important is being able to define methods that can be called immediately after an instance is created and before that instance is destroyed. We annotate these methods with @PostConstruct and @PreDestroy. These methods must be no-arg. The former will be called after a constructor and the latter will be called right before the instance is destroyed

Dependency injection using Java Annotations

We can carry out dependency injection in Spring using Java annotations in three scenarios. 

  • Constructors
  • Setter functions
  • On Fields
We tell Spring to find an implementation for a dependency by using the @Autowired annotation. This will find an implementation of the dependency. If the dependency has several implementations, we'll get an error affecting all implementations. It's best to determine which implementation is appropriate by using the @Qualified annotation. 

@Autowired
@Qualified("randomFortuneService")
private FortuneService fortuneservice

remember when using default class implementation name of a dependency implementation, we use lowercase for the first letter. 

Furthermoer Bean Scope can also be defined using annotations

@Component
@Qualified("soccerCoach")
@Scope("prototype")
public class TennisCoach implements Coach {

Friday, December 11, 2020

Inversion of Control using Java Annotations instead of XML

 So far we've been using bean tags to describe our bean classes and to also set tags. Instead, we can more easily use a single tag in our configuration file that will enable us to use annotations similar to @Override. Here we save so much space while also writing simpler to understand code.


In our configuration file, we eliminate all current bean tags and simply replace with

<context:component-scan base-package="com.rogersentongo.springdemo"/>


This provides spring with the base package to search for components


Then for every class implentation we add the annotation @Component in order to access that bean

@Component

public class TennisCoach implements Coach{


We can now access the tennis coach implementation using the string "tennisCoach". Same as class name except the first letter is lower case. Another option is to specify the name if you want


@Component("thatgoodCoach")

public class TennisCoach implements Coach{


Now we can access the bean using the string "thatgoodCoach"!!

Singleton vs Prototype Pattern

In spring, we can add the scope attribute to a bean tag in the configuration file. This will ensure that we can determine whether a single instance of an object is shared amongst several variables(Singleton) or a new object is instantiated whenever a variable is trying to instantiate that bean object(Prototype).

Thursday, December 10, 2020

Dependency Injection in Spring Using XML

So in Spring dependency injection enables us to add functionality from other classes to our implementations of object interfaces. We add these dependencies as member variables of implementation classes, then make functions to use these member variables. The configuration is done via XML where we create a bean config tag for the dependency and add either constructor or setter injection. Constructor injection requires the dependency is initialized through a constructor while setter injection requires the dependency is initialized through a setter method. There are nuances to the tags needed to create the configuration for these two methods. I would recommend looking at the xml config file of the spring-demo-one app to see these nuances.