码迷,mamicode.com
首页 > 编程语言 > 详细

spring创建bean的三种方式

时间:2020-02-19 13:08:00      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:return   ext   构造   context   imp   beans   路径   com   frame   

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的三种方式

标签:return   ext   构造   context   imp   beans   路径   com   frame   

原文地址:https://www.cnblogs.com/liuyang-520/p/12330640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!