Spring Basic Knowledge Dependency Injection

Source: Internet
Author: User

Four principles of the spring framework:

1) Use Pojo for lightweight and minimal intrusive development.

2) loose coupling is achieved through dependency injection and interface-based programming.

3) declarative programming through AOP and default habits.

4) Use AOP and Templates (template) to reduce pattern code.

1.1 Dependency Injection:

Control inversion (inversion of control-ioc) and Dependency injection (dependency injection-di) are the same concept in a spring environment.

Control reversals are implemented through dependency injection. The so-called inversion of control refers to a container that is responsible for creating objects and maintaining dependencies between objects.

The primary purpose of dependency injection is to decouple, and there are generally two ways to add a feature: Inherit the parent class, and combine another class that has that functionality.

The coupling degree is lower for the inherited parent class than for the combined phase.

  

The SPRINGIOC container (ApplicationContext) is responsible for creating the bean and injecting the functional class bean into the desired bean through the container. Spring implements IOC (DI) through XML, annotations, and Java configuration.

XML, annotations, and Java configuration are all configuration metadata.

Metadata: The data used to describe the data.

Metadata does not have any executable capability, only the metadata can be parsed and given specific functionality through outside code. The spring container parses these configuration metadata for the bean's initialization, configuration, and management dependencies.

  

To declare a bean's annotations:

@Component: no clear role

@Service: Using in the business logic layer

@Repository: Used in the Data Access Layer (DAO)

@Controller: Using in the MVC layer

Note: The above four annotation functions are the same, but different performance is used for business differentiation.

Notes injected into the bean:

@Autowired: Annotations provided by spring

@Inject: Annotations provided by JSR-330

@Resource: Annotations provided by JSR-330

The above three annotations are generally available on set methods or properties, but general idioms are used on attributes.

Note-based bean initialization and dependency injection, the spring container chooses Annotationconfigapplicationcontext.

Function Class Bean:

@Service  Public class  Funservice {    public  string SayHello (string word) {        return "Hello" + Word + " !" ;    }}

Note: @Service declares that the current Funservice class is a spring-managed bean that uses @component, @Service, @Repository, @Controller equivalent.

Beans using the Feature class:

@Service  Public class Usefunservice {    @Autowired    private  funservice funservice;      Public string SayHello (string word) {        return  Funservice.sayhello (Word);}    }

Note: @Service declares that the current class is a spring-managed bean

@Autowired inject the Funservice entity into the usefunservice so that it can use the methods in it.

Configuration class:

@ComponentScan ("Learn.learn.spring.ioc") @Configurationpublicclass  diconfig {} 

Note: @ComponentScan, automatically scans all classes under the specified package that use @component, @Controller, @Service, @Repository, and registers as beans.

Run:

 Public classApp { Public Static voidMain (string[] args) {Annotationconfigapplicationcontext context=NewAnnotationconfigapplicationcontext (Diconfig.class);//1Usefunservice Usefunservice=Context.getbean (usefunservice.class);//2System.out.println (Usefunservice.sayhello ("Di"));    Context.close (); }}

Note: 1) Use Annotationconfigapplicationcontext as a container for spring and accept a configuration class as a parameter.

2) Gets the Usefunservice bean that declares the configuration.

Results:

1.2java Configuration:

Java configuration is the recommended configuration for spring4.x, which can completely replace the XML configuration, and the Java configuration is also recommended in the Springboot configuration method.

The Java configuration is implemented through @configuration and @bean.

@configuration declares that the current class is a configuration class, which is equivalent to a spring-configured XML file.

@Bean annotations on a method, declares that the current method return value is a bean.

Note: The actual work of the Java configuration is often the same way as the XML implementation, mainly for the database configuration and other common data configuration, of course, you can also be based on business needs to do the configuration you need.

General configuration principles: Global configuration Using Java configuration (such as database, MVC, etc.); business beans use annotation configurations (@Component, @Controller, @Service, @Repository).

  

Example:

Bean for function class:

 Public class  Functionservice {    public  string Say (string word) {        return "Hello" + Word + " !" ;    }}

Note: The @service declaration bean is not used here

Using the Feature class Bean:

 // 1  public  class   Usefunctionservice { // 2   Funservice Funservice;  public  void   set (Funservice funservice) { this .    Funservice = Funservice;  public   string say (string word) {return   Funservice.sayhello (word); }}

Note: 1: The @service declaration bean is not used here

2: The bean is not injected here using @autowired annotations

Configuration class:

  @Configuration   1  public  class    Diconfig {@Bean  public   Functionservice Functionservice () { return  new   Functionservice (); @Bean  public   Usefunctionservice Usefunctionservice () { return  new     Span style= "COLOR: #000000" > Usefunctionservice (); }}

Note: 1: @Configuration indicates that this class is a spring configuration class, similar to an XML file.

Run:

 Public classApp { Public Static voidMain (string[] args) {Annotationconfigapplicationcontext context=NewAnnotationconfigapplicationcontext (Diconfig.class);//1Usefunctionservice Usefunctionservice=Context.getbean (usefunctionservice.class); System.out.println (Usefunctionservice.say ("Di"));    Context.close (); }}

Results:

  

Spring Basic Knowledge Dependency Injection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.