Automatic scanning of packages (AutoScan)
When the YMP framework is initialized, it will automatically scan all class files declared with @Bean annotations under the package path configured by the autoscan_packages parameter. First, it will analyze all implemented interfaces of the loaded classes and register them in the Bean container, and then execute Dependency injection of class members and binding of method interception agents;
Note: When multiple implementation classes of the same interface are registered to the bean container at the same time, the implementation class obtained through the interface will be The one that was last registered to the container can only be obtained correctly through the instance object type;
Example 1:
// 业务接口 public interface IDemo { String sayHi(); } // 业务接口实现类,单例模式 @Bean public class DemoBean implements IDemo { public String sayHi() { return "Hello, YMP!"; } }
Example 2:
// 示例一中的业务接口实现类,非单例模式 @Bean(singleton = false) public class DemoBean implements IDemo { public String sayHi() { return "Hello, YMP!"; } }
- ##Test code:
public static void main(String[] args) throws Exception { YMP.get().init(); try { // 1. 通过接口获取实例对象 IDemo _demo = YMP.get().getBean(IDemo.class); System.out.println(_demo.sayHi()); // 2. 直接获取实例对象 _demo = YMP.get().getBean(DemoBean.class); System.out.println(_demo.sayHi()); } finally { YMP.get().destroy(); } }