使用注解开发

Maven依赖包引入

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

Spring Bean配置

我们在使用注解的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持。使用注解需要导入context约束,增加注解的支持。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:content="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. https://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. https://www.springframework.org/schema/context/spring-context.xsd">
  9. <!--指定要扫描的包,这个包下的注解就会生效-->
  10. <content:component-scan base-package="com.package.name" />
  11. <content:annotation-config/>
  12. </beans>

@Autowired

  • 自动装配,通过类型(byType)、名字(byName)
  • 如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value=”beanId”)

@Resource

  • 自动装配,通过名字(byName)、类型(byType)
  • 如果Resource不能唯一自动装配上属性,可以通过@Resource(name=”beanId”)

@Component

@Component有几个衍生注解,在web开发中,会按照MVC三层结构分层:

  • dao【@Repository】
  • service【@Service】
  • controller【@Controller】

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

xml与注解

  • xml更加万能,适用于任何场合,维护简单方便。
  • 注解不是自己类使用不了,维护相对复杂。

xml与注解最佳实践

  • xml用来管理bean。
  • 注解只负责完成属性的注入。


使用Java的方式配置Spring

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

实体类

  1. package com.kuang.pojo;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Component;
  4. // 使用@Component注解,就是说明这个类被Spring接管了,注册到了容器中
  5. @Component
  6. public class User {
  7. private String name;
  8. public String getName() {
  9. return name;
  10. }
  11. @Value("Harry")
  12. public void setName(String name) {
  13. this.name = name;
  14. }
  15. @Override
  16. public String toString() {
  17. return "User{" +
  18. "name='" + name + '\'' +
  19. '}';
  20. }
  21. }

配置类

  1. package com.kuang.config;
  2. import com.kuang.pojo.User;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.ComponentScan;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.context.annotation.Import;
  7. // 这个也会被Spring容器托管,注册到容器中,应为他本来就是一个Component
  8. // @Configuration代表这是一个配置类,就和之前的applicationContext.xml一样
  9. @Configuration
  10. @ComponentScan("com.kuang.pojo")
  11. @Import(WebConfig.class)
  12. public class AppConfig {
  13. // 注册一个Bean,就相当于之前写的一个bean标签
  14. // 这个方法的名字,就相当于bean标签中的id属性
  15. // 这个方法的返回值,就相当于bean标签中的class属性
  16. @Bean
  17. public User user() {
  18. return new User(); // 就是返回要注入到Bean的对象
  19. }
  20. }

测试类

  1. import com.kuang.config.AppConfig;
  2. import com.kuang.pojo.User;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5. public class UserTest {
  6. public static void main(String[] args) {
  7. // 如果完全使用了配置类方式去做,我们就只能通过AnnotationConfigApplicationContext上下文来获取容器,通过配置类的class对象加载。
  8. ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
  9. User user = (User) context.getBean("user");
  10. System.out.println(user.getName());
  11. }
  12. }

这种纯Java的配置方式,在Spring boot中随处可见。