Springboot project
Posted on February 20, 2021
Tags: java
Default project
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
.run(DemoApplication.class, args);
SpringApplication}
}
1 Component v Prototype
@Component
annotation tells the class that it is used for DI injection. Note annotation is completely diff from decorator in python.- by default the class is a auto-initialized singleton, the Alien class will automatically get constructed and call it’s constructor
"Alien constructed"
only ONCE @Scope(value="prototype")
annotation means the class is not an auto-initialized singleton, and constructor behaves more like a regular object.
- by default the class is a auto-initialized singleton, the Alien class will automatically get constructed and call it’s constructor
@Component
public class Alien {
private int aid;
private String aname;
public Alien() {
super();
System.out.println("Alien constructed")
}
public int getAid() {
return aid;
}
@Component
@Scope(value="prototype")
public class Alien {
private int aid;
private String aname;
public Alien() {
super();
System.out.println("Alien constructed")
}
2 IOC
Bean is just a java class with private variable, getters and setters
ConfigurableApplicationContext context
is the IOC container.context.getBean(Alien.class);
indicates that context can build the Alien bean meaning context is a bean factory.
3 Proxy pattern
- A proxy wraps around all objects