Spring入门(4),装配Bean,基于xml

来源:互联网 发布:最全双色球缩水软件 编辑:程序博客网 时间:2024/05/18 00:01

注意:spring3不支持jdk1.8,如果要用jdk1.8请使用spring4。本例子使用的spring3。
bean的实例化方式:默认构造,静态工厂,实例化工厂。

默认构造:
<bean id = "useradd" class = "com.cn.user.UserAdd"> 就会提供默认构造。

静态工厂:
常用于spring 整合其他框架(工具)。用于生成实例对象,所有方法都是static。

<bean id = "useradd" class = "com.cn.user.UserAdd" factory-metod="add'>

实例工厂:
必须现有实例化对象,通过实例对象创建对象。提供所有的方法都是“非静态”的。

下面我们来看几个小例子:

1.静态工厂
UserService.java

package com.cn.inject.static_factory;public interface UserService {      public void addUse();}

UserServiceImpl.java

package com.cn.inject.static_factory;public class UserServiceImpl implements UserService {    @Override    public void addUse() {        System.out.println("static factory");    }}

MyBeanFactory.java

package com.cn.inject.static_factory;/** * 静态工厂 * */public class MyBeanFactory {    /**     * 创建实例     * @return     */    public static UserService createService(){        return new UserServiceImpl();    }}

beans.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">    <!-- 将我们静态工厂创建的实例交于spring         class 确定静态工厂的全限定类名        factory-method 确定静态方法名    -->    <bean id="userServiceId" class="com.cn.inject.static_factory.MyBeanFactory" factory-method="createService"></bean></beans>

TestStaticFactory.java

package com.cn.inject.static_factory;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestStaticFactory {    @Test    //自定义工厂    public void demo1(){        UserService userService = MyBeanFactory.createService();        userService.addUse();    }    @Test    public void demo2(){        //spring工厂        String xmlPath = "com/cn/inject/static_factory/beans.xml";        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);        UserService userService = applicationContext.getBean("userServiceId",UserService.class);        userService.addUse();    }}

2.实例工厂

UserService.java

package com.cn.inject.factory;public interface UserService {    public void addUse();}

UserServiceImpl.java

package com.cn.inject.factory;public class UserServiceImpl implements UserService {    @Override    public void addUse() {        System.out.println("factory");    }}

MyBeanFactory.java

package com.cn.inject.factory;/** * 实例工厂 * */public class MyBeanFactory {    /**     * 创建实例     * @return     */    public  UserService createService(){        return new UserServiceImpl();    }}

beans.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="myBeanFactoryId" class="com.cn.inject.factory.MyBeanFactory"></bean>    <!-- 创建对象 -->    <bean id="userServiceId" factory-bean="myBeanFactoryId" factory-method="createService"></bean></beans>

TestFactory.java

package com.cn.inject.factory;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestFactory {    @Test    //自定义工厂    public void demo1(){        //自定义实例工厂        //1.创建工厂        MyBeanFactory myBeanFactory = new MyBeanFactory();        //2.通过工厂实例来获取对象        UserService userService = myBeanFactory.createService();        userService.addUse();    }    @Test    public void demo2(){        //spring工厂        String xmlPath = "com/cn/inject/factory/beans.xml";        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);        UserService userService = applicationContext.getBean("userServiceId",UserService.class);        userService.addUse();    }}
1 0
原创粉丝点击