创建bean方式,spring创建bean的方式包含:声明式样注入方式和编程式注入方式。分别为:
声明式样注入包含: 1)xml 配置化方式 2)@bean注解注入方式3)@Component方式
编程式注入包含: 4)接口注入方式 5)import 导入方式。
废话不多说,上代码,
第一种:xml
1、xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="org.springframework.northeasttycoon.beans.Hello">
<property name="displayName" value="this is display!"/>
</bean>
</beans>
2、hello实体类
/**
* @author :java.zhao
* @date :2018-09-29 10:33
*/
public class Hello {
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
private String displayName;
public void sayHello() {
System.out.println("dispalyname values is:"+this.displayName);
}
}
3、启动类
/**
* @author :java.zhao
* @description:xml 方式
* @date :2018-09-29 10:10
*/
public class TestHelloService {
public static void main(String[] args) {
// 加载spring容器
ApplicationContext ac1 = new ClassPathXmlApplicationContext("classpath:beanfactory.xml");
Hello hello = (Hello)ac1.getBean("hello");
hello.sayHello();
}
}
第二种:@bean注解注入方式
1、配置类
/**
* @author :java.zhao
* @date :2018-10-09 11:10
*/
public class AppConfig02 {
// 第三种 定义bean
@Bean
public Fox fox(){
return new Fox();
}
}
2、fox 实体类
/**
* @author :java.zhao
* @date :2018-10-04 4:43
*/
public class Fox {
public Fox() {
System.out.println("new Fox()");
}
}
3、测试类
/**
* @author :java.zhao
* @date :2018-10-09 11:09
*/
public class ioc02 {
@Test
public void test01(){
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig02.class);
System.out.println("create fox bean:"+ac.getBean("fox"));
}
}
第三种:@Component 注解方式
1、Fox03类
/**
* @author :java.zhao
* @date :2018-10-04 4:43
*/
@Component
public class Fox03 {
public Fox03() {
System.out.println("new Fox()");
}
}
2、配置类
/**
* @author :java.zhao
* @date :2018-10-09 11:10
*/
@ComponentScan("org.springframework.beandefinition01.bean")
public class AppConfig03 {
}
3、测试类
/**
* @author :tycoon
* @date :2018-10-09 11:15
*/
public class ioc03 {
@Test
public void test01(){
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig03.class);
System.out.println("create fox bean:"+ac.getBean("fox03"));
}
}
第四种:接口注入方式
1、Fox04 实体类
/**
* @author :java.zhao
* @date :2018-10-04 4:43
*/
public class Fox04 {
public Fox04() {
System.out.println("new Fox04()");
}
}
2、测试类
/**
* @author :java.zhao
* @date :2018-10-09 11:15
*/
public class ioc04 {
@Test
public void test01(){
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
ac.refresh();
DefaultListableBeanFactory beanFactory = ac.getDefaultListableBeanFactory();
AbstractBeanDefinition beanDefinition1 = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
beanDefinition1.setBeanClass(Fox04.class);
beanFactory.registerBeanDefinition("fox04", beanDefinition1);
System.out.println("create fox04 bean:"+ac.getBean("fox04"));
}
}
第五种: 实现 ImportBeanDefinitionRegistrar 接口
1、实现 ImportBeanDefinitionRegistrar 接口类。既:Fox05ImportBeanDefinitionRegistrar
/**
* @author :java.zhao
* @date :2018-10-04 6:06
*/
public class Fox05ImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
BeanDefinition beanDefinition = new RootBeanDefinition(Fox05.class);
registry.registerBeanDefinition("fox05",beanDefinition);
}
}
2、fox05实体类
/**
* @author :java.zhao
* @date :2018-10-04 6:07
*/
public class Fox05 {
public Fox05() {
System.out.println("Fox05 create!");
}
}
3、配置类
/**
* @author :tycoon
* @description:TODO
* @date :2021-10-04 4:30
*/
@Import(Fox05ImportBeanDefinitionRegistrar.class)
public class AppConfig05 {
}
4、测试类
/**
* @author :java.zhao
* @date :2018-10-09 11:15
*/
public class ioc05 {
@Test
public void test01(){
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig05.class);
System.out.println("create fox05 bean:"+ac.getBean("fox05"));
}
}