記事のディレクトリ
環境
使用コードのテストは、我々は最初のものは、適切な環境にする必要があり
、それは言うまでも無い、まず新しいMavenプロジェクトを作成します
依存持って来られる必要
<?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>
次に、新しいディレクトリを作成し、私のディレクトリは、さあ掲載しました!
ああ!今、基本的にプロジェクトが始まった完成。テストにコードを使用するので、私はアセンブリ次のJavaコードを採用します。
新しいインターフェイスクラスを作成します。
package cn.entity;
public interface IBase {
void play();
}
従業員のクラスを構築します
public class staff implements IBase {
private String name = "小王";
@Override
public void play(){
System.out.println("name:"+name);
}
}
ここでの焦点はあります
コンフィギュレーション・クラスを構築します
public class staffConfig {
}
マニュアルアセンブリ
何も、どのようにそれを設定するには!あなたは春の簡単な理解を持っている場合は、簡単に気持ちを持って
、手動でBeanを追加しようとします
public class staffConfig {
@Bean
public IBase setStaff(){return new staff();}
}
はい、それは簡単です。上のテストかどうかを知るために。
テストクラス構築するための検査キットで
検査が始まったときに自動的にSpringアプリケーションコンテキストを作成するには、SpringJUnit4ClassRunnerを。ご注意@ContextConfigurationは設定CDPlayerConfigをロードするためにそれを教えてくれます。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = staffConfig.class) //导入配置
public class staffTest {
@Autowired
private IBase staff;
@Test
public void staffTest(){
staff.play();
}
}
テストは、ライン上のコンソール出力エラーが表示されませんでした。真の豆をほぼ満たされているとして、これを達成するために。そのような単一本当に厄介、それはまた、自動的にアセンブリ春することができます。
自動組立
エンティティに注釈を追加
この単純な注釈クラスがコンポーネントクラスとして表示されます@Component、とSpring Beanを知らせるこのカテゴリのために作成されます
@Component
public class staff implements IBase {
private String name = "小王";
@Override
public void play(){
System.out.println("name:"+name);
}
}
このコメントに設定して注釈@ComponentScanを追加すると、自動的にスキャン開くことです、豆を組み立てます
@ComponentScan
public class staffConfig {}
それでも正しい出力