spring

55 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第21天

spring

1、使用注解开发

在Spring4之后,要使用注解开发,必须要保证aop的包导入了

image-20221020143802926

使用注解需要导入约束,配置注解的支持!

     <?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>
 ​
  1. bean
  2. 属性如何注入
 //等价于<bean id="user" class="com.kuang.pojo.User"/>
 //@Component 组件
 ​
 @Component
 public class User {
 ​
     //相当于  <property name="name" value="白莲"/>
     @Value("白莲")
     public String name;
 }
 ​
  1. 衍生的注解 @Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!
  • dao 【@Repository】
  • service 【@Service】
  • controller 【@Controller】

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

  1. 自动装配
  • @Autowired:自动装配通过类型,名字。如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value = "xxx")去配置。
  • @Nullable 字段标记了了这个注解,说明这个字段可以为null;
  • @Resource:自动装配通过名字,类型。
  1. 作用域
 @Component
 @Scope("singleton")
 public class User {
 ​
     //相当于  <property name="name" value="白莲"/>
     @Value("白莲")
     public String name;
 }
 ​
  1. 小结 xml与注解: xml更加万能,适用于任何场合!维护简单方便 注解不是自己类使用不了,维护相队复杂! xml与注解最佳实践: xml用来管理bean; 注解只负责完成属性的注入; 我们在使用的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持
     <!--指定要扫描的包,这个包下的注解就会生效-->
     <context:component-scan base-package="com.kuang"/>
     <!--开启注解的支持    -->
     <context:annotation-config/>
 ​

2、使用Java的方式配置Spring

我们现在要完全不使用Spring的xml配置了,全权交给Java来做! JavaConfig是Spring的一个子项目,在Spring4之后,它成为了一个核心功能!

image-20221020155211605

实体类

 //这里这个注解的意思,就是说明这个类被Spring接管了,注册到了容器中
 @Component
 public class User {
     private String name;
 ​
     public String getName() {
         return name;
     }
 ​
     @Value("黑心白莲") //属性注入值
     public void setName(String name) {
         this.name = name;
     }
 ​
     @Override
     public String toString() {
         return "User{" +
                 "name='" + name + ''' +
                 '}';
     }
 }
 ​

配置文件

 // 这个也会Spring容器托管,注册到容器中,因为它本来就是一个@Component
 // @Configuration代表这是一个配置类,就和我们之前看的beans.xml
 @Configuration
 @ComponentScan("com.kuang.pojo")
 @Import(KuangConfig2.class)
 public class KuangConfig {
 ​
     // 注册一个bean,就相当于我们之前写的一个bean标签
     // 这个方法的名字,就相当于bean标签中id属性
     // 这个方法的返回值,就相当于bean标签中的class属性
     @Bean
     public User user(){
         return new User(); // 就是返回要注入到bean的对象!
     }
 }
 ​

测试类

 public class MyTest {
     public static void main(String[] args) {
 ​
         //如果完全使用了配置类方式去做,我们就只能通过 AnnotationConfig 上下文来获取容器,通过配置类的class对象加载!
         ApplicationContext context = new AnnotationConfigApplicationContext(KuangConfig.class);
 ​
         User user = context.getBean("user", User.class);
         System.out.println(user.getName());
     }
 }
 ​

这种纯Java的配置方式,在SpringBoot中随处可见!