在maven webapp中使用@Autowired时出现NullPointerException

时间:2017-03-13 01:46:30

标签: spring maven jersey

我正在尝试使用maven @Autowired将Jersey与Spring集成。当我从ApplicationContext获取对象时,我看到我的代码正在执行,但是当我尝试使用NullPointerException时,它会抛出我<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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:annotation-config /> <context:component-scan base-package="com.pack.resource" /> <bean id="person" class="com.pack.resource.Person"> <property name="name" value="SomeNamexxxx" /> </bean> 。以下是代码段:

的applicationContext.xml

package com.pack.resource;

 public class Person {

private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Person() {

}

 }

Person.java

package com.pack.resource;

 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;

 import org.springframework.beans.factory.annotation.Autowired;
 import javax.ws.rs.core.MediaType;
 import org.springframework.stereotype.Component;

 @Component
 @Path("/hello")
 public class Hello {

 @Autowired
 Person person;

@GET
@Produces(MediaType.TEXT_PLAIN)
public String getName() {

    return person.getName();

}

Hello.java

ApplicationContext.getBean("person").getName()

}

但是当我使用@Autowired时,它会给我bean属性中的实际值。 为什么import os # specify the command prompt command you need to be run command = 'netsh advfirewall firewall add rule name="BlockAIM" protocol=TCP dir=out remoteport=80 action=block' # assign variable to the command output = os.popen2(command) # To get output call output.read() 注释不能正常工作。请帮助我。 TIA!

0 个答案:

没有答案