十一、使用注解方式注入属性
1.1删除applicationContext中employeeService对于sessionFactory的注入
<bean id="employeeService" class="com.myz.service.imps.EmployeeService"> </bean>
1.2在employeeService类中增加注解
//当我们给某个属性值增加了@Resource后,spring就会启动byName的方式注入此属性 @Resource private SessionFactory sessionFactory;
1.3启用注解扫描
<!-- 启用注解扫描 --> <context:annotation-config />
2.1同理,移除applicationContext中action对于employeeService的注入
<!-- 配置action 而且配置scope可以解决action的单例的问题--> <bean name="/login" class="com.myz.web.actions.LoginAction" scope="prototype"> </bean>
2.2在LoginAction中增加注解
public class LoginAction extends DispatchAction { @Resource private EmployeeServiceInterface employeeService; public EmployeeServiceInterface getEmployeeService() { return employeeService; } public void setEmployeeService(EmployeeServiceInterface employeeService) { this.employeeService = employeeService; }
2.3已经启用了注解扫描的话,就不用再启用了