spring创建bean的三种方式

Posted 牛牛的自留地

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring创建bean的三种方式相关的知识,希望对你有一定的参考价值。

1、使用构造器创建bean

1.1、使用无参构造器创建

package com.ly.spring;
public class Person {
    private String name;
    public void say(String name) {
        System.out.println("你好,我叫"+name);
    }
}
<?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="person" class="com.ly.spring.Person"></bean>
</beans>

 

1.2、使用有参构造器创建

package com.ly.spring;
public class Person {
    private String name;
    public Person(String name) {
        this.name = name;
    }
    public void say() {
        System.out.println("我的名字叫"+this.name);
    }
}

 

<?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="person" class="com.ly.spring.Person">
    <!--指定构造器对应参数名对应的值-->
    <constructor-arg name="name" value="有参构造"></constructor-arg>
</bean>
</beans>

2、使用静态工厂方法创建bean

Person:

package com.ly.spring;
public class Person {
    private String name;
    public Person(String name) {
        this.name = name;
    }
    public void say() {
        System.out.println("我的名字叫"+this.name);
    }
}

 

Chinese:

package com.ly.spring;
public class Chinese implements Person {
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public void say() {
        System.out.println("我的名字叫"+this.name);
    }
}

 

American:

package com.ly.spring;
public class American implements Person {
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public void say() {
        System.out.println("我的名字叫"+this.name);
    }
}

 

工厂类PersonFactory:

package com.ly.spring;
public class PersonFactory {
    public static Person getPerson(String name) {
        if("Chinese".equals(name)) {
            return new Chinese();
        }else if("American".equals(name)) {
            return new American();
        }
        return null;
    }
}

配置文件:

<?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">
    <!--
    class指定工厂类类路径
    factory-method指定工厂类的静态方法
    -->
    <bean id="chinese" class="com.ly.spring.PersonFactory"  factory-method="getPerson">
        <!--给工厂的静态方法注入参数-->
        <constructor-arg name="name" value="Chinese"></constructor-arg>
        <!--通过set方法给Chinese类的name属性注入值-->
        <property name="name" value="中国人"></property>
    </bean>
    <bean id="american" class="com.ly.spring.PersonFactory" factory-method="getPerson">
        <constructor-arg name="name" value="American"></constructor-arg>
        <property name="name" value="美国人"></property>
    </bean>
</beans>

 

测试类:

package com.ly.spring.test;
import com.ly.spring.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Person p1 = context.getBean("chinese", Person.class);//获取到的其实是Chinese类
        p1.say();//执行时会调用Chinese类的方法
        Person p2 = context.getBean("american",Person.class);//获取到的其实是American类
        p2.say();//执行时会调用American类的方法
    }
}

3、使用实例工厂方法创建bean

修改后的工厂类PersonFactory:

package com.ly.spring;
public class PersonFactory {
    //这里不再是静态方法
    public Person getPerson(String name) {
        if("Chinese".equals(name)) {
            return new Chinese();
        }else if("American".equals(name)) {
            return new American();
        }
        return null;
    }
}

 

修改后的配置文件:

<?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="personFactory" class="com.ly.spring.PersonFactory"></bean>
    <!--
    factory-bean指定配置的工厂bean
    factory-method指定工厂类的实例方法
    -->
    <bean id="chinese" factory-bean="personFactory" factory-method="getPerson">
        <!--给工厂的实例方法注入参数-->
        <constructor-arg name="name" value="Chinese"></constructor-arg>
        <!--通过set方法给Chinese类的name属性注入值-->
        <property name="name" value="中国人"></property>
    </bean>
    <bean id="american" factory-bean="personFactory" factory-method="getPerson">
        <constructor-arg name="name" value="American"></constructor-arg>
        <property name="name" value="美国人"></property>
    </bean>
</beans>

 

以上是关于spring创建bean的三种方式的主要内容,如果未能解决你的问题,请参考以下文章

spring管理bean的三种创建方式

spring创建bean的三种方式

spring创建bean的三种方式

spring中创建bean对象的三种方式以及作用范围

Spring提供的三种装配Bean方式

Spring的三种注入方式扩展之spring对Bean的管理110402