依赖查找:
BeanFactory beanFactory = new ClassPathXmlApplicationContext("basic_di/inject-set.xml");
Person person = beanFactory.getBean(Person.class);
根据type查找一组对象:
ApplicationContext ctx = new ClassPathXmlApplicationContext("basic_dl/quickstart-oftype.xml");
Map<String, DemoDao> beans = ctx.getBeansOfType(DemoDao.class);
beans.forEach((beanName, bean) -> {
System.out.println(beanName + " : " + bean.toString());
});
根据注解查找:
ApplicationContext 中有一个方法叫 getBeansWithAnnotation ,它可以传入一个注解的 class ,返回所有被这个注解标注的 bean 。
ApplicationContext ctx = new ClassPathXmlApplicationContext("basic_dl/quickstart-withanno.xml");
Map<String, Object> beans = ctx.getBeansWithAnnotation(Color.class);
beans.forEach((beanName, bean) -> {
System.out.println(beanName + " : " + bean.toString());
});
获取IOC容器中的所有Bean:ApplicationContext的getBeanDefinitionNames()方法
ApplicationContext ctx = new ClassPathXmlApplicationContext("basic_dl/quickstart-withanno.xml");
String[] beanNames = ctx.getBeanDefinitionNames();
// 利用jdk8的Stream快速编写打印方法
Stream.of(beanNames).forEach(System.out::println);
查找是否有某个bean:ApplicationContext.containsBean()
延迟查找:ApplicationContext.getBeanProvider()
ObjectProvider<Dog> dogProvider = ctx.getBeanProvider(Dog.class);
Dog dog = dogProvider.getIfAvailable();
if (dog == null) {
dog = new Dog();
}
//或者
Dog dog = dogProvider.getIfAvailable(() -> new Dog());
依赖注入: property注入, value直接赋值,ref引用
<bean id="person" class="com.linkedbear.spring.basic_di.a_quickstart_set.bean.Person">
<property name="name" value="test-person-byset"/>
<property name="age" value="18"/>
</bean>
<bean id="cat" class="com.linkedbear.spring.basic_di.a_quickstart_set.bean.Cat">
<property name="name" value="test-cat"/>
<!-- ref引用上面的person对象 -->
<property name="master" ref="person"/>
</bean>
注解驱动:AnnotationConfigApplicationContext
@Bean(name = "aaa") // 4.3.3之后可以直接写value,name就是id,不写的话是方法名作为id
public Person person() {
return new Person();
}
@Component("aaa") //模式注解,不指定的话id就是类名小写
public class Person { }
ApplicationContext ctx = new AnnotationConfigApplicationContext(QuickstartConfiguration.class); //可以传入配置类,也可以传入根包
Person person = ctx.getBean(Person.class);
System.out.println(person);
@ComponentScan:搭配@Configuration一起用,扫描指定路径包及子包下的所有 @Component 组件;如果不指定扫描路径,则默认扫描本类所在包及子包下的所有 @Component 组件。
AnnotationConfigApplicationContext 的构造方法中有一个类型为 String 可变参数的构造方法:可以扫描对应包下的@component组件
ApplicationContext ctx = new AnnotationConfigApplicationContext("com.linkedbear.spring.annotation.c_scan.bean");
xml中启用组件扫描:
<context:component-scan base-package="com.linkedbear.spring.annotation.c_scan.bean"/>
@Configuration也是@Component
xml中引入注解:在 xml 中要引入注解配置,需要开启注解配置,同时注册对应的配置类
<context:annotation-config />
<bean class="com.linkedbear.spring.annotation.d_importxml.config.AnnotationConfigConfiguration"/>
注解引入xml:在注解配置中引入 xml ,需要在配置类上标注 @ImportResource 注解,并声明配置文件的路径:
@Configuration
@ImportResource("classpath:annotation/beans.xml")
public class ImportXmlAnnotationConfiguration {
}
标签:xml,ApplicationContext,spring,ctx,注解,new,class
From: https://www.cnblogs.com/sjj123/p/16751409.html