繁体   English   中英

使用getter方法获取值时,应用于setter方法的@Resource注释返回null指针异常

[英]@Resource annotation applied on setter method is returning the null pointer exception when using getter method to get the value

I have a simple class called circle which has a property called center, I have a simple class called circle which has a property called center, on which i have applied @Resource annotation to inject the dependency from spring.xml but somehow the center value is不从 spring.xml 注入,这就是为什么我在获取值时得到 null 指针异常的原因。 我在 xml 中定义了一个 bean,其名称与 circle 的属性名称相同


     //Circle class:

        package org.devesh.learning.spring;

        import javax.annotation.Resource;

        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Qualifier;

public class Circle implements Shape {

    private Point center; 

    @Override
    public void draw() {

      System.out.println("Circle drawn");
      System.out.println("Circle center is : "+ center.getX() + "," + center.getY());       
    }

    public Point getCenter() {
        return center;
    }

    //@Autowired
    //@Qualifier("circle related")
    @Resource
    public void setCenter(Point center) {
        this.center = center;
    }

}


Point class:

package org.devesh.learning.spring;

public class Point {

    private int x;
    private int y;


    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }



}

spring.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
         https://www.springframework.org/schema/context/spring-context-3.2.xsd"
    xmlns:context="http://www.springframework.org/schema/context/spring-context-3.2.xsd">

     <bean id="circle" class="org.devesh.learning.spring.Circle">
     </bean>

       <bean id ="pointA"  class="org.devesh.learning.spring.Point">
      <property name="x" value="${pointA.pointX}"></property>
      <property name="y" value="${pointA.pointY}"></property> 
  </bean>


   <bean id = "center" class="org.devesh.learning.spring.Point"> 
      <property name="x" value="20"></property>
      <property name="y" value="0"></property> 
  </bean>


  <bean id = "pointC" class="org.devesh.learning.spring.Point">
      <property name="x" value="-20"></property>
      <property name="y" value="0"></property> 
  </bean>

   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="locations" value="pointconfig.properties"></property>  
 </bean>



</beans>  

您有多个相同点类型的 bean,通过其 ID 注入中心,@Resource(name="center")

如何通过 ID 注入 Spring 依赖项?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM