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>
示例:
(放在目标方法前)
Resource:
@Resouce(name=“首字母小写”)
直接按照bean的ID注入,可以独立使用
name用于指定bean的ID
只能注入其他bean类型的数据,而基本类型和String类型无法使用
value:
用于注入基本类型和String类型数据
用于指定数据的值,使用spring中的SpEL(也就是spring中的el表达式)
例:${表达式}
Scope:
@Scope(value=“singleton/prototype”)
单例多例
singleton prototype