導航:首頁 > 文件管理 > 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註解的配置文件相關的資料

熱點內容
怎麼把圖片加入視頻文件夾 瀏覽:734
滑鼠右鍵的壓縮文件 瀏覽:44
awr導出cad文件 瀏覽:925
參公文件去哪裡找 瀏覽:827
excel表批量日期設置成文件夾存放 瀏覽:90
如何把資料庫加入其中 瀏覽:661
編程除法怎麼取消取整 瀏覽:625
股票編程軟體哪裡有賣 瀏覽:503
access導入多個txt文件 瀏覽:917
大臉app安卓下載 瀏覽:439
怎麼休改文件名 瀏覽:989
cdr導出圖片不顯示文件名 瀏覽:761
pcdmis如何離線編程 瀏覽:201
微信推文插入文件 瀏覽:844
生產文件櫃圖片 瀏覽:244
如何設置微信數據增加 瀏覽:95
excel文件不再增大 瀏覽:613
ug編程刀路怎麼復制不用滑鼠選取 瀏覽:97
excel文件打開擴展名錯誤 瀏覽:10
阿里巴巴怎麼做數據包 瀏覽:442

友情鏈接