编程知识 cdmana.com

Catalog

IOC Inversion of control

Inversion of control

Dependency injection

IOC Who controls who , What's in reverse ?

Who depends on whom is dependency injection , What was injected ?

IOC and DI Comparison of

IOC Containers

Concept

Components Bean

Concept

Scope

Components Bean register

be based on XML

be based on java To configure

Automatic assembly


 

IOC Inversion of control

Inversion of control

IOC It's called inversion of control , It means how an object gets the reference of the object it depends on , Objects are controlled by containers , It's not up to us to new.

Inversion of control , It's not a technology , It's a design idea . stay Java In development ,Ioc It means that you design the object to be controlled by the container , Instead of traditional direct control within your object . So the inversion of control is , About how an object gets a reference to the object it depends on , This reversal of responsibility .

Inversion of control IOC And dependency injection DI, Often foolishness can't tell , Actually IOC and DI It's just a different way of expressing the same concept .

Dependency injection

DI It's called dependency injection , It means that the container dynamically injects a dependency into the component . The purpose is not to extend functionality , It's about improving component reuse .

Why is it different expression of the same concept ? Through the following understanding and comparison, we can find that :

IOC Who controls who , What's in reverse ?

  1. Who controls who?
    • Certainly IOC Container controls object
  2. What to control
    • It mainly controls the acquisition of external resources , Including objects 、 Documents, etc.
  3. Why is reversal
    • Because containers help us find and inject objects , Object is just passively accepting dependent objects
  4. What's in reverse
    • The acquisition of dependent objects is reversed

Who depends on whom is dependency injection , What was injected ?

  1. Who depends on whom
    1. Of course, the application depends on IoC Containers
  2. Why to rely on
    1. Application needs IoC Container to provide external resources required by the object
  3. Who infuses who?
    1. apparently IoC Container injection an object in an application , Application dependent objects
  4. What was injected
    1. It is the external resources needed to inject an object ( Including objects 、 resources 、 const data )

IOC and DI Comparison of

therefore , From the above split understanding we find that ,IOC and ID It's all about A Module depends on B modular , then A How to introduce modules B Module thing .

Tradition Java SE Programming , We pass directly inside the object new To create an object , It's the program that creates the dependent object ;

and IOC The angle is :IOC There is a special container to create these objects , by Ioc Container to control Creation of images , From the initiative creation of the original program to the passive creation of the program IOC Container to create .

and DI The angle is :A The module originally depends on B modular , But now it's dependent on IOC Containers , because IOC The container contains B Object of module , And by the IOC The container is right A Module injection depends on B Module object .

IOC Containers

Concept

In each framework there is a concept of a container , The so-called container is the common service , encapsulated , then , Users only need to follow certain rules , We can achieve unity 、 flexible 、 Security 、 convenient 、 Fast purpose

Container with dependency injection , Responsible for instantiation 、 location 、 Configuring objects in an application and establishing dependencies between them

Components Bean

Concept

from IoC Container management of the objects that make up the application we call it Bean.

Bean Is the Spring Container initialization 、 Objects of assembly and management , besides ,bean It's no different from other objects in the application

Scope

bean The scope of @scope Comments to set .

Four types of

singleton: The singleton pattern : For all users with a specific name of the shared instance .ioc Container startup will call methods to create objects and put them in ioc In the container . Every time I get it from the container (map.get()) Take in ;

prototype: Archetypal model : For each user to have their own object .ioc Container startup does not call methods to create objects in the container . The method is called to create the object every time it gets ;

request: In the same request , Get the same instance , Different requests get different instances in the same request , Get the same instance , Different requests get different instances ;

session: In the same conversation , Get the same instance , Different sessions ask for different instances ;

Components Bean register

be based on XML

bean Instance implementation class person.java

public class Person {

	private String name;
	private Integer age;
	private String nickName;	
	
	public String getNickName() {
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
	public Person(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}

	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}
	

}

To configure xml file

<!--  Package scanning 、 Just mark @Controller、@Service、@Repository,@Component -->
    <!--<context:component-scan base-package="com.atguigu" use-default-filters="false"></context:component-scan>-->

    <bean id="person" class="com.dmsdbj.bean.Person"  scope="prototype" >
		<property name="age" value="${}"></property>
		<property name="name" value="zhangsan"></property>
	</bean>

In profile , adopt <bean> Elemental id Property specifies the bean Unique name of ,class Property specifies the bean Implementation class of , Can be interpreted as Spring The container is through this implementation class new One. Bean. adopt <property> Labeled name and age Field assignment . This will be person Class registered to IOC In the container , If other classes need to use person.java, After introduction ,IOC The container automatically creates objects for it .

Test class

// obtain xml The way , return IOC Containers 
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
// from IOC Get... In the container Person Type of ban, And then operate 
Person bean=(Person)applicationContext.getBean("person");
System.out.println(bean);

be based on java To configure

Configuration class

// Configuration class == The configuration file 
@Configuration  // tell Spring This is a configuration class 
@ComponentScans(value="com.dmsdbj")   // Package scanning  value: Specify packages to scan 

public class MainConfig {
	
	// Register a Bean; Type of return value ,id The default is to use the method name as the id
	@Bean("person")
	public Person person01(){
		return new Person("lisi", 20);
	}

}

Configuration classes are equivalent to configuration files , Use... In configuration classes @Configuration&@Bean Register components in container , Use @ComponentScan- Auto scan components & Specify scanning rules

Test class

// Get configuration class , return IOC Containers 
ApplicationContext applicationContext=new AnnotationConfigApplicationContext(MainConfig.class);
// from IOC Get... In the container Person Type of bean, And then operate 
Person bean=applicationContext.getBean(Person.class);
System.out.println(bean);

Automatic assembly

Spring Leveraging dependency injection (DI), Finish right IOC Dependency assignment of components in the container ;

@Autowire Automatic injection :

yes spring Annotations , By default, the property must be assigned well , If you don't, you'll make a mistake ; have access to @Autowired(required=false) Turn off default assignment ;

The default priority is to find the corresponding component in the container according to the type :applicationContext.getBean(BookDao.class); Assign when found ;

If multiple components of the same type are found , Then the name of the property is used as the component's id Go to the container to find ,applicationContext.getBean("bookDao");

@Qualifier("bookDao"): Use @Qualifier Specifies the... Of the component to be assembled id, Instead of using property names ;

By default, auto assembly must assign a good attribute , If you don't, you'll make a mistake ; have access to @Autowired(required=false);

@Primary: Give Way Spring During automatic assembly , Default to preferred bean; You can also continue to use @Qualifier Specify the bean Name

@Resource Automatic injection :

yes java Normative notes , You can talk to @Autowired The same function of automatic assembly ; But there is no support for @Primary function , There was no support for @Autowired(reqiured=false) The function of

@Inject Automatic injection :

yes java Normative notes , Import required javax.inject My bag , and Autowired Same function , But no support no support @Autowired(reqiured=false) The function of

 

There's something wrong , Thank you for correcting ..

版权声明
本文为[Wooden pine cat]所创,转载请带上原文链接,感谢

Tags spring ioc
Scroll to Top