Java代码配置注入
分为两种注入方式:构造注入、方法注入
1、构造注入
1)、定义Bean
定义Controller控制层
public class TestController {
// 被注入对象
private TestService testService;
// 测试注入方法
public String testController() {
return testService.testService();
}
// --------------------------------------------------
// 构造注入
public TestController(TestService testService) {
this.testService = testService;
}
}
定义Service服务层接口
public interface TestService {
// 测试注入:调用方法
String testService();
}
定义Service服务层接口实现
// 接口实现类
public class TestServiceImpl implements TestService {
// 被注入对象
private TestRepository repository;
// 测试注入方法
public String testService() {
return repository.testRepository();
}
// --------------------------------------------------
// 构造注入
public TestServiceImpl(TestRepository repository) {
this.repository = repository;
}
}
定义Repository数据层接口
public interface TestRepository {
// 测试注入:调用方法
String testRepository();
}
定义Repository数据层接口实现
// 接口实现类
public class TestRepositoryImpl implements TestRepository {
// 测试注入方法
public String testRepository() {
return "------------------------- >>> 获取数据成功!!";
}
}
2)、注入Bean
@Bean 修饰的方法名,也是 Bean 的别名
@Configuration
public class TestConfig {
@Bean
public TestController testController(TestService testService){
// 参数方式
return new TestController(testService);
}
@Bean
public TestService testService(){
// 直接写方法
return new TestServiceImpl(testRepository());
}
@Bean
public TestRepository testRepository(){
return new TestRepositoryImpl();
}
}
3)、完整测试代码
依赖
<properties>
<spring.version>4.2.1.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 依赖注入 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 仅测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
测试类
public class Test {
@org.junit.Test
public void test() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class);
TestController controller = context.getBean(TestController.class);
String data = controller.testController();
System.out.println("结果:" + data);
context.close();
}
}
结果
结果:------------------------- >>> 获取数据成功!!
2、方法注入
1)、定义Bean
定义Controller控制层
public class TestController {
// 被注入对象
private TestService testService;
// 测试注入方法
public String testController() {
return testService.testService();
}
// --------------------------------------------------
// 方法注入
public void setTestService(TestService testService) {
this.testService = testService;
}
}
定义Service服务层接口
public interface TestService {
// 测试注入:调用方法
String testService();
}
定义Service服务层接口实现
// 接口实现类
public class TestServiceImpl implements TestService {
// 被注入对象
private TestRepository repository;
// 测试注入方法
public String testService() {
return repository.testRepository();
}
// --------------------------------------------------
// 方法注入
public void setRepository(TestRepository repository) {
this.repository = repository;
}
}
定义Repository数据层接口
public interface TestRepository {
// 测试注入:调用方法
String testRepository();
}
定义Repository数据层接口实现
// 接口实现类
public class TestRepositoryImpl implements TestRepository {
// 测试注入方法
public String testRepository() {
return "------------------------- >>> 获取数据成功!!";
}
}
2)、注入Bean
@Configuration
public class TestConfig {
@Bean
public TestController testController(TestService testService) {
TestController controller = new TestController();
// 参数方式
controller.setTestService(testService);
return controller;
}
@Bean
public TestService testService() {
TestServiceImpl service = new TestServiceImpl();
// 直接写方法
service.setRepository(testRepository());
return service;
}
@Bean
public TestRepository testRepository() {
return new TestRepositoryImpl();
}
}
3)、完整测试代码
依赖(不变)
<properties>
<spring.version>4.2.1.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 依赖注入 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 仅测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
测试类(不变)
public class Test {
@org.junit.Test
public void test() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class);
TestController controller = context.getBean(TestController.class);
String data = controller.testController();
System.out.println("结果:" + data);
context.close();
}
}
结果(不变)
结果:------------------------- >>> 获取数据成功!!