您的位置:首页 > 编程语言 > Java开发

Spring学习笔记(二)

2015-11-01 17:53 387 查看

六、初始化和销毁

之前说过的IoC代表着对象的创建、初始化和销毁都由Spring框架来完成,那么初始化和销毁是怎么回事呢?

想要做到这两件事,需要在类中自己写一个初始化和销毁的方法,然后在配置文件中进行注册。仍然以HelloWorld为例,代码书写如下:

package com.tt.helloworld;

import java.io.Serializable;

public class HelloWorld implements Serializable {

private Long id;
private String name;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HelloWorld(){
System.out.println("create object by constructor");
}
public void hello(){
System.out.println("hello world");
}
public void init(){
System.out.println("init");
}
public void destroy(){
System.out.println("destroy");
}
}


可以看到,这里我用init()方法来完成初始化任务,用destroy()方法来完成销毁任务。接着在配置文件中进行配置:

<bean id="helloWorld" class="com.tt.helloworld.HelloWorld" init-method="init" destroy-method="destroy" ></bean>


这样,我们就可以自定义对象初始化和销毁的过程了。另外,Spring容器默认在容器关闭的时候执行对象的销毁。

还有一个小的知识点是,若scope=”prototype”的话,则Spring容器关闭时也不执行销毁的过程。

七、DI

依赖注入,通俗的解释其实就是实现给类中的属性赋值。

在Spring容器中有两种方式可以实现依赖注入,分别是set方法注入和构造方法注入。

(1)set方法注入

想使用set方法注入的话当然首先要有set方法,因此首先要在类中写好属性的set方法,然后在配置文件中配置。仍然以HelloWorld为例,配置文件书写如下:

<bean id="helloWorld" class="com.tt.helloworld.HelloWorld" init-method="init" destroy-method="destroy" scope="prototype">
<property name="id" value="123"></property>
<property name="name" value="shuai ge"></property>
</bean>


这样的话,程序员再编写测试类时就可以直接调用该类的属性了,并且它们的值是id=123,name=shuai ge(帅哥是我室友的外号,因为他的名字就是帅,哈哈)。

(2)构造方法注入

首先要重写构造方法,针对之前的HelloWorld构造方法书写如下:

public HelloWorld(Long id,String name){
this.id=id;
this.name=name;
}


配置文件书写如下:

<constructor-arg index="0" value="123"></constructor-arg>
<constructor-arg index="1" value="shuai ge"></constructor-arg>


要记得构造方法的index都是从0开始计数的。然后测试类的书写同上。

之前一直觉得IoC和DI没什么意义,创建对象这件事程序员自己也可以做,并且也不是很费事,今天才明白,IoC和DI真正的作用是实现完全的面向接口编程,下面就用一个例子来体会一下吧。

例子:想象一个业务场景,我们在书写文档的时候可能会有多种类型的文档:word、excel和PDF,但它们都属于文档,可以定义一个接口叫Document,然后让这三种类型的文档都去实现它,然后在客户端调用document,利用Spring的IoC和DI即可实现完全的面向接口编程。

代码如下:

//Doucemnt接口
package com.tt.inter;

public interface Document {
public void reader();
public void writer();
}


//DocumentManager类,用它来操作文档。
package com.tt.inter;

public class DocumentManager {
private Document document ;
public DocumentManager(Document document){
this.document = document;
}
public Document getDocument() {
return document;
}
public void setDocument(Document document) {
this.document = document;
}

}


//ExcelDocument类
package com.tt.inter;

public class ExcelDocument implements Document{
public void reader(){
System.out.println("excel reader");
}
public void writer(){
System.out.println("excel writer");
}
}


//PDFDocument类
package com.tt.inter;

public class PDFDocument implements Document{
public void reader(){
System.out.println("PDF reader");
}
public void writer(){
System.out.println("PDF writer");
}
}


//WordDocument类
package com.tt.inter;

public class WordDocument implements Document{
public void reader(){
System.out.println("word reader");
}
public void writer(){
System.out.println("word writer");
}
}


配置文件:

<bean id="documentmanager" class="com.tt.inter.DocumentManager">
<constructor-arg index="0" ref="pdfdocument"></constructor-arg>
</bean>
<bean id="exceldocument" class="com.tt.inter.ExcelDocument"></bean>
<bean id="pdfdocument" class="com.tt.inter.PDFDocument"></bean>
<bean id="worddocument" class="com.tt.inter.WordDocument"></bean>


//测试类
package com.tt.test;

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

import com.tt.inter.Document;
import com.tt.inter.DocumentManager;

public class TestInter {
@Test
public void TestDocument(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DocumentManager document = (DocumentManager)context.getBean("documentmanager");
document.getDocument().reader();
document.getDocument().writer();
}
}


当运行测试类的时候我们就可以看到运行结果了,若
<constructor-arg index="0" ref="pdfdocument"></constructor-arg>
中的ref配置为pdfdocument,则执行pdfdocument的方法,若改为其他的document则运行其他的方法,改变文档格式只需要修改配置文件的内容,而测试类从始至终都只是一个接口,完全看不到是如何实现的,因此是完全的面向接口编程。还真的很有意思呢。

八、注解方式实现IoC和DI

用注解实现IoC和DI的话则不需要写太多配置文件的内容,但是效率会有相应的影响。

在这里,有两个注解:@Resourse和@Component,@Resourse用在引用类的属性上,用于属性注入,而@Component用在类上,实现类的控制反转。举个例子来看一下吧:

创建两个类:Student和Course,并且Student中有一个属性是Course类型

//Student类
package com.tt.annotation;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

@Component("ss")
public class Student {
private long id;
private String name;
@Resource(name="cc")
private Course course;
public void show(){
this.course.show();
}
}


// Course类
package com.tt.annotation;

import org.springframework.stereotype.Component;

@Component("cc")
public class Course {
private long id;
private String name;
private String description;
public void show(){
System.out.println("course");
}
}


配置文件要加上如下的一句话:

<context:component-scan base-package="com.tt.annotation"></context:component-scan>


另外namespace也要改,改为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">[/code] 
这么几步改完之后,就可以用注解的形式实现IoC和DI了。

今天就总结到这里,以后接着总结,加油。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring