系统网站建设公司,wordpress 命令行高亮,有哪些好点的单页网站,wordpress弹出登录功能#xff1a; 定一一个字符串数组#xff0c;每个元素都是一个类的全限定名#xff08;包名类名#xff09;#xff0c;把这些类的实例注册到Spring 容器。 一、定义要注册的类#xff1a;
package cn.edu.tju.service;import org.springframework.context.annotatio…功能 定一一个字符串数组每个元素都是一个类的全限定名包名类名把这些类的实例注册到Spring 容器。 一、定义要注册的类
package cn.edu.tju.service;import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;import java.util.Date;public class DemoService {public String getTime(){return now is new Date().toLocaleString();}
}
package cn.edu.tju.service;public class TestService {
}
二、自定义ImportSelector实现ImportSelector,实现selectImports方法
package cn.edu.tju.config;import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.stereotype.Component;public class MyImportSelector implements ImportSelector {Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {return new String[]{cn.edu.tju.service.DemoService, cn.edu.tju.service.TestService};}
}
selectImports方法返回的就是要注册的类的全限定名。 三、在配置类中导入MyImportSelector
package cn.edu.tju.config;import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;import java.io.IOException;
import java.io.Reader;import static org.apache.ibatis.io.Resources.getResourceAsReader;Configuration
Import(MyImportSelector.class)
public class MyConfig {}
四、在容器中获取通过MyImportSelector注册的bean
package cn.edu.tju;import cn.edu.tju.mapper.StudentMapper;
import cn.edu.tju.service.DemoService;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;SpringBootApplication
public class Start {public static void main(String[] args) {ConfigurableApplicationContext context SpringApplication.run(Start.class, args);String[] beanDefinitionNames context.getBeanDefinitionNames();for(String str: beanDefinitionNames){//System.out.println(str);}DemoService demoService context.getBean(DemoService.class);System.out.println(demoService.getTime());}
}