Spring(2):实例化Bean的三种方法
程序员文章站
2022-03-03 12:33:12
...
实例化Bean
一、实现功能
学习使用spring实例化bean,通过构造方法、静态方法和实例方法实例化Bean,同时学习Bean的别名方法
二、准备
1.Pom
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
2.Spirng.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"
default-autowire="byName"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
</beans>
三、通过三种实例化Bean方法以及别名方法
1.构造方法实例化bean
(1)Bean1:需要有构造方法
package com.spring.ioc;
/**
* Created by Administrator on 2019/10/24.
*/
public class Bean1 {
public Bean1() {
System.out.println("Bean1 is ini");
}
}
(2)交由spring管理
<bean class="com.spring.ioc.Bean1" id="bean1"/>
(3)测试函数
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2019/10/24.
*/
public class test {
@Test
public void fortest(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
//通过构造方法实例化bean的测试
Bean1 bean1=context.getBean("bean1",Bean1.class);
System.out.println("bean1 = " + bean1);
}
}
2.静态方法实例化bean
(1)类
-》Bean2
package com.spring.ioc;
/**
* Created by Administrator on 2019/10/24.
*/
public class Bean2 {
public Bean2() {
System.out.println("Bean2 is ini");
}
}
-》Bean2Factory
package com.spring.ioc;
/**
* Created by Administrator on 2019/10/24.
*/
public class Bean2Factory {
public static Bean2 getBean2(){
return new Bean2();
}
}
(2)交给spring
<bean class="com.spring.ioc.Bean2Factory" factory-method="getBean2" id="bean2"/>
(3)测试代码
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2019/10/24.
*/
public class test {
@Test
public void fortest(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
//通过构造方法实例化bean的测试
Bean1 bean1=context.getBean("bean1",Bean1.class);
System.out.println("bean1 = " + bean1);
//通过静态方法实例化bean的测试
// Bean2 bean2=Bean2Factory.getBean2();
Bean2 bean2=context.getBean("bean2",Bean2.class);
System.out.println("bean2 = " + bean2);
}
}
3.实例方法实例化bean3
(1)类
-》Bean3
package com.spring.ioc;
/**
* Created by Administrator on 2019/10/24.
*/
public class Bean3 {
public Bean3() {
System.out.println("Bean3 is ini");
}
}
-》Bean3Factory
package com.spring.ioc;
/**
* Created by Administrator on 2019/10/24.
*/
public class Bean3Factory {
public Bean3 getBean3(){
return new Bean3();
}
}
(2)交由Spirng.xml容器管理
<bean class="com.spring.ioc.Bean3Factory" id="bean3Factory"/>
<bean class="com.spring.ioc.Bean3" id="bean3" factory-bean="bean3Factory"
factory-method="getBean3"/>
(3)测试代码
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2019/10/24.
*/
public class test {
@Test
public void fortest(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
//通过构造方法实例化bean的测试
Bean1 bean1=context.getBean("bean1",Bean1.class);
System.out.println("bean1 = " + bean1);
//通过静态方法实例化bean的测试
// Bean2 bean2=Bean2Factory.getBean2();
Bean2 bean2=context.getBean("bean2",Bean2.class);
System.out.println("bean2 = " + bean2);
//通过实例化方法实例化bean的测试
// Bean3 bean3=Bean3Factory.getBean2();
// Bean3Factory bean3Factory=new Bean3Factory();
// Bean3 bean3=bean3Factory.getBean3();
Bean3 bean3=context.getBean("bean3",Bean3.class);
System.out.println("bean3 = " + bean3);
}
}
4.bean别名
(1)方法一:spirng.xml中使用name属性配置
<bean class="com.spring.ioc.Bean1" name="bean1_1,bean1_2" id="bean1"/>
(2)方法二:spirng.xml中使用alias标签配置:只支持一个别名
<alias name="bean1" alias="bean1_3"/>
(3)测试代码
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2019/10/24.
*/
public class test {
@Test
public void fortest(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
//通过构造方法实例化bean的测试
Bean1 bean1=context.getBean("bean1",Bean1.class);
System.out.println("bean1 = " + bean1);
//通过静态方法实例化bean的测试
// Bean2 bean2=Bean2Factory.getBean2();
Bean2 bean2=context.getBean("bean2",Bean2.class);
System.out.println("bean2 = " + bean2);
//通过实例化方法实例化bean的测试
// Bean3 bean3=Bean3Factory.getBean2();
// Bean3Factory bean3Factory=new Bean3Factory();
// Bean3 bean3=bean3Factory.getBean3();
Bean3 bean3=context.getBean("bean3",Bean3.class);
System.out.println("bean3 = " + bean3);
//测试别名
Bean1 bean1_1=context.getBean("bean1_1",Bean1.class);
System.out.println("bean1_1 = " + bean1_1);
Bean1 bean1_2=context.getBean("bean1_2",Bean1.class);
System.out.println("bean1_2 = " + bean1_2);
Bean1 bean1_3=context.getBean("bean1_3",Bean1.class);
System.out.println("bean1_3 = " + bean1_3);
}
}
推荐阅读
-
02Spring基于xml的IOC配置--实例化Bean的三种方式
-
pygame学习笔记(2):画点的三种方法和动画实例
-
spring初始化bean时执行某些方法完成特定的初始化操作
-
Spring Bean的实例化之属性注入源码剖析过程
-
Spring中几种构造方法实例化的方法
-
Spring实例化Bean的五种方式
-
聊聊spring之bean对象的实例化过程
-
spring配置文件(spring的开发步骤;bean中的scope,init-method,destroy-method;bean的工厂静态方法实例化;工厂动态方法实例化)
-
pygame学习笔记(2):画点的三种方法和动画实例
-
Spring之bean的实例化和依赖注入