Spring的bean管理(xml方式)之Bean实例化的方式

avatar 2017年08月02日20:07:48 1 2020 views
博主分享免费Java教学视频,B站账号:Java刘哥
1 在spring里面通过配置文件创建对象 2 bean实例化三种方式实现

第一种、使用类的无参数构造创建(重点)

(1) xml 文件 bean1.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xsi:schemaLocation="
  5.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <!--ioc入门-->
  7.     <bean id="user" class="com.liuyanzhao.ioc.User"></bean>
  8. </beans>
(2) User 类
  1. package com.liuyanzhao.ioc;
  2. public class User {
  3.     public void add() {
  4.         System.out.println("add..........");
  5.     }
  6. }
(3)测试类 TestIOC 类
  1. package com.liuyanzhao.ioc;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class TestIOC {
  6.     @Test
  7.     public void testUser() {
  8.         //1、加载Spring配置文件,根据创建对象
  9.         ApplicationContext context =
  10.             new ClassPathXmlApplicationContext("bean1.xml");
  11.         //2、得到配置创建的对象
  12.         User user = (User) context.getBean("user");
  13.         System.out.println(user);
  14.     }
  15. }
 

第二种 、使用静态工厂创建(了解)

(1)xml 文件 bean1.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xsi:schemaLocation="
  5.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <!--使用静态工厂创建对象-->
  7.     <bean id="bean2" class="com.liuyanzhao.bean.Bean2Factory" factory-method="getBean2"></bean>
  8. </beans>
(2)Bean2 类
  1. package com.liuyanzhao.bean;
  2. public class Bean2 {
  3.     public void  add() {
  4.         System.out.println("bean2.........");
  5.     }
  6. }
(3)Bean2Factory 类
  1. package com.liuyanzhao.bean;
  2. public class Bean2Factory {
  3.     //静态方法,返回Bean2对象
  4.     public static Bean2 getBean2() {
  5.         return new Bean2();
  6.     }
  7. }
(4) 测试类 TestIOC 类
  1. package com.liuyanzhao.bean;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class TestIOC {
  6.     @Test
  7.     public void testUser() {
  8.         //1、加载Spring配置文件,根据创建对象
  9.         ApplicationContext context =
  10.             new ClassPathXmlApplicationContext("bean1.xml");
  11.         //2、得到配置创建的对象
  12.         Bean2 bean2 = (Bean2) context.getBean("bean2");
  13.         System.out.println(bean2);
  14.     }
  15. }
 

第三种、使用实例工厂创建(了解)

(1)xml 文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xsi:schemaLocation="
  5.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <!--使用实例工厂创建对象-->
  7.     <!--创建工厂对象-->
  8.     <bean id="bean3Factory" class="com.liuyanzhao.bean.Bean3Factory"></bean>
  9.     <bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3" ></bean>
  10. </beans>
(2) Bean3 类
  1. package com.liuyanzhao.bean;
  2. public class Bean3 {
  3.     public void add() {
  4.         System.out.println("bean3.........");
  5.     }
  6. }
(3)Bean3Factory 类
  1. package com.liuyanzhao.bean;
  2. public class Bean3Factory {
  3.     //普通方法,返回Bean3对象
  4.     public Bean3 getBean3() {
  5.         return new Bean3();
  6.     }
  7. }
(4)测试类 TestIOC 类
  1. package com.liuyanzhao.bean;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class TestIOC {
  6.     @Test
  7.     public void testUser() {
  8.         //1、加载Spring配置文件,根据创建对象
  9.         ApplicationContext context =
  10.             new ClassPathXmlApplicationContext("bean1.xml");
  11.         //2、得到配置创建的对象
  12.         Bean3 bean3 = (Bean3) context.getBean("bean3");
  13.         System.out.println(bean3);
  14.     }
  15. }
 
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0