1. 程式人生 > >使用Java代碼和註解完成Spring配置

使用Java代碼和註解完成Spring配置

spring 註解 Java配置 配置

使用Java代碼和註解完成Spring配置

使用Java代碼和註解進行Spring的配置。
個人理解:

  1. 使用Spring的時候可以使用XML文件配置,也可以使用Java代碼配置。使用Java代碼配置的時候,相當於使用Java類作為XML的配置文件,通過使用java代碼創建bean實例,並通過java代碼完成依賴的註入操作。當需要變更依賴註入時,可通過修改java代碼實現。

  2. 使用註解配置的時候,個人理解存在兩種方式第一種是註解+XML形式,第二種是Java代碼+註解形式。使用Java+註解的形式,相當於在Java代碼中指定組件掃描,在具體的bean中使用註解標識。
/*
  1.使用Java配置時,配置文件使用@Configuration修飾,在Java代碼中使用@Bean修飾
  2.在獲取bean時,通過AnnotationConfigApplicationContext獲取
  3.如果需要結合註解配置時,則使用@ComponentScan註解開啟掃描
 4.常用的註解有以下幾類
*/
@Controller
@Bean
@Service
@Component
@Repository
@Autowired

使用Maven插件或者命令行創建一個普通的Java工程

具體創建過程省略

?
創建包結構:
技術分享圖片
?

按照web層,service層,數據庫層的方式分別創建三個包
App.java是項目的啟動文件
JavaConfiguration.java java配置類,不使用註解
AnnotationConfiguration.jar java+註解的配置類

?

在Pom文件內添加Spring的依賴

?

 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.4.RELEASE</version>
    </dependency>

?
如果需要使用junit進行單元測試時,添加junit依賴

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
    </dependency>

代碼解析

使用Java配置+註解的方式

web、service、repository層的代碼分析

在包下分別創建文件:
每個類均有一個構造函數和一個addUser的方法構成。

?

web/UserController.java

在web層,模擬SpringMVC中的Controller層。此處使用@Controller註解配置此類,依賴項userService通過@Autowired自動裝配。

@Controller
public class UserController {
    public UserController(){
        System.out.println("UserController 構造");
    }

    @Autowired
    private UserService userService;

    /*
    public  UserController(UserService userService){
        this.userService = userService;
    }*/

    public void addUser(){
        System.out.println("Controller:UserController");
        this.userService.addUser();
    }
}

?

service/UserService.java

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    /*
    public UserService(UserRepository userRepository){
        this.userRepository = userRepository;
    }
    */

    public UserService(){
        System.out.println("UserService 構造");
    }

    public void addUser(){
        System.out.println("service:addUser ");
        this.userRepository.addUser();
    }
}

?

Repositroy/UserRepository.java

@Repository
public class UserRepository {

    public UserRepository(){
        System.out.println("UserRepository 構造");
    }

    public void addUser(){
        System.out.println("Repository:addUser");
    }
}

?
?

UserController依賴UserService,UserService依賴UserRepository。其中UserController使用註解@Controller修飾,請其交給Spring管理。UserService使用@Service修飾,UserRepository使用@Repository修飾。使用@Autowired自動裝配。
給類添加註解以後,相當於這幾個bean都有spring管理。

?

配置類代碼解析

AnnotationConfiguration.java
此文件是配置類,需要標註說明該文件是配置類,同時通過@ComponentScan指定進行組件掃描。

@Configuration
@ComponentScan
public class AnnotationConfiguration {

    /*
    @Bean("myController")
    MyController getMyController(){
        return new MyController();
    }*/
}

?

測試代碼解析

本實例中引入junit
TestApp.java
特別註意,需要使用:AnnotationConfigApplicationContext
其他的使用方式和XML配置是一致的,無區別。

public class TestApp {

    ApplicationContext applicationContext;

    @Before
    public void setUp(){
        applicationContext =
                new AnnotationConfigApplicationContext(AnnotationConfiguration.class);
    }

    @Test
    public void TestUserController(){
        UserController userController =
                (UserController)applicationContext.getBean("userController");

        userController.addUser();
    }

}

運行結果
技術分享圖片

先運行構造函數

總結

為什麽說是Java+註解的方式進行配置,使用Java代碼書寫配置類,相關Bean的配置使用的是註解。
其實還存在很多搭配:
java + java
xml + xml
xml + 註解

使用Java代碼和註解完成Spring配置