Java Bean不是自动布线
我目前有一个application-context.cml:Java Bean不是自动布线,java,spring,Java,Spring,我目前有一个application-context.cml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
default-autowire="byType">
<context:annotation-config />
<tx:annotation-driven />
<bean class="app.bl.facade.impl.DishFacadeImpl" />
我正在尝试自动连线它:
@Autowired
private DishFacade dishFacade;
public List<Dish> getEvents() {
System.out.println(dishFacade);
return dishFacade.getAll();
}
@Autowired
私人洗碗机正面;
公共列表getEvents(){
System.out.println(dishFacade);
返回dish facade.getAll();
}
我打印出来的结果是
null
,但我不知道为什么。假设您的包是app.bl.facade
,您必须将此标记放在应用程序上下文中。xml
<context:component-scan base-package="app.bl.facade" />
上面的XML标记将执行自动扫描。应创建为bean的每个类都使用正确的原型注释进行注释,如@Component
(对于普通bean)或@Controller
(对于servlet控制器)或@Repository
(对于DAO类)或者@Service
,这些类应该放在相同的基本包app.bl.facade
下
如果满足上述条件,spring将自动找到所有这些条件,并为每个条件创建一个bean。默认的自动布线模式为“byType”。对于这种类型的自动布线,使用类类型。因此,在Springbean配置文件中,应该只为这种类型配置一个bean
因此,您应该自动连接DishFacadeImpl
,而不是DishFacadeImpl
,我认为这是接口
可能会有帮助。Spring是否也在其中创建了DishFacade?我相信您需要添加
where is
您有setter方法吗?为什么他需要这个?因为他正在使用annotation config。他需要的是在DishFacade的setter上设置@Autowiring,或者让DishFacadeSpring bean的setter只自动连接到其他spring管理的bean,除非设置了外部setter或构造函数属性来执行注入过程。