1 在spring里面通过配置文件创建对象
2 bean实例化三种方式实现
(1) xml 文件 bean1.xml
(2) User 类
(3)测试类 TestIOC 类
(1)xml 文件 bean1.xml
(2)Bean2 类
(3)Bean2Factory 类
(4) 测试类 TestIOC 类
(1)xml 文件
(2) Bean3 类
(3)Bean3Factory 类
(4)测试类 TestIOC 类
                    2 bean实例化三种方式实现
第一种、使用类的无参数构造创建(重点)
(1) xml 文件 bean1.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">
 - <!--ioc入门-->
 - <bean id="user" class="com.liuyanzhao.ioc.User"></bean>
 - </beans>
 
(2) User 类
- package com.liuyanzhao.ioc;
 - public class User {
 - public void add() {
 - System.out.println("add..........");
 - }
 - }
 
(3)测试类 TestIOC 类
- package com.liuyanzhao.ioc;
 - import org.junit.Test;
 - import org.springframework.context.ApplicationContext;
 - import org.springframework.context.support.ClassPathXmlApplicationContext;
 - public class TestIOC {
 - @Test
 - public void testUser() {
 - //1、加载Spring配置文件,根据创建对象
 - ApplicationContext context =
 - new ClassPathXmlApplicationContext("bean1.xml");
 - //2、得到配置创建的对象
 - User user = (User) context.getBean("user");
 - System.out.println(user);
 - }
 - }
 
第二种 、使用静态工厂创建(了解)
(1)xml 文件 bean1.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="bean2" class="com.liuyanzhao.bean.Bean2Factory" factory-method="getBean2"></bean>
 - </beans>
 
(2)Bean2 类
- package com.liuyanzhao.bean;
 - public class Bean2 {
 - public void add() {
 - System.out.println("bean2.........");
 - }
 - }
 
(3)Bean2Factory 类
- package com.liuyanzhao.bean;
 - public class Bean2Factory {
 - //静态方法,返回Bean2对象
 - public static Bean2 getBean2() {
 - return new Bean2();
 - }
 - }
 
(4) 测试类 TestIOC 类
- package com.liuyanzhao.bean;
 - import org.junit.Test;
 - import org.springframework.context.ApplicationContext;
 - import org.springframework.context.support.ClassPathXmlApplicationContext;
 - public class TestIOC {
 - @Test
 - public void testUser() {
 - //1、加载Spring配置文件,根据创建对象
 - ApplicationContext context =
 - new ClassPathXmlApplicationContext("bean1.xml");
 - //2、得到配置创建的对象
 - Bean2 bean2 = (Bean2) context.getBean("bean2");
 - System.out.println(bean2);
 - }
 - }
 
第三种、使用实例工厂创建(了解)
(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="bean3Factory" class="com.liuyanzhao.bean.Bean3Factory"></bean>
 - <bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3" ></bean>
 - </beans>
 
(2) Bean3 类
- package com.liuyanzhao.bean;
 - public class Bean3 {
 - public void add() {
 - System.out.println("bean3.........");
 - }
 - }
 
(3)Bean3Factory 类
- package com.liuyanzhao.bean;
 - public class Bean3Factory {
 - //普通方法,返回Bean3对象
 - public Bean3 getBean3() {
 - return new Bean3();
 - }
 - }
 
(4)测试类 TestIOC 类
- package com.liuyanzhao.bean;
 - import org.junit.Test;
 - import org.springframework.context.ApplicationContext;
 - import org.springframework.context.support.ClassPathXmlApplicationContext;
 - public class TestIOC {
 - @Test
 - public void testUser() {
 - //1、加载Spring配置文件,根据创建对象
 - ApplicationContext context =
 - new ClassPathXmlApplicationContext("bean1.xml");
 - //2、得到配置创建的对象
 - Bean3 bean3 = (Bean3) context.getBean("bean3");
 - System.out.println(bean3);
 - }
 - }
 

                            
                                


                
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏