Spring(8) 使用注解开发

8.使用注解开发

 8.1使用注解定义 Bean

  Spring 提供了以下多个注解,这些注解可以直接标注在 Java 类上,将它们定义成 Spring Bean。

注解说明
@Component 该注解用于描述 Spring 中的 Bean,它是一个泛化的概念,仅仅表示容器中的一个组件(Bean),并且可以作用在应用的任何层次,例如 Service 层、Dao 层等。

使用时只需将该注解标注在相应类上即可。
@Repository 该注解用于将数据访问层(Dao 层)的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
@Service 该注解通常作用在业务层(Service 层),用于将业务层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
@Controller 该注解通常作用在控制层(如 Struts2 的 Action、SpringMVC 的 Controller),用于将控制层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。

  

  在Spring4之后,要使用注解开发,必须要保证aop的包导入,由于我是基于Maven实现

   <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.3.14</version>
   </dependency>

  使用注解需要导入context约束,增加注解的支持!

<?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/>
</beans>

 

  此时我们通过一个案例来看一下

  我们先搭建好环境

  

   Spring文件(这时候我们已经不需要在beans.xml文件中装配bean了)

<?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="com.luo"></context:component-scan>
    <context:annotation-config/>
</beans>

  通过在注解@Component标注在相应类上即可

  @Component等价于<bean id="user" class="com.luo.pojo.User"></bean>

  

  通过测试可以得出

  

 8.2 属性注入

  我们可以通过@Vaule来注入属性的值

 @Value 等价于  <property name="name" value="阿军"></property>

  

 8.3衍生注解

  @Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!

    Dao [@Repository]      就是在Dao层下的类上注解使用@Repository 功能和@Component一样

    Service [@Service]

    Controller [@Controller]

    这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配bean

8.4自动装配

  @Autowired:自动装配通过类型,名字
  如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value="xxx")
     @Nullable 字段标记了这个注解,说明这个字段可以为null
     @Resource:自动装配通过名字,类型
     @Component:组件,放在类上,说明这个类被Spring给管理了,就是bean!

8.5作用域

  @Scope(“prototype”)        在类上可以用@Scope使用定义像以往的作用域

8.6 小结

  Xml与注解:

       Xml更加万能,适用于任何场合!维护简单方便

       注解不是自己类使用不了,维护相对复杂!

  Xml与注解最佳实践

     Xml用来管理Bean;

     注解只负责完成属性的注入

     我们在使用的过程中,主需要注意一个问题:必须让注解生效,就需要开启注解的支持

 

  注意:扫描的是包,不是类。注解要写在类的上面

  <!--指定要扫描的包,这个包下的注解就会生效 -->

  <context:component-scan base-package="com.xxx.xxx"></context:component-scan>
 

 

posted @ 2022-05-11 12:25  十三加油哦  阅读(91)  评论(0)    收藏  举报