Spring学习随记2
spring-IOC之依赖注入
官网地址
XML篇
POM依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
代码结构
使用@Configuration,@Bean注解代码实现
package com.csh.test.project.annotation.configure;
import com.csh.test.project.annotation.service.EmployeeService;
import com.csh.test.project.annotation.service.ManagementService;
import com.csh.test.project.annotation.service.impl.EmployeeServiceImpl;
import com.csh.test.project.annotation.service.impl.ManagementServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public EmployeeService employeeService() {
return new EmployeeServiceImpl();
}
@Bean
public ManagementService managementService(EmployeeService employeeService) {
final ManagementServiceImpl managementService = new ManagementServiceImpl();
managementService.setEmployeeService(employeeService);
return managementService;
}
}
package com.csh.test.project.annotation.service.impl;
import com.csh.test.project.annotation.service.EmployeeService;
public class EmployeeServiceImpl implements EmployeeService {
public EmployeeServiceImpl() {
System.err.println("EmployeeServiceImpl constructor");
}
@Override
public void print() {
System.err.println("EmployeeServiceImpl print!!!");
}
}
package com.csh.test.project.annotation.service.impl;
import com.csh.test.project.annotation.service.EmployeeService;
import com.csh.test.project.annotation.service.ManagementService;
public class ManagementServiceImpl implements ManagementService {
public void setEmployeeService(EmployeeService employeeService) {
employeeService.print();
}
public ManagementServiceImpl() {
System.err.println("ManagementServiceImpl");
}
}
package com.csh.test.project.annotation.service;
public interface EmployeeService {
void print();
}
package com.csh.test.project.annotation.service;
public interface ManagementService {
}
package com.csh.test.project.annotation;
import com.csh.test.project.annotation.configure.AppConfig;
import com.csh.test.project.annotation.service.ManagementService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
ctx.getBean(ManagementService.class);
}
}
使用@Configuration,@ComponentScan,@Component/@Service/@Repository,@AutoWired注解代码实现(接口类/App.java同上)
package com.csh.test.project.annotation.configure;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = {"com.csh.test.project.annotation"})
public class AppConfig {
}
package com.csh.test.project.annotation.service.impl;
import com.csh.test.project.annotation.service.EmployeeService;
import org.springframework.stereotype.Repository;
@Repository
public class EmployeeServiceImpl implements EmployeeService {
public EmployeeServiceImpl() {
System.err.println("EmployeeServiceImpl constructor");
}
@Override
public void print() {
System.err.println("EmployeeServiceImpl print!!!");
}
}
package com.csh.test.project.annotation.service.impl;
import com.csh.test.project.annotation.service.EmployeeService;
import com.csh.test.project.annotation.service.ManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class ManagementServiceImpl implements ManagementService {
@Autowired
public void setEmployeeService(EmployeeService employeeService) {
employeeService.print();
}
public ManagementServiceImpl() {
System.err.println("ManagementServiceImpl");
}
}
拓展
package com.csh.test.project.annotation;
import com.csh.test.project.annotation.configure.AppConfig;
import com.csh.test.project.annotation.service.ManagementService;
import com.csh.test.project.annotation.service.impl.AfterRegisterTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.register(AfterRegisterTest.class);
ctx.refresh();
ctx.getBean(ManagementService.class);
ctx.getBean(AfterRegisterTest.class);
}
}
@Qualifier注解的使用
可标记字段、方法、参数、类/接口/枚举、自定义注解
@Qualifier和@Component/@Service/@Repository结合标注类
@Qualifier和@Autowired结合标记引用哪个类
@Repository
@Qualifier("1")
public class EmployeeServiceImpl implements EmployeeService
@Repository
@Qualifier("2")
public class EmployeeServiceImpl2 implements EmployeeService
@Repository
public class ManagementServiceImpl implements ManagementService {
@Autowired
@Qualifier("2")
public void setEmployeeService(EmployeeService employeeService) {
employeeService.print();
}