日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring注解开发-Bean注册

發布時間:2025/1/21 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring注解开发-Bean注册 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、使用注解開發我們需要在applicationContext.xml文件中添加context標簽。
在配置文件中開啟注解掃描.

<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- <context:annotation-config /> --><!-- 下面的配置作用是指定spring掃描的包,它包含了context:annotation-config的功能,所以在開發中一般就不需要在配置上面的 --><context:component-scan base-package="cn.niwotaxuexiba" /></beans>

代碼中使用@Component注解,Service層使用@Service注解.

//@Component("userService") @Service("userService") // <bean id="userService" class="cn.niwotaxuexiba.annotation.UserServiceImpl"> public class UserServiceImpl implements IUserService {@Value("張三")private String name;//@Autowired //默認是按照類型來進行注入//@Qualifier("userDao")@Resource(name="userDao")private IUserDAO userDao;@Overridepublic void add() {// System.out.println("userService add.." + name);userDao.add();}@Value("張三")public void setName(String name){this.name=name;}}

在spring2.5后為@Component添加了三個衍生的注解(重點)
@Repository 用于DAO層
@Service 用于service層
@Controller 用于表現層
對于我們的bean所處在的位置可以選擇上述三個注解來應用,如果你的bean不明確位置,就可以使用@Component.

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Spring注解开发-Bean注册的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。