这是一个简单的Spring bean,其中的complexMsg字段之一是自动接线的。 但是在运行时,bean为空。 春季版本是3.2。 类似的问题已经在较早之前发布,其解决方案已经存在于我的代码中。 Bean代码:

public class GreetingServiceImpl implements IGreetingService {
private String simpleMsg = null;    
//Autowired annotation is equivalent to autowire="byType"
@Autowired
private ComplexMessage complexMsg ; 

public String getSimpleMsg() {
    return simpleMsg;
}
     ...

春季配置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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">    
<context:annotation-config />
<bean id="greetingBean" class="simple.GreetingServiceImpl">
    <property name="simpleMsg" value="HelloWorld!!" />  
</bean> 
<bean id="complexMsg" class="simple.ComplexMessage">
    <property name="receiver" value="Raka" />
    <property name="content" value="MerryChristmas" />
</bean> 
</beans>

主类:

public static void main(String[] args) {
    IGreetingService greetingSrvc = null;       
    BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("greetingConfig.xml"));
    greetingSrvc =(IGreetingService) beanFactory.getBean("greetingBean");

greetingSrvc.complexMsg为空。

===============>>#1 票数:0

可能您错过了组件扫描?

<context:component-scan base-package="simple"/>

如果此.class文件位于.jar文件中,则可以添加

@零件

除了

@服务

注解。

例如:

@Service @Component公共类Abc {}

===============>>#2 票数:0

我已经尝试过了,并且对我有用,我在这里使用了应用程序上下文。 您的bean.xml文件内容不适当,我将其更改了..让我知道其他信息..请参见下面的完整代码

beans.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">   
<context:annotation-config />
<bean id="greetingBean" class="com.kb.autowiring2.GreetingServiceImpl">

</bean> 
<bean id="complexMsg" class="com.kb.autowiring2.ComplexMessage">

</bean> 
</beans>

package com.kb.autowiring2;

public interface IGreetingService {
    public ComplexMessage getComplexMsg();

}


package com.kb.autowiring2;

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

public class GreetingServiceImpl implements IGreetingService {
    @Autowired
    private ComplexMessage complexMsg ;

    public ComplexMessage getComplexMsg() {
        return complexMsg;
    }

}


package com.kb.autowiring2;

public class ComplexMessage {

}


package com.kb.autowiring2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.kb.autowiring1.Person;

public class Client {

    public static void main(String[] args) {
         IGreetingService greetingSrvc = null;       
         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/kb/autowiring2/beans.xml");
            IGreetingService p = applicationContext.getBean("greetingBean",IGreetingService.class);
            System.out.println(p.getComplexMsg());


    }

}

===============>>#3 票数:0 已采纳

我真的认为您应该使用其他tan BeanFacotry ApplicationContext 原因是ApplicationContext实现了1个我认为是BeanPostProcessor而BeanFacotry没有的接口。 注意beanfactory缺少功能,例如@Transational。

  ask by kakoli translate from so

未解决问题?本站智能推荐: