1、factorybean注解方式注入
factorybean即工厂bean,可以让开发者干预bean的创建过程。直接上代码
pom.xml
<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>com.mine</groupId>
<artifactId>spring-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- log -->
<!-- log4j通过slf4j来代理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</dependency>
<!-- apache commons logging通过slf4j来代理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<!-- java.util.logging 通过slf4j来代理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</dependency>
<!-- log -->
</dependencies>
</project>
BeanService
注意不要写service注解
package com.mine.factorybean.service;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Data
@Slf4j
public class BeanService {
private String serviceId;
public String test() {
log.info(serviceId);
return serviceId;
}
}
FactoryService
package com.mine.factorybean.service;
import java.util.UUID;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class FactoryService implements FactoryBean<BeanService> {
public BeanService getObject() throws Exception {
BeanService beanService = new BeanService();
beanService.setServiceId(UUID.randomUUID().toString());
log.info("创建bean对象,ServiceId:" + beanService.getServiceId());
return beanService;
}
public Class<BeanService> getObjectType() {
return BeanService.class;
}
public boolean isSingleton() {
return false;
}
}
FactoryBeanController
package com.mine.factorybean.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mine.factorybean.service.BeanService;
@RestController
@RequestMapping("factory")
public class FactoryBeanController {
@Autowired
private BeanService beanService1;
@Autowired
private BeanService beanService2;
@RequestMapping("getServiceId")
public String getServiceId(String type) {
if ("1".equals(type)) {
return beanService1.test();
} else {
return beanService2.test();
}
}
}
SpringApp
package com.mine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringApp {
public static void main(String[] args) {
SpringApplication.run(SpringApp.class, args);
}
}
请求下面两个地址,返回的serviceId是不同的,因为bean不是单例的。
http://localhost:8080/factory/getServiceId?type=1
http://localhost:8080/factory/getServiceId?type=2