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);