Spring-learning-1

Spring-1

一、定义Bean的几种方式

Bean是什么:JavaBean、SpringBean,Bean是一种对象,Bean的对象具有set,get,toString等方法

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class User {
    private String name;
}
  • xml
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
User user = applicationContext.getBean("user", User.class);
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="user" class="com.example.springbean.bean.User">
        <constructor-arg name="name" value="yin"/>
    </bean>
    
</beans>
  • @Bean

    方法的名字就是bean的名字

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanF.class);
User user = applicationContext.getBean("user", User.class);
public class BeanF {
    @Bean
    public User user(){
        return new User();
    }
}
  • @Component

    ​ @ComponentScan扫描被@Component标注的类,生成Bean

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentScanConfig.class);
User user = applicationContext.getBean("user", User.class);
@Component
public class User {
    private String name;
}
@ComponentScan("com.example.springbean.bean")
public class ComponentScanConfig {
}
  • BeanDefinition

    ​ 编程式定义一个Bean,是其他定义Bean的底层逻辑

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
beanDefinition.setBeanClass(User.class);
applicationContext.registerBeanDefinition("user",beanDefinition);
applicationContext.refresh();
User user = applicationContext.getBean("user", User.class);
  • FactoryBean

    ​ 当注册factoryBean的时候,会同时创建两个Bean,&beanName,beanName

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
beanDefinition.setBeanClass(PersonFactoryBean.class);
applicationContext.registerBeanDefinition("person",beanDefinition);
applicationContext.refresh();
PersonFactoryBean _person = applicationContext.getBean("&person", PersonFactoryBean.class);
Person person = applicationContext.getBean("person", Person.class);
System.out.println(_person);
System.out.println(person);
public class PersonFactoryBean implements FactoryBean {
    @Override
    public Object getObject() throws Exception {
        return new Person();
    }

    @Override
    public Class<?> getObjectType() {
        return Person.class;
    }
}
public class Person {
    private String name;
}
  • Supplier

    简单一点的直接注入

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.registerBean("person2",Person.class);
applicationContext.registerBean(Person.class, new Supplier<Person>() {
    @Override
    public Person get() {
        Person person = new Person();
        person.setName("yin");
        return person;
    }
});
applicationContext.refresh();
Person person = applicationContext.getBean("person", Person.class);
Person person2 = applicationContext.getBean("person2", Person.class);
System.out.println(person);
System.out.println(person2);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cofer_Yin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值