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

Spring回顾(二)注解实现IoC和DI

2017-09-03 10:03 302 查看

注解实现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 {

4000
//这两行注解相当于@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 !");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring 回顾 java ee ioc