Geek time: brother pony's learning notes
Main implementation strategies of IoC: the interviewer always asks the difference between IoC and DI. Does he really understand it?
Main implementation strategies of Ioc
- Service positioning mode. Java EE components are usually obtained through JNDI technology
- Dependency injection
- Context dependent query
- Template method
- Policy mode (not too deep)
It can be said to be dependency lookup and dependency injection
Responsibilities of IoC container: in addition to dependency injection, what responsibilities does IoC cover?
- Decoupling between implementation and task execution
- Focus on the ultimate goal of the design, not its specific realization
- Release this module
- Module cancellation (for example, if the dependency is changed, the impact is obvious in case of synchronous call, but the impact may not be obvious in case of IOC)
Vernacular duty
- General Responsibilities
- Dependency processing
-Dependency lookup
-Dependency injection - Life cycle management
-Container
-Managed resources (Java Beans or other resources) - to configure
-Container
-Externalized configuration
-Managed resources
Implementation of IoC container
Traditional IoC container implementation: are JavaBeans also IoC containers?
Traditional JavaBeans implementation
Anemia model, only writable methods, readable methods, default construction parameters
public class Person { /** * Age */ private Integer age; /** * name */ private String name; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Get JavaBeans meta information
public class BeanInfoDemo { public static void main(String[] args) throws IntrospectionException { //Java operates in an introspective way BeanInfo beanInfo = Introspector.getBeanInfo(Person.class); Stream.of(beanInfo.getPropertyDescriptors()) .forEach(propertyDescriptor -> { System.out.println(propertyDescriptor.toString()); }); } }
result
java.beans.PropertyDescriptor[name=age; propertyType=class java.lang.Integer; readMethod=public java.lang.Integer ioc.eg1.Person.getAge(); writeMethod=public void ioc.eg1.Person.setAge(java.lang.Integer)] java.beans.PropertyDescriptor[name=class; propertyType=class java.lang.Class; readMethod=public final native java.lang.Class java.lang.Object.getClass()] java.beans.PropertyDescriptor[name=name; propertyType=class java.lang.String; readMethod=public java.lang.String ioc.eg1.Person.getName(); writeMethod=public void ioc.eg1.Person.setName(java.lang.String)]
Why do you get this information?
Let's take a look at what's in the BeanInfo source code
public interface BeanInfo { //Description information of Bean BeanDescriptor getBeanDescriptor(); //Description of the Bean event EventSetDescriptor[] getEventSetDescriptors(); //Field information of Bean PropertyDescriptor[] getPropertyDescriptors();
Here we get the meta information of the Person field, but why is there an additional class field?
Because each Object has a readable method getClass(), and then it will be regarded as a default property and mistaken for a property
So there will be one more attribute class above, and it has only one reading method
If you want to turn it off, pass one more parameter
BeanInfo beanInfo = Introspector.getBeanInfo(Person.class,Object.class);
The second parameter is the parent class to prevent multi-level classes from printing out. This parameter is stopClass