In depth understanding of the use of spring ioc for java

  • 2021-07-03 00:05:20
  • OfStack

Use of spring-ioc

IOC container is used in many frameworks, while it is widely used in spring. At the framework level, many functions use ioc technology. Let's look at the use of ioc.

Register the service with the ioc container Reflecting instances of corresponding types using attribute injection In the case of polymorphism, use the instance of name reflection type

Register the service with the ioc container

@ Bean Register Component
Use @ Bean annotation for type registration. By default, the return value of type bean in your ioc container and all method names named bean are not directly related to your package name. If your interface has multiple implementations, you can use @ Bean ("lind") to declare it when registering.

@Component,@Configuration,Service,Repository注册组件

These annotations are declared on the class, While @ Bean is declared on the method, it should be noted that these annotations 1 generally refer to the implementation of an interface, and add these annotations to the implementation class, for example, a data warehouse interface UserRepository, which can have a variety of data persistence methods, such as SqlUserRepositoryImpl and MongoUserRepositoryImpl, so you need to give them an alias when registering, such as @ Repository ("Sql-UserRepositoryImpl) qlUserRepositoryImpl. The default name is the class name, but note 类名首字母为小写 .


public interface EmailLogService {
 void send(String email, String message);
}

@Component()
public class EmailLogServiceHttpImpl implements EmailLogService {
 private static final Logger logger = LoggerFactory.getLogger(EmailLogServiceHttpImpl.class);

 @Override
 public void send(String email, String message) {
 Assert.notNull(email, "email must not be null!");
 logger.info("send email:{},message:{}", email, message);
 }
}
@Component("email-socket")
public class EmailLogServiceSocketImpl implements EmailLogService {
 private static final Logger logger = LoggerFactory.getLogger(EmailLogServiceSocketImpl.class);

 @Override
 public void send(String email, String message) {
 Assert.notNull(email, "email must not be null!");
 logger.info("send email2:{},message:{}", email, message);
 }
}
//  Look 1 Test code when calling 
 @Resource(name = "email-socket")
 EmailLogService socketEmail;
 @Autowired
 @Qualifier( "emailLogServiceHttpImpl")
 EmailLogService httpEmail;

 @Test
 public void testIoc2() {
 socketEmail.send("ok", "ok");
 }


 @Test
 public void testIoc1() {
 httpEmail.send("ok", "ok");
 }

Using bean Objects in Programs

1. Assemble an bean object using Resource
In passing 别名 When calling bean, you can use the @ Resource annotation to assemble objects

2. Assemble an bean object using @ Autowired
You can also use @ Autowired
@ Qualifier ("emailLogServiceHttpImpl") two annotations to implement the 多态 .

Usage scenario

In scenarios where we have the same behavior but implement it differently, such as version 1 interface and version 2 interface, the get method implementation is different, and this
Both versions should be kept at the same time, so we need to follow the principle of opening and closing, extend a new interface, and refactor the code in business.
Extract two versions of the same method into the base class, maintain their own unique methods, give their bean a name, and at assembly time, pass
The name of bean can be assembled.

Write a pseudo code:


class Api_version1(){
@Autowired 
@Qualifier("print-version1")
PrintService printService;
}

class Api_version2(){
@Autowired 
@Qualifier("print-version2")
PrintService printService;
}

class BasePrintService{}

interface PrintService{}

@Service("print-version1")
class PrintServiceImplVersion1 extends BasePrintService implements PrintService{}

@Service("print-version2")
class PrintServiceImplVersion2 extends BasePrintService implements PrintService{}

Related articles: