[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

例:
image

测试:

image

结果:
image

posted @ 2021-11-24 22:01  从零开始学java_wxz  阅读(15)  评论(0)    收藏  举报