[Spring]7.注解
使用注解的条件
与上文相同。
<?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:annotation-config/>
<context:component-scan base-package="com.wang"/>
</beans>
<context:component-scan base-package="com.wang"/>为自动扫描该包下的注解。
常用注解
@Component:放在类上,表示放入IoC容器。作为一个bean。
@Value:注入属性。
在MVC架构中
- Dao层用@Repository。
- Controller层使用@Controller
- Service层用@Service
例:

测试:

结果:


浙公网安备 33010602011771号