我是春天的新手,我试图理解下面的概念.
假设这accountDAO是一个依赖AccountService.
场景1:
<bean id="accServiceRef" class="com.service.AccountService">
<property name="accountDAO " ref="accDAORef"/>
</bean>
<bean id="accDAORef" class="com.dao.AccountDAO"/>
Run Code Online (Sandbox Code Playgroud)
场景2:
<bean id="accServiceRef" class="com.service.AccountService" autowire="byName"/>
<bean id="accDAORef" class="com.dao.AccountDAO"/>
Run Code Online (Sandbox Code Playgroud)
在AccountService课堂上:
public class AccountService {
AccountDAO accountDAO;
....
....
}
Run Code Online (Sandbox Code Playgroud)
在第二种情况下,如何注入依赖?当我们说它是由Name自动装配时,它究竟是如何完成的.在引入依赖项时匹配哪个名称?
提前致谢!
Pau*_*zie 12
使用@Component和@Autowire,它是Spring 3.0的方式
@Component
public class AccountService {
@Autowired
private AccountDAO accountDAO;
/* ... */
}
Run Code Online (Sandbox Code Playgroud)
将组件扫描放在应用程序上下文中,而不是直接声明bean.
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com"/>
</beans>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11079 次 |
| 最近记录: |