Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Spring to implement dependency injection in Android

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to use Spring to achieve dependency injection in Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Manually assemble dependent objects

Manual assembly of dependent objects, in which there are two more programming methods

In the xml configuration file, by configuring under the bean node

Assemble using @ Autowired or @ Resource annotations in java code

Dependency injection-manual assembly-XML mode

Injecting dependencies through setter method

Of the element

< property >

Child elements indicate that their set methods are used to inject. You can inject anything from basic types to collection classes, even the bean of an application system.

Injecting dependencies through setter method

Simple bean configuration

Configure bean's simple properties, basic data types, and String.

Injecting dependencies through setter method

Quote other bean

Internal bean

The disadvantage of this approach is that you can't reuse this personClass instance anywhere else because it is specifically for personService.

Assembly set

If the property of bean is a collection type, it should be handled as follows:

A, assembly List and array:

List1 list2 obj1 obj2

B. Assemble set:

Set1 set2

Set is used in the same way as list, except that objects are assembled into set, while list is assembled into List or arrays.

Assembly set

C, Assembly map:

Map01 map02

The values in map are the same as those in and, which can make any valid attribute element, it is important to note that the key value must be String.

D, Assembly Properties:

Prop1 prop2

E. Set null:

Through the order of parameters:

Zhang San 56

Injecting dependencies through constructors

56 Zhang San

Dependency injection-manual assembly-annotation mode

The prerequisite for assembling using @ Autowired or @ Resource annotations in java code is.

1. To introduce the context namespace, you need to configure the following information in the xml configuration file:

2. Add the context:annotation-config tag in the configuration file

This configuration implicitly registers multiple processors that parse comments

AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor

Note: @ Resource notes lib\ j2ee\ common-annotations.jar in the spring installation directory

Assemble by using @ Autowired or @ Resource annotations in java code. The difference between the two annotations is: @ Autowired assembles by type by default, @ Resource assembles by name by default, and assembles by type only when a bean that matches the name is not found.

@ Autowired

PrivatePersonDao personDao;// is used on fields

@ Autowired

Publicvoid setPersonDao (PersonDaopersonDao) {/ / this.personDao = personDao;} on the set method for the property

The @ Autowired annotation assembles dependent objects by type. By default, it requires that dependent objects must exist. If null values are allowed, you can set its required property to false.

@ Autowired (required=false) privatePersonDao personDao;// is used on the field @ Autowired (request=false) public voidsetPersonDao (PersonDaopersonDao) {/ / on the set method of the attribute this.personDao = personDao;}

If we want to use assembling by name, we can use it with the @ Qualifier annotation. As follows:

@ Autowired@Qualifier ("personDao") privatePersonDao personDao;// is used on the field @ Autowired publicvoidsetPersonDao (@ Qualifier ("personDao") PersonDao personDao) {/ / on the set method of the property this.personDao= personDao;}

The @ Qualifier annotation can also be specified as a parameter to a constructor or a method:

The @ Resource annotation, like @ Autowired, can also be annotated on the setter method of a field or property.

@ Resource annotations are assembled by name by default.

The name can be specified through the name attribute of @ Resource, if no name attribute is specified

When the annotation is marked on the field, the name of the field is taken as the bean name by default to find the dependent object.

When the annotation is annotated on the setter method of the property, the attribute name is taken as the bean name by default to find the dependent object.

@ Resource (name= "personDao") privatePersonDaopersonDao;// is used on the field @ Resource (name= "personDao") publicvoidsetPersonDao (PersonDaopersonDao) {/ / on the set method for the property this.personDao = personDao;}

The latter is equivalent to the xml configuration file

Note: if the name attribute is not specified and the dependent object cannot be found by default, the @ Resource annotation falls back to assembling by type. But once the name attribute is specified, it can only be assembled by name.

two。 Automatic assembly of dependent objects

For automatic assembly, we can understand it, it is not recommended that you use it. Example:

The value of the autowire attribute is as follows

ByType: assemble by type, and you can find a bean that matches that type in the container based on the type of the attribute. If more than one is found, an exception will be thrown. If it is not found, the property value is null.

ByName: assemble by name. You can look for the same bean in the container according to the name of the attribute. If it is not found, the value of the attribute is null.

Constructor is similar to byType, except that it applies to constructor parameters. If no bean is found in the container that matches the constructor parameter type, an exception will be thrown.

Autodetect: first try to use constructor for automatic assembly, and then use the byType method. The treatment of uncertainty is consistent with constructor and byType.

Manage components into the spring container by automatically scanning the classpath

In the previous examples, we used the bean definition of XML to configure the component. In a larger project, there are usually hundreds of components, and if these components are configured using the bean definition of xml, it will obviously increase the size of the configuration file, and it is not easy to find and maintain.

Spring2.5 introduces an automatic component scanning mechanism that looks for classes annotated with @ Component, @ Service, @ Controller, and @ Repository annotations under the classpath and manages these classes in a spring container. It serves the same purpose as configuring components using bean nodes in a xml file.

To use the automatic scanning mechanism, we need to open the following configuration information:

1. To introduce the context namespace, you need to configure the following information in the xml configuration file:

2. Add the context:component-scan tag in the configuration file

Where base-package is the package to be scanned (including subpackages).

Note:

1. When scanning elements with components, AutowiredAnnotationBeanPostProcessor and CommonAnnotationBeanPostProcessor are implicitly included. That is, even components are automatically detected and woven-all of this does not require any bean configuration metadata to be provided in XML.

2. Function introduction

@ Service is used to label business layer components,

@ Controller is used to label control layer components (such as action in struts),

@ Repository is used to annotate the data access component, the DAO component.

@ Component generally refers to components, and we can use this annotation to mark components when they are not easy to classify.

/ / Dao layer importorg.springframework.stereotype.Repository; importcom.test.dao.PersonDao; @ Repository ("personDao") publicclassPersonDaoBean implements PersonDao {} / / Business layer importjavax.annotation.Resource; importorg.springframework.stereotype.Service; importcom.test.dao.PersonDao; importcom.test.service.PersonService; @ Service ("personService") publicclassPersonServiceBean implements PersonService {@ Resource (name= "personDao") privatePersonDao personDao } this is the end of "how to implement dependency injection with Spring in Android". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report