环境
要用代码测试,首先要把环境弄好
首先新建一个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);
}
}
下面是重点了
建一个配置类,
同时配置类最好加注解@Configuration(虽然这个不加对以下测试不会造成影响)
@Configuration会声明这是一个配置类
public class staffConfig {
}
手动装配
没有任何东西,要怎么配置呢!,如果你有简单的理解一个spring,你就有一种简单的感觉
先试试手动添加bean
public class staffConfig {
@Bean
public IBase setStaff(){return new staff();}
}
没错,就这么简单。去测试一下就知道可不可以了。
在测试包建一个测试类
SpringJUnit4ClassRunner,以便在测试开始的时候自动创建Spring的应用上下文。注 解@ContextConfiguration会告诉它需要在staffConfig中加载配置。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = staffConfig.class) //导入配置
public class staffTest {
@Autowired
private IBase staff;
@Test
public void staffTest(){
staff.play();
}
}
测试,看见控制台输出没报错就行了。如以上没错bean装的实现差不多就是这个。单着这样确实很麻烦,所以spring还可以通过自动装配。
自动装配
在实体添加注解
@Component这个简单 的注解表明该类会作为组件类,并告知Spring要为这个类创建bean
@Component
public class staff implements IBase {
private String name = "小王";
@Override
public void play(){
System.out.println("name:"+name);
}
}
在config中添加注解 @ComponentScan 这个注解就是开启自动扫描,装配bean
@ComponentScan
public class staffConfig {}
依旧正确输出