導航:首頁 > 文件管理 > spring註解的配置文件

spring註解的配置文件

發布時間:2024-07-05 05:45:55

1. spring的常用註解是什麼

Spring Boot常用註解

1、@SpringBootApplication

替代 @SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan

2、@ImportAutoConfiguration

導入配置類,一般做測試的時候使用,正常優先使用@EnableAutoConfiguration

3、@SpringBootConfiguration

替代@Configuration

4、@ImportResource

將資源導入容器

5、@PropertySource

導入properties文件

6、PropertySources

@PropertySource 的集合

(1)spring註解的配置文件擴展閱讀:

Controller 該類為Controller。

RequestMapping 配置方法路徑等信息。

ResponseBody 返回值,例如JSON,XML。

PathVariable 獲取RESTFUL路徑中的值如 /company/{corpId}/dept/{deptId}。

RequestParam 獲取Request參數值如xxx?from=index_nav。

Component,Repository,Service。

一般用Repository,service用Service,需要多個service時,一般用Componet。

2. spring怎麼配置註解

@Repository註解:

1 package imooc_spring.test.anotation.myrepository;

2

3 import org.springframework.stereotype.Repository;

4

5 /**

6 * 指定id,默認為dAO,即類名首字母小寫,如果指定了名稱那麼只能ctx.getBean(指定名稱)來獲取bean

7 * 這個例子里就只能通過ctx.getBean("wyl)來獲取DAO 的實例了;

8 *

9 * @author Wei

10 */

11 @Repository("wyl")

12 public class DAO {

13 /**

14 * 返回x和y的乘積

15 *

16 * @param x

17 * @param y

18 * @return x*y

19 */

20 public int multi(int x, int y) {

21 return x * y;

22 }

23 }

復制代碼

@Component 註解:

復制代碼

1 package imooc_spring.test.anotation;

2

3 import org.springframework.stereotype.Component;

4 /**

5 * Component 註解

6 * @author Wei

7 *

8 */

9 @Component

10 public class TestObj {

11 public void SayHi(){

12 System.out.println(" Hi this is TestObj.SayHi()...");

13 }

14 }

復制代碼

@Controller註解悶吵:

復制代碼

1 package imooc_spring.test.anotation;

2

3 import org.springframework.stereotype.Controller;

4

5 @Controller

6 public class UserController {

7 public void execute(){

8 System.out.println(" UserController.execute()...");

9 }

10 }

復制代碼

@Repository註解:

復制代碼

1 package imooc_spring.test.anotation;

2

3 import org.springframework.stereotype.Repository;

4

5 //@Repository

6 @Repository("wyl_repo")

7 public class UserRepositoryImpl implements IUserRepository {

8 //模譽拍擬持久化層

9 慶罩羨 @Override

10 public void save() {

11 // TODO Auto-generated method stub

12 System.out.println(" UserRepositoryImpl.save()...");

13 }

14

15 }

復制代碼

@Service註解:

復制代碼

1 package imooc_spring.test.anotation;

2

3 import org.springframework.stereotype.Service;

4

5 @Service

6 public class UserService {

7 public void add(){

8 System.out.println(" UserService.add()...");

9 }

10 }

3. SpringBoot項目使用@PropertySource註解載入properties配置文件,但輸出對象值為null

不是要加上前綴?
@ConfigurationProperties(prefix = "Example")
@PropertySource("classpath:my.properties")//這是屬性文件路徑

4. java開發中spring註解的使用方法

註解方式李態配置(簡化XML配置文件)
組件自動掃描功能
首先需要在applicationContext.xml中添加<context:component-scan/>
a.掃描Bean組件的註解,替代xml中的<bean>元素的定義。
@Service 用於Service業務組件
@Control 用於Action控制組件
@Respository 用於DAO數據訪問組哪源源件
@Component 用於其他組件
Bean組件掃描到容器後,裂羨默認名字為類名(首字母小寫)
如果需要自定義名稱可以使用@Service("id名")
b.依賴注入的註解標記
@Resource 按名稱@Resource(name="id名")
@AutoWired 按名稱
@Autowired
@Qualifier("id名")
c.其他註解
@Scope 等價於<bean scope="">
@PostConstruct 等價於<bean init-method="">
@PreDestroy 等價於<bean destroy-method="">

閱讀全文

與spring註解的配置文件相關的資料

熱點內容
哪個網站能看片 瀏覽:750
大數據在稅務應用 瀏覽:18
等離子個圓怎麼編程 瀏覽:386
運營商的大數據 瀏覽:764
實戰java高並發程序設計視頻 瀏覽:52
程序員改造硬體 瀏覽:782
js監聽enter 瀏覽:929
硬碟壞了如何把d盤數據導出 瀏覽:900
a1528的運營商文件在哪 瀏覽:997
文件怎麼還原打開方式 瀏覽:527
matlab讀取ai矢量文件格式 瀏覽:370
windows7的文件夾組織結構是一種 瀏覽:230
ie6安裝程序無法安裝所有組件 瀏覽:795
貴陽大數據作文 瀏覽:418
用友u8c盤臨時文件 瀏覽:792
資料庫被刪除該怎麼辦 瀏覽:167
福州福大中宇大數據技術有限公司 瀏覽:687
iphone5s攝像頭為什麼會進灰塵 瀏覽:300
刷數據鍵是哪個 瀏覽:415
網站被ddos了不管會怎麼樣 瀏覽:347

友情鏈接