spring注解創建對象


注解創建對象

(四者功能相同)

Component

Controller:一般用在表現層

Service:一般用在業務層

Repository:一般用在持久層

配置xml文件:

<?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
      https://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      https://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="文件所在包"></context:component-scan>
</beans>

示例:

@Component(value=“首字母小寫”)

(放在目標方法前)

Resource:

@Resouce(name=“首字母小寫”)

直接按照bean的ID注入,可以獨立使用

name用於指定bean的ID

只能注入其他bean類型的數據,而基本類型和String類型無法使用

value:

用於注入基本類型和String類型數據

用於指定數據的值,使用spring中的SpEL(也就是spring中的el表達式)

例:${表達式}

Scope:

@Scope(value=“singleton/prototype”)

單例多例

singleton prototype

PreDestroy:銷毀方法

PostConstruct:初始化方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM