Java教程

spring-bean实例化

本文主要是介绍spring-bean实例化,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

bean的实例化有3种方式:

方式一:
默认构造方式:表示调用类的默认构造方法创建bean对象。

<bean  id=""  class=""></bean>

目录结构:

  创建UserService:

package com.spring02.beanbox;

public class UserService {
    public void saveUser() throws Exception{
        System.out.println("UserService.saveUser()");
    }
}

创建bean.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="UserService" class="com.spring02.beanbox.UserService"></bean>
</beans>

创建测试类:

package com.spring02.beanbox;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDemo {
    @Test
    public void test() throws Exception{
        ApplicationContext ac = new ClassPathXmlApplicationContext("com/spring02/beanbox/bean.xml");
        UserService userService = (UserService) ac.getBean("UserService");
        userService.saveUser();
    }
}


方式二:
静态工厂方式:有一个工厂类,在类中所有方法都是静态的,作用生产目标类对象。创建工厂类,定义静态方法生产需要的bean对象。
配置:<bean. id="". class="静态工厂bean". factory-method="工厂方法"></bean>

目录结构:

 

 

创建UserService:

 

package com.spring02.beanbox;

public class UserService {
    public void saveUser() throws Exception{
        System.out.println("UserService.saveUser()");
    }
}

 

 

创建静态工厂bean:MyStaticFactoryBean

package com.spring02.beanbox;

public class MyStaticFactoryBean {
    private static UserService userService;
    public static UserService createUserService(){
        if (userService == null){
            userService = new UserService();
        }
        return userService;
    }
}

创建bean.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="UserService2" class="com.spring02.beanbox.MyStaticFactoryBean" factory-method="createUserService"></bean>
</beans>

创建测试类:
package com.spring02.beanbox;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDemo {
    @Test
    public void test() throws Exception{
        ApplicationContext ac = new ClassPathXmlApplicationContext("com/spring02/beanbox/bean.xml");
        UserService userService = (UserService) ac.getBean("UserService2");
        userService.saveUser();
    }
}


方式三:

实例工厂方式:有一个工厂类,在类中所有方法都是成员方法,作用生产目标类对象。创建工厂类,定义成员方法生产需要的bean对象。
配置文件中:先配置示例工厂bean,再利用工厂bean生产目标bean对象。
<bean  id=""  factory-bean=""  factory-method=""></bean>

 

 

创建UserService:

package com.spring02.beanbox;

public class UserService {

    public void saveUser() throws Exception{
        System.out.println("UserService3.saveUser()");
    }
}

创建实例工厂MyObjectFactoryBean:

package com.spring02.beanbox;

public class MyObjectFactoryBean {
    private static UserService userService;

   public UserService createUserService(){
       if(this.userService == null){
           this.userService = new UserService();
       }
       return userService;
   }
}


 创建bean.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 -->
    <bean id="MyFactoryBean" class="com.spring02.beanbox.MyObjectFactoryBean"></bean>
    
   <!-- 利用工厂bean生产目标bean -->
    <bean id="UserService3" factory-bean="MyFactoryBean" factory-method="createUserService"></bean>
</beans>

 创建测试类:

package com.spring02.beanbox;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDemo {
    @Test
    public void test() throws Exception{
        ApplicationContext ac = new ClassPathXmlApplicationContext("com/spring02/beanbox/bean.xml");
        UserService userService = (UserService) ac.getBean("UserService3");
        userService.saveUser();
    }
}
这篇关于spring-bean实例化的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!