Wednesday, 7 February 2018

How to use beans with annotations ?


Spring container manages all the beans specified in XML config or defined with annotations like, @component, @Resource, @Bean etc.

@Component : Used to define beans (managed by Spring container)
public abstract class Shape {
    abstract void draw();
}


@Component
public class Arc extends Shape {
    public void draw() {
        System.out.println("Draw an Arc.....");
    }

}

By default, bean ID / name will be the same as class name.
Best practice is to use bean ID / name with every bean with @Component :
@Component("arc")
public class Arc extends Shape { ... }


// Get bean objects from Spring container 
Shape arcA = ctx.getBean(Arc.class);
System.out.println("Arc object : " + arcA);
Shape arcB = ctx.getBean(Arc.class);
System.out.println("Arc object : " + arcB);

Both will print the same object, because by default Spring beans are singleton.


@Scope : Used to define bean scope
In order to create different objects every time you can define scope = prototype

@Scope("prototype")
@Component

public class Arc extends Shape { ... }

Best practice is to use Singleton beans, until there is no data maintained inside the beans.
If outer class is singleton and inner one is prototype, there is no sense making inner class prototype, as there will be always a single object outside.


@Bean : Used to define another object as Spring bean
When you create beans using "new" keyword, it will not be managed by Spring container.
Still you can register this created object using @bean and inject at other places.
@Bean
public Shape arc() {
   return new Arc();
}



Monday, 5 February 2018

How to create a Springboot applicaiton ?




1. Open eclipse Neon
3. Generate a "Maven project" with "Java" and Spring boot 1.5.10
    (By default stable version will be selected)
4. Click "Switch to the full version" for more config
5. Provide group = com.portal | artifact = core | Name = Portal Core | Desc =Base framework for Springboot app  | Packaging = JAR | Java version = 8
6. Click Generate

It will give you a zip to download.

7. Save the zip and extract

8. Put folder in workspace (C:\java-developper-softwares\gitrepo\portal -> core)

9. In eclipse, Import > Maven > Existing Maven project

10. Choose root dir = C:\java-developper-softwares\gitrepo\portal\core
       POM must be auto selected.
11. Finish

12. Change in pom.xml according to requirement, and save. It will update all the dependencies.
spring-boot-starter : To run Spring as an standalone application.
spring-boot-starter-web : Also required to run Spring as a Web application or API.

It gives the below class :
@SpringBootApplication
public class TrainingApplication {
    public static void main(String[] args) {
       ApplicationContext ctx = SpringApplication.run(TrainingApplication.class, args);

    }
}


You can write your code in main method.

Friday, 8 December 2017

How to open and work with eclipse faster ?


1. Make folder java-developper-softwares in C:/
2. Install/unzip/copy eclipse, maven, ant, workspace and source code in same folder.
3. Work efficiently & enjoy

Friday, 1 December 2017

How to check processors/cores, memory and disk space using single command ?


Single command to check Processors/cores, Memory and Disk space


# echo "Processor : $(grep "processor" /proc/cpuinfo | wc -l)" && grep "cpu cores" /proc/cpuinfo | awk -F: '{num+=$2} END {print "Cores     :", num}' && echo "Memory    : $(head -1 /proc/meminfo | awk '{print $2}') KB" && echo "$(fdisk -l | grep "Disk /dev" | grep -v "mapper" | awk '{num+=$3} END {print "Disk      :", num}') GB"



Processor : 4
Cores     : 4
Memory    : 8011500 KB
Disk      : 375.8 GB