Bean-depth study of the spring assembly - Code Test

surroundings

Use code testing, we must first make things right environment
First create a new maven project, it goes without say

Brought dependent needed

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.zzs.test</groupId>
  <artifactId>springtest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>springtest Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <!-- 用来设置版本号 -->
    <!-- spring版本号 -->
    <spring.version>5.1.5.RELEASE</spring.version>
  </properties>


  <!-- 用到的jar包 -->
  <dependencies>
    <!-- 单元测试 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <!-- 表示开发的时候引入,发布的时候不会加载此包 -->
      <scope>test</scope>
    </dependency>

    <!-- spring框架包 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

  <build>
    <finalName>PersonnelSSM</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-maven-plugin</artifactId>
          <version>9.4.14.v20181114</version>
          <configuration>
            <!-- <scanIntervalSeconds>10</scanIntervalSeconds> -->
            <httpConnector>
              <port>8080</port>
            </httpConnector>
            <webApp>
              <contextPath>/</contextPath>
            </webApp>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Then create a new directory, my directory posted Come on!
Here Insert Picture Description

Ok! Now basically completed the project began. Because to use the code to test, so I will adopt the following Java code assembly.

Create a new interface class

package cn.entity;

public interface IBase {
    void play();
}

Build a class of employees

public class staff implements IBase {
    private String  name = "小王";

    @Override
    public void play(){
        System.out.println("name:"+name);
    }
}

Here is the focus of the

Build a configuration class

public class staffConfig {

}

Manual assembly

Nothing, how to configure it! If you have a simple understanding of a spring, you have an easy feeling
to try to manually add the bean

public class staffConfig {
    @Bean
    public IBase setStaff(){return new staff();}
}

Yes, it's that simple. To know if the test on.

In the test kits to build a test class
SpringJUnit4ClassRunner, to automatically create a Spring application context when the test began. Notes @ContextConfiguration will tell it to load the configuration CDPlayerConfig.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = staffConfig.class)  //导入配置
public class staffTest {


    @Autowired
    private IBase staff;


    @Test
    public void staffTest(){
        staff.play();
    }

}

Test, did not see the console output error on the line. As true bean is almost filled to achieve this. Such a single really troublesome, it can also automatically spring assembly.

Automated assembly

Add annotations in the entity
@Component this simple annotation class will show as a component class, and inform the Spring bean to be created for this category

@Component
public class staff implements IBase {
    private String  name = "小王";

    @Override
    public void play(){
        System.out.println("name:"+name);
    }
}

Add annotations @ComponentScan in config in this comment is to open automatically scan, assemble bean

@ComponentScan
public class staffConfig {}

Still correct output

Published 14 original articles · won praise 12 · views 1754

Guess you like

Origin blog.csdn.net/weixin_43157543/article/details/104577849