首页 > 其他分享 >spring

spring

时间:2022-10-03 22:12:51浏览次数:51  
标签:xml ApplicationContext spring ctx 注解 new class

依赖查找:


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

相关文章

  • 【Spring boot】自动配置的开启原理
    本文结论源码使用springboot2.6.6版本开始自动配置的核心注解:@EnableAutoConfiguration@EnableAutoConfiguration中使用了@Import(AutoConfigurationImportSelector.c......
  • Spring Cloud入门
    简介微服务是由springboot开发的一个个的模块,是一个个独立的进程springcloud是微服务全家桶springcloud通过网关调用多个负载均衡(微服务)springboot是以数字作为版本,spring......
  • 2022-10-03-SpringMVC执行流程梳理及结合源码断点调试过程源码分析
    SpringMVC执行流程梳理接口方式控制器实现流程分析控制器层代码实现控制器配置SpringMVC.xml配置文件客户端浏览器发起请求,按回车前端控制器拦截所有请求/......
  • Spring Cloud:第二章:eureka服务发现
    服务注册中心:eureka-server新建一个springboot项目:eureka-server,其pom.xml配置如下:<properties><project.build.sourceEncoding>UTF-8</project.build.sou......
  • Spring Cloud:第一章:基本介绍
    SpringCloud中文网SpringCloud是什么?SpringCloud是一系列框架的有序集合,将市面上开发得比较好的模块集成进去,进行封装,从而减少了各模块的开发成本。SpringCloud......
  • SpringBoot访问Clickhouse执行时报错:org.springframework.beans.factory.UnsatisfiedD
    1依赖信息<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"......
  • Springboot笔记
    SpringBootHelloWorld1.创建Meven工程2.引入依赖pom.xml<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</a......
  • Spring学习第一步 配置环境
    springspring配置:创建spring工程在pom.xml导入spring依赖和单元测试依赖创建pojo包--->在里面创建实体类创建applicationContext配置文件---->写bean相关......
  • springmvc-servlet.xml
    <?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"......
  • Spring-Cloud-Alibaba项目构建
    SpringCloudAlibaba项目构建版本说明由于我之前的eblog单机版本的springboot版本为2.1.4,所以之后的所有的项目都以该项目版本为基础。我们打开spring官网里面的spri......