Spring bean的bean的三种实例化方式
Bean 定义
被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的。bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象。这些 bean 是由用容器提供的配置元数据创建的,例如,已经在先前章节看到的,在 XML 的表单中的 定义。
spring bean的实例化方式一共有三种方式:
1.构造器构造bean
Spring可以使用默认构造器或者有参构造器创建Bean实例
有参构造器
UserService.java
1 2 3 4 5 6 7 8 9 10 11 12 | package com.zk.spring; public class UserService { String name; public UserService(String name) { this .name=name; } public void addUser(){ System.out.println(name); } } |
TestDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.zk.spring; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestDemo { @Test public void test1(){ String xmlpath= "ApplicationContext.xml" ; ApplicationContext ac= new ClassPathXmlApplicationContext(xmlpath); UserService us=(UserService) ac.getBean( "UserService" ); us.addUser(); } } |
ApplicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 | <?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:util= "http://www.springframework.org/schema/util" xsi:schemaLocation=" http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id= "UserService" class = "com.zk.spring.UserService" > <constructor-arg index= "0" value= "Spring" ></constructor-arg> </bean> </beans> |
运行效果图:
有参构造器
UserService.java
1 2 3 4 5 6 7 8 9 10 | package com.zk.spring; public class UserService { public UserService(){ } public void addUser(){ System.out.println( "addUser" ); } } |
TestDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.zk.spring; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestDemo { @Test public void test1(){ String xmlpath= "ApplicationContext.xml" ; ApplicationContext ac= new ClassPathXmlApplicationContext(xmlpath); UserService us=(UserService) ac.getBean( "UserService" ); us.addUser(); } } |
ApplicationContext.xml
1 2 3 4 5 6 7 8 9 | <?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:util= "http://www.springframework.org/schema/util" xsi:schemaLocation=" http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id= "UserService" class = "com.zk.spring.UserService" ></bean> </beans> |
运行效果图:
2.使用实例工厂实例化Bean
实例化静态工厂注入bean,需要先实例化一个工厂类,然后通过由实例化工厂对象中的一个方法来创建bean,并注入到容器中。 在 bean 的 factory-bean 属性里指定拥有该工厂方法的 Bean;在 factory-method 属性里指定该工厂方法的名称;使用 construtor-arg 元素为工厂方法传递方法参数。
UserService接口
1 2 3 4 5 | package com.zk.myspring; public interface UserService { public void addUser(); } |
UserServiceImpl实现类
1 2 3 4 5 6 7 8 9 | package com.zk.myspring; public class UserServiceImpl implements UserService{ @Override public void addUser() { // TODO Auto-generated method stub System.out.println( "UserService 实例工厂" ); } } |
MyBeanFactory实例工厂
1 2 3 4 5 6 7 8 9 10 | package com.zk.myspring; public class MyBeanFactory { /* * 创建实例工厂 */ public UserService createService(){ return new UserServiceImpl(); } } |
ApplicationContext.xml配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?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:util= "http://www.springframework.org/schema/util" xsi:schemaLocation=" http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 将实例工厂实例交给spring class 确定实例工厂全限定类名 factory-method:确定静态方法名 --> <bean id= "MyBeanFactoryId" class = "com.zk.myspring.MyBeanFactory" ></bean> <bean id= "userserviceId" factory-bean= "MyBeanFactoryId" factory-method= "createService" ></bean> </beans> |
运行效果图:
3.使用静态工厂创建Bean
利用静态工厂方法可以把bean注入到IOC容器中。在XML文件中配置bean时,要指定class的属性为工厂的类;factory-method属性指定工厂类中工厂方法,用于创建bean;constrctor-arg用于给工厂方法传递参数。
UserService.java
1 2 3 4 5 | package com.zk.spring; public interface UserService { public void addUser(); } |
UserServiceImpl.java
1 2 3 4 5 6 7 8 9 | package com.zk.spring; public class UserServiceImpl implements UserService{ @Override public void addUser() { // TODO Auto-generated method stub System.out.println( "UserService静态工厂" ); } } |
ApplicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 | <?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:util= "http://www.springframework.org/schema/util" xsi:schemaLocation=" http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- <bean id= "UserService" class = "com.zk.spring.UserService" ></bean>--> <bean id= "UserServiceId" class = "com.zk.spring.MyBeanFactory" factory-method= "createService" > </bean> </beans> |
TestDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.zk.spring; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestDemo { @Test public void test1(){ String xmlpath= "ApplicationContext.xml" ; ApplicationContext ac= new ClassPathXmlApplicationContext(xmlpath); UserService us=(UserService) ac.getBean( "UserServiceId" ); System.out.println(us); us.addUser(); } } |
运行效果图:
参考:https://blog.csdn.net/u010502101/article/details/78638449
https://www.w3cschool.cn/wkspring/8kei1icc.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)