Spring框架入门之SpringIOC容器对象的使用

为什么要使用Spring

在目前基于JSP、Servlet、Bean(传统MVC)模式下的项目虽然做到了业务层与视图层的分离,但代码的复用性和解耦能力有待进一步提高。代码中存在着许多冗余且不易维护的部分,如何将这些部分进一步精简是Spring等高级框架研究的重点

快速使用第一步,从Bean到实例化对象

以前需要new出来的对象现在使用反射机制在XML中指定出来即可

如下所示的一个实例业务层对象

public class testService {
    public void testShowService(){
        System.out.println("showing the service...");
    }
}

从前我们要使用它,简单的方法是new一个实例对象

而现在,我们在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">

    <!--配置业务层的对象,该XML命名为applicationcontext.xml-->

    <bean id="service" class="com.erwin.service.impl.testService"></bean>
<!--指明bean的id与对应class位置,之后的使用中只需要指定id即可通过反射机制获得实例化对象-->
</beans>

测试一下,我们能否正常使用它:

public class test {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Spring容器对象获取业务层对象
        testService serviceA = (testService) ac.getBean("service");
        serviceA.testShowService();

    }

}

获得输出:

在这里插入图片描述

这样看起来好像通过Spring容器的方式并没有比new简单多少,甚至更加麻烦了,但是这样做的好处是,我们已经将servlet与对象的实例化解耦,之后如果修改了需要实例化的对象(名称或结构),我们不需要再在serlvet中修改更多的内容,而在xml中进行修改即可。这也是动态编译(假,Java并没有动态编译)的一大优势所在。

有参数的实例化方法

上述例子中是一个最简单的状态,即只调用了Bean对象的无参数构造方法,那么如何才能调用其有参数的方法呢?

public class testService {

    String name;
    public void testShowService(){
        System.out.println("showing the service..."+name);
    }
    public testService(String name){
        this.name = name;
    }/*新增带参数方法*/
    public testService(){

    }
}
    <bean id="serWithName" class="com.erwin.service.impl.testService">
        <constructor-arg index="0" type="java.lang.String" name="name" value="Erwin"></constructor-arg>
    </bean>

index表示第一个参数,type表示对应类或数据结构

public class test {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Spring容器对象获取业务层对象
        testService serviceA = (testService) ac.getBean("serWithName");
        serviceA.testShowService();
    }
}

在这里插入图片描述

但实际上很少使用这种方法,因为它实在有点繁琐,而使用无参构造方法后调用set方法的方式来实现,被称为属性注入

属性注入

<?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">
    <!--创建student的bean对象-->
    <!--
    属性注入方式
    特点:相当于创建一个空对象然后使用set方法赋值
    使用:
    property:在bean标签下使用子标签property,表示调用set方法给某个属性赋值
    属性:name:要赋值的属性名
          value:值
    -->
    <bean id="stu3" class="com.bjsxt.pojo.Student">
        <property name="sid" value="2"></property>
        <property name="sname" value="李四"></property>
    </bean>
</beans>

底层实际上是Spring调用了类中的set方法实现的,所以类中必须含有set方法:

public class testService {

    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void testShowService(){
        System.out.println("showing the service..."+name);
    }
    public testService(String name){
        this.name = name;
    }
    public testService(){

    }
}

通过工厂模式

考虑这样一个场景:

我们在使用Java代码处理某个问题的时候,需要创建A对象,调用 A对象中的某个方法,但是A对象的创建依赖B对象,而B对象的 创建又依赖于C对象,C对象的创建又依赖于D对象…,如下:


D d=new D();
C c=new C(d);
B b=new B(c);
A a=new A(b);

这样造成代码的阅读性极差

解决:

将对象的创建过程进行封装,直接返回创建好的对象使用.

实现:

工厂设计模式

本质:就是封装对象的创建过程的一种代码的编程思想

<?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">
    <!--创建student的bean对象-->
    <!--工厂设计模式-->
    <!--动态工厂-->
        <bean id="factory" class="com.bjsxt.pojo.StudentFactory"></bean>
        <!--生产Student对象-->
        <bean id="stu1" factory-bean="factory" factory-method="newIntance"></bean>
    <!--静态工厂-->
    <!--可以理解为静态方法直接用类名调用-->
        <bean id="stu2" class="com.bjsxt.pojo.StudentFactory" factory-method="newIntanceStatic"></bean>
</beans>

//动态方法
public class StudentFactory{

	public Student newInstance(){
	
	return new Student();	
}

	public static Student newIntanceStatic(){
	return new Student();
	}


}
public class testStu {
    
    public static void main(String[] args) {
        //创建容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
        //获取容器中的对象
        //工厂设计模式     
            //动态工厂
            Student student = (Student) ac.getBean("stu1");
            System.out.println("动态工厂:"+student);
            //静态工厂
            Student student1 = (Student) ac.getBean("stu2");
            System.out.println("静态工厂:"+student1);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值