Bean 定义
被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的。bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象。这些 bean 是由用容器提供的配置元数据创建的,例如,已经在先前章节看到的,在 XML 的表单中的 定义。
spring bean的实例化方式一共有三种方式:
1.构造器构造bean
Spring可以使用默认构造器或者有参构造器创建Bean实例
有参构造器
UserService.java
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
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
<?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
package com.zk.spring; public class UserService { public UserService(){ } public void addUser(){ System.out.println("addUser"); } }
TestDemo.java
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
<?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接口
package com.zk.myspring; public interface UserService { public void addUser(); }
UserServiceImpl实现类
package com.zk.myspring; public class UserServiceImpl implements UserService{ @Override public void addUser() { // TODO Auto-generated method stub System.out.println("UserService 实例工厂"); } }
MyBeanFactory实例工厂
package com.zk.myspring; public class MyBeanFactory { /* * 创建实例工厂 */ public UserService createService(){ return new UserServiceImpl(); } }
ApplicationContext.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" 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
package com.zk.spring; public interface UserService { public void addUser(); }
UserServiceImpl.java
package com.zk.spring; public class UserServiceImpl implements UserService{ @Override public void addUser() { // TODO Auto-generated method stub System.out.println("UserService静态工厂"); } }
ApplicationContext.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" 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
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