关闭

Spring @Autowired与@Inject

标签: javaSpring
476人阅读 评论(0) 收藏 举报
分类:

@Autowired

1、新建Calculateshort.java和Calculate.java

Calculateshort.java:

package com.mrw.springtest3;
public class Calculateshort {
    public void differ(double x,double y){
        System.out.println("x-y="+(x-y));
    }
}

Calculate.java:

package com.mrw.springtest3;
import org.springframework.beans.factory.annotation.Autowired;
public class Calculate {
    @Autowired           //@Autowired(required=false)
    private Calculateshort calculateshort;
    public void sub(double x,double y){
        System.out.println("x+y的和为:"+(x+y));
    }
    public void say(double x,double y){
        calculateshort.differ(x,y);
    }
}

@Autowired:如果beans.xml未装入calculateshort将抛出异常:

@Autowired:beans.xml未装入calculateshort不抛出上述异常

2、将Calculateshort和Calculate装入beans.xml中

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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
        <context:annotation-config></context:annotation-config> //必须加入不然Spring无法扫描到@Autowired
       <bean id="Calculate" name="Calculate"
      class="com.mrw.springtest3.Calculate">
    </bean>
    <bean id="calculateshort" name="calculateshort"
        class="com.mrw.springtest3.Calculateshort">
    </bean>
</beans>

3、编写CalculateTest.java进行测试

package com.mrw.springtest3;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import com.mrw.springtest1.SpringHello;
public class CalculateTest {
    @Test
    public void test() {

  1.         BeanFactory context=new XmlBeanFactory(new ClassPathResource("beans.xml"));
  2.        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");     

        Calculate calculate=(Calculate)context.getBean("Calculate");

        calculate.sub(100, 100);
        calculate.say(200, 100);

    }
}

使用1的话将抛出异常java.lang.NullPointerException

使用2的话能输出正确结果

4、运行结果


@Inject

首先必须加入javax.inject-1.jar

1、与上述的@Autowired的第1步一致,只需把Calculate中的@Autowired改为@Inject即可

2、与上述的@Autowired的第2步一致

3、与上述的@Autowired的第3步一致,细节也是一致

4、运行结果与@Autowired的一致


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:29134次
    • 积分:555
    • 等级:
    • 排名:千里之外
    • 原创:28篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论