factorybean註解方式注入

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章