57th: Scala Dependency Injection in-depth practical explanation

Source: Internet
Author: User

We're going to learn the next dependency injection. Let's start with the code:

Package Scala.learn

Trait Logger {def log (msg:string)}
Trait Auth {
Auth:logger =
Def Act (msg:string) {
Log (msg)
}
}

Object DI extends Auth with Logger {
Override def log (msg:string) = println (msg)
}

Object test57{
def main (args:array[string]) {
Di.act ("I hope you'll like it")
}
}

First, define a trait, which is the structure type.

Then define another trait, its this pointer alias is auth, and specify mixed logger traits. In this way, the pair that inherits the Auth trait must also be mixed with the logger trait, and must have a string type parameter.

Then, define a singleton pair like Di, which inherits from Auth and mixes the logger trait, so the pair is like a logger type with a string type argument, and it overrides the log method.

In this way, the pair like di can normally invoke the Act method in the Auth method, and the Act method invokes the rewritten log method, which eventually prints the MSG.

Share more of the Scala resources:

Baidu Cloud Disk: http://pan.baidu.com/s/1gd7133t

Micro Cloud Disk: http://share.weiyun.com/047efd6cc76d6c0cb21605cfaa88c416

360 Cloud Disk: Http://yunpan.cn/cQN9gvcKXe26M (extract code: 13CD)

Information from DT Big Data Dream Factory public account: Dt_spark

Follow the account for more information about Scala learning

57th: Scala Dependency Injection in-depth practical explanation

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.