Spring回顧(二)註解實現IoC和DI

註解實現DI

@Resource

爲JavaEE拓展包提供。代替xml配置,注入對象,按照bean的id和類型匹配。具體寫法如下:
配置文件:

<!—配置註解解析器-->
<context:annotation-config></context:annotation-config>
<bean id="car" class="com.spring.annotation.di.Car"></bean>
<bean id="wheel" class="com.spring.annotation.di.Wheel"></bean>

Car.java

package com.spring.annotation.di;
import javax.annotation.Resource;
public class Car {
    @Resource
    private Wheel wheel;
    public void show(){
        System.out.println("I'm a Car !");
        wheel.show();
    }
}

Wheel.java

package com.spring.annotation.di;
public class Wheel {
    public void show(){
        System.out.println("I'm a wheel !");
    }
}

測試類:Annotation_di_test.java

package com.spring.annotation.di;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Annotation_di_test {
    @Test
    public void test_Annotation_Di(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        Car car = (Car) context.getBean("car");
        car.show();
    }

Tips:這裏@Resource註解還能添加name屬性。

  • 如果@Resource的註解的name屬性的值爲”“

則把@Resource所在的屬性的名稱和spring容器中的id作匹配
如果匹配成功,則賦值
如果匹配不成功,則會按照類型進行匹配
如果匹配成功,則賦值,匹配不成功,報錯

  • 如果@Resource的註解的name屬性的值不爲”“

則解析@Resource註解name屬性的值,把值和spring容器中的ID進行匹配
如果匹配成功,則賦值
如果匹配不成功,則報錯

@Autowired

代替xml配置,注入對象,按照bean的類型匹配。爲spring框架提供。與Qualifier註解組合。可以代替@Resource註解的功能。例子如下:
Car.java

package com.spring.annotation.di;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Car {
    //這兩行註解相當於@Resource(name="wheel")
    @Autowired
    @Qualifier("wheel")
    private Wheel wheel;
    public void show(){
        System.out.println("I'm a Car !");
        wheel.show();
    }
}

IoC

@Component

將類放到Spring容器中,我們稱這個類爲一個bean,也稱作一個component。在定義類類時使用註解@Component,能替代在配置文件applicationContext.xml中配置bean。從而減少配置文件的書寫。
首先我們要在配置文件中聲明哪個包的bean註解需要被掃描:

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

在定義bean類時使用@Component註解,可使用value屬性,即指定bean的id。若不指定value屬性,則會以第一個字母小寫的類名作爲bean的id

package com.spring.annotation.ioc;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

@Component(value="helloWorld")
public class HelloWorld {
    public void hello(String name){
        System.out.println("Hello, " + name);
    }

@PostConstructor & @PreDestroy

xml中可以配置一個bean的init方法和destroy方法,分別在bean被實例化和銷燬的時候調用。我們除了通過xml配置之外,還能通過註解來聲明哪個方法爲init方法,哪個方法爲destroy方法。
使用的註解分別爲:
@PostConstructor
@PreDestroy
例子:

package com.spring.annotation.di;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Car {
    //這兩行註解相當於@Resource(name="wheel")
    @Autowired
    @Qualifier("wheel")
    private Wheel wheel;
    public void show(){
        System.out.println("I'm a Car !");
        wheel.show();
    }
    @PostConstruct
    public void init (){
        System.out.println("Car init !");
    }
    @PreDestroy
    public void destroy(){
        System.out.println("Car destroy !");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章