美文网首页Spring我爱编程
spring ioc bean实例化的三种方式(xml配置)

spring ioc bean实例化的三种方式(xml配置)

作者: DouDouZH | 来源:发表于2018-05-25 22:08 被阅读15次

    bean实例化有三种方式

    1、使用无参数构造创建(重点)

    User.java

    package work.zhangdoudou.ico;
    
    public class User {
        public void add(){
            System.out.println("add······");
        }
    }
    

    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"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!-- 无参数构造创建 -->
        <bean id="user" class="work.zhangdoudou.ico.User"></bean>
    </beans>
    

    TestUser.java

    package work.zhangdoudou.test;
    
    import static org.junit.Assert.*;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import work.zhangdoudou.ico.User;
    
    public class TestUser {
    
        @Test
        public void test() {
            //1加载配置文件,根据创建对象
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            //2得到创建的对象
            User user=(User) context.getBean("user");
            user.add();
            System.out.println(user);
            
        }
    }
    

    运行结果


    image.png
    • 类不写构造默认无参构造
    • 写了有参数构造,无参数构造被覆盖,必须手动重写
    • 类里面没有无参构造,会出现异常
    发生异常
    2、使用静态工厂创建
    • 创建静态方法,返回类对象
      user2.java
    package work.zhangdoudou.ico;
    
    public class User2 {
        private String username;
        
        public User2(){
            
        }
        public User2(String username) {
            this.username = username;
        }
    
        public void add(){
            System.out.println("user2······");
        }
    }
    

    静态工厂类Bean2Factory .java

    package work.zhangdoudou.BeanFactory;
    
    import org.springframework.context.annotation.Bean;
    import work.zhangdoudou.ico.User2;
    
    public class Bean2Factory {
        //静态方法返回Bean2对象
        public static User2 getBean2(){
            return new User2();
        }
    
    }
    

    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"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!-- 使用静态工厂创建bean2 -->
        <bean id="user2" class="work.zhangdoudou.BeanFactory.Bean2Factory" factory-method="getBean2" ></bean>
    </beans>
    

    TestUser2.java

    package work.zhangdoudou.test;
    
    import static org.junit.Assert.*;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import work.zhangdoudou.ico.User;
    import work.zhangdoudou.ico.User2;
    
    public class TestUser2 {
    
        @Test
        public void test() {
            //1加载配置文件,根据创建对象
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            //2得到创建的对象
            User2 user2=(User2) context.getBean("user2");
            user2.add();
            System.out.println(user2);
            
        }
    }
    

    运行结果


    image.png
    3、使用实例工厂创建
    • 创建不是静态的方法,返回类对象

    User3.java

    package work.zhangdoudou.ico;
    
    public class User3 {
        private String username;
        
        public User3(){
            
        }
        public User3(String username) {
            this.username = username;
        }
    
        public void add(){
            System.out.println("user3······");
        }
    }
    

    user3的实例工厂 Bean3Factory.java

    package work.zhangdoudou.BeanFactory;
    
    import org.springframework.context.annotation.Bean;
    import work.zhangdoudou.ico.User3;
    
    public class Bean3Factory {
        //普通的方法, 返回Bean3对象
        public User3 getBean3(){
            return new User3();
        }
    }
    

    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"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <!-- 实例工厂创建对象 -->
        <!-- 创建工厂对象 -->
        <bean id="bean3Factory" class="work.zhangdoudou.BeanFactory.Bean3Factory"></bean>
        <bean id="user3" factory-bean="bean3Factory" factory-method="getBean3"></bean>
    </beans>
    

    TestUser3.java

    package work.zhangdoudou.test;
    
    import static org.junit.Assert.*;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import work.zhangdoudou.ico.User3;
    
    public class TestUser3 {
    
        @Test
        public void test() {
            //1加载配置文件,根据创建对象
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            //2得到创建的对象
            User3 user3=(User3) context.getBean("user3");
            user3.add();
            System.out.println(user3);
            
        }
    }
    

    运行结果


    image.png

    相关文章

      网友评论

        本文标题:spring ioc bean实例化的三种方式(xml配置)

        本文链接:https://www.haomeiwen.com/subject/cctpjftx.html