java - setter方法上使用@Resource注解使用getter方法获取值时返回空指针异常

标签 java spring

我有一个名为circle的简单类,它有一个名为center的属性,我有一个名为circle的简单类,它有一个名为center的属性,我在其上应用了@Resource注释来注入(inject)来自spring.xml的依赖项,但不知何故中心值不是从 spring.xml 注入(inject)的,这就是为什么我在获取值时遇到空指针异常。 我有一个在 xml 中定义的 bean,其名称与圆的属性名称相同


     //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注入(inject)中心, @Resource(名称=“中心”)

How do I inject a Spring dependency by ID?

关于java - setter方法上使用@Resource注解使用getter方法获取值时返回空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58145791/

相关文章:

java - 构建命令失败,外部 native 问题 android studio

java - 如何在与 JTextPane 关联的 StyledDocument 中使用 onmouseover

java - BorderLayout - 添加另一个组件后子组件仍然可见

java - Spring & JNDI : locate resource platform independent

java - 如何在浏览器中显示Word文档文件而不是下载Spring MVC?

java - 如何从设置 Activity 管理 onBackPressed?

java - IIB - 从聚合消息中获取 DOM 节点

java - 如何在没有 XML 的情况下将 Camel 与 Spring 一起使用

java - Spring Security 在本地主机上工作,但在服务器上的 docker 中给出 '403 Forbidden'

Java Spring - 忽略 GET 上嵌套对象类中的 Null 值