Java 默认@Autowired不注入
这是一个简单的springbean,其中complexMsg字段之一是自动连接的。但是在运行时,bean是空的。Spring版本是3.2。 类似的问题已经在前面发布过,我的代码中已经有了解决方案。 Bean代码:Java 默认@Autowired不注入,java,spring,autowired,Java,Spring,Autowired,这是一个简单的springbean,其中complexMsg字段之一是自动连接的。但是在运行时,bean是空的。Spring版本是3.2。 类似的问题已经在前面发布过,我的代码中已经有了解决方案。 Bean代码: public class GreetingServiceImpl implements IGreetingService { private String simpleMsg = null; //Autowired annotation is equivalent to aut
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;
}
...
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"
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>
greetingSrvc.complexMsg为空。您可能错过了组件扫描 如果这个.class文件在一个.jar文件中,您可能可以添加 @组成部分 注释除了 @服务 注释 例如: @服务@Component公共类Abc{}
我已经试过了,它对我很有效,我在这里使用了应用程序上下文。 您的beans.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());
}
}
我真的认为你应该使用ApplicationContext,而不是tanbeanfacotry。原因是ApplicationContext实现了1个接口,我认为它是BeanPostProcessor,而BeanFactory没有。注:beanfactory缺少@Transational等功能。XmlBeanFactory是罪魁祸首。改为使用ClassPathXmlApplicationContext。此外,自动连线依赖项没有默认构造函数。终于开始工作了。
<?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());
}
}