Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java setter方法在Spring中不起作用?_Java_Spring - Fatal编程技术网

Java setter方法在Spring中不起作用?

Java setter方法在Spring中不起作用?,java,spring,Java,Spring,我试图从上下文文件初始化setter方法,但它并没有按预期工作 //塞特法 public class Service { private long Id=0; public long getId() { return Id; } public void setId(long Id) { this.Id = Id; } } //调用方法 public class test { //...source code

我试图从上下文文件初始化setter方法,但它并没有按预期工作

//塞特法

public class Service {

    private long Id=0;

    public long getId() {
        return Id;
    }
    public void setId(long Id) {
        this.Id = Id;
    }

}
//调用方法

public class test {

    //...source code
    Service sercvi = new Service();
    System.out.println("******************ID"+sercvi.getId());
    .................................. 

}
Context.xml

    ..........................................
    ..........................................
    <bean id="Service"  class="com.test.Service.Service" > 
      <property name="Id" value="100"/> 
    </bean>
   ...........................................
   ..........................................
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
..........................................
...........................................
..........................................
下面是在web.xml文件中加载上下文文件 web.xml

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
....................
上下文定位
/WEB-INF/Context.xml
如果我运行应用程序,它总是为id返回0值,但我在context.xml中初始化了100。 为什么不检索该值? 如何从上下文文件初始化setter方法值? 还有,如何检索这些值


如何解决此错误?

您的
服务
实例正在手动创建
Spring
仅当bean由Spring上下文处理时才会触发注入

如何解决此错误

不要手动初始化bean,使用Spring上下文或Spring注入恢复它们

您可以将该bean注入另一个bean中:

@Component
public class FooComponent {
    @Autowired
    Service service;
    public void foo() {
        System.out.println(service.getId()); //prints the value injected by Spring
    }
}
或者直接从Spring上下文中恢复它

@Component
public class BarComponent {
    @Autowired
    BeanFactory beanFactory;
    void bar() {
        Service service = (Service)beanFactory.get("service");
        System.out.println(service.getId()); //prints the value injected by Spring
    }
}
还有另一个选项,可以让您手动创建bean,并通过使用
@Configurable
注释获取其中的所有Spring功能,如前所述


如果您想测试Springbean,请使用,但这更多地用于集成测试,而不是单元测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(location={"classpath:location/of/your/spring.xml"})
public class MySpringTest {
    @Autowired
    Service service;

    @Test
    public void fooTest() {
        System.out.println(service.getId());
        Assert.assertEquals(100, service.getId());
    }
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(location={"classpath:location/of/your/spring.xml"})
public class MySpringTest {
    @Autowired
    Service service;

    @Test
    public void fooTest() {
        System.out.println(service.getId());
        Assert.assertEquals(100, service.getId());
    }
}