Introduction to Spring: IOC, AOP, and common annotations

Source: Internet
Author: User

POJO: Simple plain old-fashioned Java object

1. Dependency Injection di

Any application consists of more than two classes that work together to accomplish specific task logic. Traditionally , each object is responsible for managing references to objects that it collaborates with (the objects on which it depends), but this results in a high degree of coupling .

Writing code should be high cohesion, low-coupling, and coupling can be difficult to reuse and understand.

Dependency Injection Di, also called control inversion, refers to the creation and management of third-party components that are responsible for the objects by spring. objects do not need to create a reference to manage their dependencies, and dependencies are automatically injected into the objects they need .

1.1 Three ways to rely on injection
    • Constructor injection constructor Injection: the example BraveKnight does not have a coupling to a particular quest implementation, and the interface is used to implement the dependency, which can be replaced with a different implementation.
 Public classBraveknightImplementsknight{PrivateQuest Quest; Public Braveknight(Quest Quest) { This.Quest=quest; } Public void embarkonquest() {Quest.Embark; }}@Configuration//define Bean Assembly class Public classknightconfig{The //method name is the name of the bean by default, and the method return value is the Bean object    @bean     PublicKnightKnight(){return New Braveknight(Quest()); }@bean     PublicQuestQuest(){return New slaydragonquest(System. out); }}
    • @AutowiredInterface injection: The injected class must have a related comment
    • SetterMethod injection;
1.2 Assembly

the behavior of creating collaboration between app components is called Assembly . The easiest way to assemble is to automatically scan the package: after spring MVC configures the tag, spring automatically scans the base-package Java files of its sub-packages and @Component @[email protected] registers them as beans if they are scanned for classes that have such annotations:

<context:component-scan base-package="package.name" annotation-config="true"/>

It's also a way to use Java configuration:

@Configuration//定义bean装配类publicclass KnightConfig{        //方法名默认就是Bean的名称,该方法返回值就是Bean的对象    @bean    publicknight(){        returnnewBraveKnight(quest());    }        @bean    publicquest(){        returnnewSlayDragonQuest(System.out);    }}
2. Programming AOP for Facets

DI (IOC) is loosely coupled, and aspect-oriented programming separates the applications from the various functions to form reusable components.

We always want each class to focus on its core logic, but there is always a need to use the services of the log subclass-these system services are often referred to as crosscutting concerns because they span multiple components of the system.

2.1 How pointcuts are configured
    1. The first step is to declare the crosscutting concern class as a bean, or to use it directly @Aspect ;
    2. Create cross-sectional;

Tangent expression

    • A pointcut expression indicates that a * com.sif.bnea.Singer.perform(..) notification call is triggered when any overload of the method, including parameters and return values, is performed.

    • Adding &&within(packageName.*) a pointcut that represents a configuration behind an expression only matches the PackageName package.

    • Bean (XXX)

Example of creating a cross plane:

@Aspect//Identifies the current class as a slice Public classaudience{//define tangent points and their predecessor actions    @Before("Execution (* * package. Perform.perform (..)) ") Public void beforeaction(){Sout("hahaha"); }@AfterReturning("Execution (* * package. Perform.perform (..)) ") Public void  After(){Sout("Wondful"); }@AfterThrowing("Execution (* * package. Perform.perform (..)) ") Public voidAfterDemandrefund(){Sout("Bad"); }@Around}
3. Common annotations
  • @Controller
  • @RequestMapping(value = "/simple",method = RequestMethod.POST): The mapped path and the type of request mapped to;
  • @Configuration @bean: Assembly Configuration Method
  • @Autowired: Defined on the interface to be injected (but the interface @Service is not annotated, the class implementing the interface needs to be @Service commented;
  • @RequestParam(name="nameid") String id: It is also an injection, which aligns the form submission parameter names with the method parameters;
  • @RequestBody: resolves the body portion of the request requests to the object specified by the parameter, and uses the httpmessageconverter of the system's default configuration to parse the data;
  • @Responsebody: Returns the result of the method to the corresponding HTTP response of the request without being resolved to a jump path;
  • @PathVariable: If @RequestMapping("/users/{username}") A variable is used, the method can be used String userProfile(@PathVariable("username") String username) ;
  • @TransactionalCan only be applied to the public method to be valid;
  • @Scheduled(cron = "0/1 * * * * ?"): Realization of timed tasks;
  • @Entity @Table: @Entity Note named this is an entity bean, @Table Note Specifies the entity to map with the database table, where @table.name () is used to specify the table name of the mapping table. If the default @table comment, the system defaults to the table name of the map table;
  • @Aspect: The current class is identified as a slice: @Befroe @AfterReturn @AfterThrowing @Around ;

Introduction to Spring: IOC, AOP, and common annotations

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.