Spring @Autowired与@Inject

@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的一致


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MRW456/article/details/49964253
文章标签: java Spring
个人分类: Spring
上一篇Spring4之Hello World
下一篇Spring @Configuration
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭