Spring-03 dependency injection (DI)
Dependency injection (DI)
- Dependency Injection (DI).
- Dependency: the creation of a Bean object depends on the container, and the Bean object depends on resources.
- Injection: refers to the resources that the Bean object depends on, which are set and assembled by the container.
1. Constructor injection
This is the case in this blog post Spring-02
2.Set mode injection
2.1 entity class
public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
public class DataTest { private String name; private Address address; private String[] books; private List<String> hobbys; private Map<String,String> card; private Set<String> games; private String wife; private Properties info; public void setName(String name) { this.name = name; } public void setAddress(Address address) { this.address = address; } public void setBooks(String[] books) { this.books = books; } public void setHobbys(List<String> hobbys) { this.hobbys = hobbys; } public void setCard(Map<String, String> card) { this.card = card; } public void setGames(Set<String> games) { this.games = games; } public void setWife(String wife) { this.wife = wife; } public void setInfo(Properties info) { this.info = info; } public void show(){ System.out.println("name="+ name + ",address="+ address.getAddress() + ",books=" ); for (String book:books){ System.out.print("<<"+book+">>\t"); } System.out.println("\n hobby:"+hobbys); System.out.println("card:"+card); System.out.println("games:"+games); System.out.println("wife:"+wife); System.out.println("info:"+info); } }
2.2 constant injection
<bean id="DataTest" class="pojo.DataTest"> <property name="name" value="zc"></property> </bean>
2.3 Bean injection
<bean id="address" class="pojo.Address"> <property name="address" value="Henan"/> </bean> <bean id="DataTest" class="pojo.DataTest"> <property name="address" ref="address"/> </bean>
2.4 array injection
<bean id="DataTest" class="pojo.DataTest"> <property name="books"> <array> <value>Journey to the West</value> <value>Trisomy</value> <value>The old man and the sea</value> </array> </property> </bean>
2.5 injection
<bean id="DataTest" class="pojo.DataTest"> <property name="hobbys"> <list> <value>listen to the music</value> <value>watch movie</value> <value>Mountain climbing</value> </list> </property> </bean>
2.6 Map injection
<bean id="DataTest" class="pojo.DataTest"> <property name="card"> <map> <entry key="China Post" value="1000"/> <entry key="build" value="2000"/> </map> </property> </bean>
2.7 set injection
<bean id="DataTest" class="pojo.DataTest"> <property name="games"> <set> <value>AOA</value> <value>BOB</value> <value>COC</value> </set> </property> </bean>
2.8 Null injection
<bean id="DataTest" class="pojo.DataTest"> <property name="wife" > <null/> </property> </bean>
2.9 Properties injection
<bean id="DataTest" class="pojo.DataTest"> <property name="info"> <props> <prop key="driver">123456</prop> <prop key="url">123456</prop> <prop key="username">123465</prop> <prop key="password">123456</prop> </props> </property> </bean>
2.10 testing
@Test public void Test(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); DataTest dataTest = (DataTest) context.getBean("DataTest"); dataTest.show(); }

3. Expansion mode injection
3.1 P namespace injection
Add constraint file to header file: in the header of configuration file
xmlns:p="http://www.springframework.org/schema/p"
injection
<bean id="user" class="pojo.User" p:name="zc" p:age="20"/>
3.2 C namespace injection
Add constraint file to header file: in the header of configuration file
xmlns:c="http://www.springframework.org/schema/c"
injection
<bean id="user" class="pojo.User" c:name="zc" c:age="20"/>
At this time, it will burst red because there is a lack of parameter constructors. This method is also called constructor injection
3.3 testing
@Test public void Test1(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); User user = (User) context.getBean("user"); System.out.println(user); }

Scope of Bean
Scope | describe |
---|---|
singleton | There is only one bean instance in the spring IoC container. Beans exist in a singleton mode, and the default value of the bean scope range. |
prototype | Every time you call Bean from the container, you return a new instance, which is middle note execution of newXxxBean() when you call getBean(). |
request | Each HTTP request will create a new Bean, and this scope is only applicable to the Spring WebApplicationContext environment of the web. |
session | The same HTTP Session shares a Bean, and different sessions use different beans. This scope is only applicable to the Spring WebApplicationContext environment of the web. |
application | Limit the scope of a Bean to the life cycle of ServletContext. This scope is only applicable to the Spring WebApplicationContext environment of the web. |
Among the several scopes, the request and session scopes are only used in web-based applications, and can only be used in Web-based Spring ApplicationContext environment.
1.Singleton
When the scope of a bean is singleton, there will only be one shared bean instance in the Spring IoC container, and all requests for a bean will only return the same instance of the bean as long as the id matches the bean definition. Singleton is a singleton type, which automatically creates a bean object when creating a container. It exists whether you use it or not, and the object obtained each time is the same object. Note that the singleton scope is the default scope in Spring. To define a bean as a singleton in XML, you can configure it as follows:
<bean id="ServiceImpl" class="cn.csdn.service.ServiceImpl" scope="singleton">
2.Prototype
When the scope of a bean is prototype, it means that a bean definition corresponds to multiple object instances. A prototype scoped bean will cause a new bean instance to be created every time a request is made to the bean (inject it into another bean, or call the container's getBean() method programmatically). Prototype is a prototype type. It is not instantiated when we create the container. Instead, we create an object when we get the bean, and the object we get each time is not the same object. As a rule of thumb, you should use the prototype scope for stateful beans and the singleton scope for stateless beans. Define a bean as a prototype in XML, which can be configured as follows:
<bean id="account" class="com.foo.DefaultAccount" scope="prototype"/> perhaps <bean id="account" class="com.foo.DefaultAccount" singleton="false"/>
3.Request
When the scope of a bean is Request, it means that in an HTTP Request, a bean definition corresponds to an instance; That is, each HTTP Request will have its own bean instance, which is created according to a bean definition. This scope is only valid in the case of web-based Spring ApplicationContext. Consider the following bean definitions:
<bean id="loginAction" class=cn.csdn.LoginAction" scope="request"/>
For each HTTP request, the Spring container will create a new loginaction bean instance according to the loginaction bean definition, and the loginaction bean instance is only valid in the current HTTP request. Therefore, you can rest assured to change the internal state of the created instance according to the needs, while the instances created according to the loginaction bean definition in other requests will not see these state changes specific to a request. When the processing of the request ends, the bean instance of the request scope will be destroyed.
4.Session
When the scope of a bean is Session, it means that in an HTTP Session, a bean definition corresponds to an instance. This scope is only valid in the case of web-based Spring ApplicationContext. Consider the following bean definitions:
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
For an HTTP Session, the Spring container will create a new userPreferences bean instance according to the userPreferences bean definition, and the userPreferences bean is only valid in the current HTTP Session. Like the request scope, you can safely change the internal state of the created instance as needed. However, for instances created according to userPreferences in other HTTP sessions, you will not see these state changes specific to an HTTP Session. When the HTTP Session is finally discarded, the beans within the scope of the HTTP Session will also be discarded.
Personal blog is:
MoYu's Github Blog
MoYu's Gitee Blog