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

來源:互聯網
上載者:User

標籤: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自動裝配。

@Controllerpublic 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

@Servicepublic 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

@Repositorypublic 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@ComponentScanpublic 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配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.