1. 程式人生 > >@service註解得作用與注意事項

@service註解得作用與注意事項

 <context:component-scan base-package=”com.eric.spring”>其中base-package為需要掃描的包(含所有子包),多個包可以用逗號分隔。@Service用於標註業務層元件,@Controller用於標註控制層元件(如struts中的action),@Repository用於標註資料訪問元件,即DAO元件,而@Component泛指元件,當元件不好歸類的時候,我們可以使用這個註解進行標註。@Servicepublic class VentorServiceImpl implements iVentorService {}@Repository public class VentorDaoImpl implements iVentorDao {
}getBean的預設名稱是類名(頭字母小寫),如果想自定義,可以@Service(“aaaaa”)這樣來指定,這種bean預設是單例的,如果想改變,可以使用@Service(“beanName”) @Scope(“prototype”)來改變。可以使用以下方式指定初始化方法和銷燬方法(方法名任意):@PostConstructpublic void init() {}@PreDestroypublic void destory() {}