<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.9</version> </dependency>
public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("cn.com"); Pojo bean = context.getBean(Pojo.class); System.out.println(bean.getName()); }
package cn.com;
@Configuration public class Config { @Bean public Pojo getPojo() { return new Pojo(); } }
公共封装集成到spring-boot方式:
src/main/resources/META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.com.config.Config(配置类)
项目打成jar通过maven引入到spring-boot项目的依赖,就可以实现自动被spring管理