Using Java code and annotations to complete the spring configuration

Source: Internet
Author: User

Using Java code and annotations to complete the spring configuration

Use Java code and annotations for spring configuration.
Personal Understanding:

    1. You can use the XML file configuration when you use spring, or you can use Java code configuration. When using Java code configuration, it is equivalent to using a Java class as an XML configuration file, creating a bean instance by using Java code and completing the dependent injection operation through Java code. When a dependency injection needs to be changed, it can be implemented by modifying the Java code.

    2. When using annotation configuration, there are two ways of personal understanding: The first is the annotation +xml form, the second is the Java code + annotation form. Using the form of java+ annotations, it is equivalent to specifying component scans in Java code, using annotation identifiers in specific beans.
/*  1.使用Java配置时,配置文件使用@Configuration修饰,在Java代码中使用@Bean修饰  2.在获取bean时,通过AnnotationConfigApplicationContext获取  3.如果需要结合注解配置时,则使用@ComponentScan注解开启扫描 4.常用的注解有以下几类*/@Controller@Bean@Service@Component@Repository@Autowired
Create a common Java project using the MAVEN plugin or command line

Specific creation process omitted

?
To create a package structure:

?

Create three packages by Web tier, service tier, database tier, respectively
App.java is the startup file for the project
Javaconfiguration.java Java configuration class, not using annotations
Annotationconfiguration.jar configuration classes for java+ annotations

?

Adding spring dependencies within the Pom file

?

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

?
If you need to use JUnit for unit testing, add a junit dependency

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

How to use Java Configuration + annotations

Code Analysis for Web, service, repository layers

Create the files under the package separately:
Each class has a constructor and a AddUser method composition.

?

Web/usercontroller.java
At the Web layer, simulate the controller layer in the SPRINGMVC. @ControllerThis class is configured here using annotations, and the dependency userservice through @Autowired automatic assembly.

@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 relies on userservice,userservice to rely on userrepository. Where Usercontroller uses annotation @controller adornments, please give it to spring management. UserService uses @service adornments, Userrepository uses @repository adornments. Automatic assembly using @autowired.
After adding annotations to a class, the equivalent of these beans has spring management.

?

Configuring Class Code parsing

Annotationconfiguration.java
This file is a configuration class that needs to be annotated to indicate that the file is a configuration class and that the component scan is specified through @componentscan.

@Configuration@ComponentScanpublic class AnnotationConfiguration {    /*    @Bean("myController")    MyController getMyController(){        return new MyController();    }*/}

?

Test Code parsing

The introduction of JUnit in this example
Testapp.java
Special attention, need to use: Annotationconfigapplicationcontext
Other ways of using and XML configuration are consistent and no difference.

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();    }}

Run results

Run the constructor first

Summarize

Why java+ annotations are configured in a way that uses Java code to write configuration classes, and the related bean's configuration uses annotations.
In fact, there are many combinations:
Java + Java
XML + XML
XML + Annotations

Using Java code and annotations to complete the spring configuration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.