Home > Back-end >  The javaEe into
The javaEe into

Time:10-13

Introduction: write your own an interface and an implementation class, an instance of this class USES the Spring framework

CodePudding user response:


 package com. The add. Config. 

The import org.springframework.context.annotation.Com ponentScan;

@ ComponentScan (value="https://bbs.csdn.net/topics/com.add")
Public class AddConfig
{

}


 package com. The add. Impl; 

The import org.springframework.stereotype.Com ponent;

The import com. The add. Pojo. Adder;
The import com. The add. Service. The AddService;

@ Component
Public class AddImpl implements the AddService
{

@ Override
Public int the sum (Adder a, Adder b)
{
The return of al-qeada etE () + b.g etE ();
}

}


 package com. The add. Pojo; 

The import org.springframework.stereotype.Com ponent;

@ Component
Public class Adder
{
Private int e;

Public int getE ()
{
return e;
}

Public void setE (int) e
{
This. E=e;
}

}


 package com. The add. Service; 

The import com. The add. Pojo. Adder;

Public interface the AddService
{
Public int the sum (Adder a, Adder b);
}


 package com. The add. Main; 

The import org. Springframework. Context. ApplicationContext;
The import org. Springframework. Context. The annotation. AnnotationConfigApplicationContext;

The import com. The add. Config. AddConfig;
The import com. The add. Impl. AddImpl;
The import com. The add. Pojo. Adder;
The import com. The add. Service. The AddService;

Public class AddMain
{

Public static void main (String [] args)
{
ApplicationContext CTX=new AnnotationConfigApplicationContext (AddConfig. Class);
The AddService the AddService=CTX. GetBean (AddImpl. Class);

Adder a=new Adder ();
Adder b=new Adder ();

A.s etE (6);
B.s etE (5);

System. The out. Println (the addService. Sum (a, b));//11
(CTX) (AnnotationConfigApplicationContext). The close ();
}

}
  • Related