Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
带构造函数的Spring3.2bean无法初始化,因为使用@Required的注入属性_Spring_Spring Mvc_Spring Security_Spring Roo_Spring Webflow - Fatal编程技术网

带构造函数的Spring3.2bean无法初始化,因为使用@Required的注入属性

带构造函数的Spring3.2bean无法初始化,因为使用@Required的注入属性,spring,spring-mvc,spring-security,spring-roo,spring-webflow,Spring,Spring Mvc,Spring Security,Spring Roo,Spring Webflow,在Spring3.2项目中,我正在引导一个具有注入属性的服务bean。使用属性文件中的@Required@Value(…)注入属性。我确信spring上下文很好地识别了属性文件 请检查以下服务类别: @Service public class SomeService { private String aURIString; @Required @Value("${someUrl}") public void setaURIString(String aUR

在Spring3.2项目中,我正在引导一个具有注入属性的服务bean。使用属性文件中的@Required@Value(…)注入属性。我确信spring上下文很好地识别了属性文件

请检查以下服务类别:

@Service
public class SomeService {  
    private String aURIString;
    @Required 
    @Value("${someUrl}") 
    public void setaURIString(String aURIString) {
        this.aURIString = aURIString;
    }    
    private URI oamURI;
    public SomeService() throws Exception {
        super();
        try {
            oamURI = new URI(aURIString);
        } catch (URISyntaxException e) {
            throw new Exception("URISyntaxException created while creating a URI instance for url= "+aURIString);
        }
    }
}
另外,请在下面找到配置类

@Configuration
@EnableWebMvc
@PropertySource({ "classpath:filter.properties" })
@ComponentScan(basePackages = { "com.someOrg.SomePrj"  })
public class WebConfig extends WebMvcConfigurerAdapter {
    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        return new PropertySourcesPlaceholderConfigurer();
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
}
以及属性文件“filter.properties”

当我使用以下测试文件进行测试时

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {WebConfig.class})
public class RestTemplateTest {
    @Resource
    private SomeService someService;    
    @Test
    public void testService() {
    }   
}
我得到以下例外

java.lang.IllegalStateException: Failed to load ApplicationContext
at     org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:105)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:74)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someService' defined in file [C:\CVS_Checkouts\someSpace\SomePrj\target\classes\com\someOrg\SomePrj\SomeService\SomeService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.someOrg.SomePrj.SomeService.SomeService]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1013)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:959)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:128)
at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
... 25 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.someOrg.SomePrj.SomeService.SomeService]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1006)
... 41 more
Caused by: java.lang.NullPointerException
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
at com.someOrg.SomePrj.SomeService.SomeService.<init>(SomeService.java:26)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
... 43 more
排队

private String aURIString = "www.google.com"
不过效果不错

它也可以正常工作,如果我清空构造函数并执行以下命令,@Required属性将从属性文件中注入:

private String aURIString;
oamURI = new URI(aURIString);
指向成员方法而不是构造函数

我需要构造函数中的@Required属性


有人能告诉我们一种方法来确保上下文在调用@Service类构造函数之前注入@Required属性,这样当spring上下文调用服务构造函数时它就可用了吗?

没有办法让通过setter注入的值在构造函数中可用

你可以:

  • 请改用构造函数注入:

    @Autowired
    public SomeService(@Value("${someUrl}") String aURIString) throws Exception { ... }
    
  • 对于依赖于注入值的初始化代码,使用
    @PostConstruct
    而不是构造函数:

    @PostConstruct
    public void init() throws Exception { ... }
    
  • 在setter中转换注入值,而不是在构造函数中转换注入值:

    @Required 
    @Value("${someUrl}") 
    public void setaURIString(String aURIString) throws Exception {
        try {
            oamURI = new URI(aURIString);
        } catch (URISyntaxException e) {
            throw new Exception("URISyntaxException created while creating a URI     instance for url= "+aURIString);
        }
    }
    

无法在构造函数中使用通过setter注入的值

你可以:

  • 请改用构造函数注入:

    @Autowired
    public SomeService(@Value("${someUrl}") String aURIString) throws Exception { ... }
    
  • 对于依赖于注入值的初始化代码,使用
    @PostConstruct
    而不是构造函数:

    @PostConstruct
    public void init() throws Exception { ... }
    
  • 在setter中转换注入值,而不是在构造函数中转换注入值:

    @Required 
    @Value("${someUrl}") 
    public void setaURIString(String aURIString) throws Exception {
        try {
            oamURI = new URI(aURIString);
        } catch (URISyntaxException e) {
            throw new Exception("URISyntaxException created while creating a URI     instance for url= "+aURIString);
        }
    }
    

谢谢你的全面回答谢谢你的全面回答