原型 Bean 销毁

huangapple go评论68阅读模式
英文:

Prototype Bean Destruction

问题

我有以下的PROTOTYPE类。

  1. 我的DemoService对象会被Spring销毁吗?它会被垃圾回收吗?
  2. 我如何手动销毁DemoService,以便进行垃圾回收?
@Service
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS) 
public class DemoService {

@Autowired
private DaoCall daoCall;  //调用数据库。连接将会被连接池关闭。

@Autowired
private KafkaTemplate<String, String> template; //用于向Kafka发送消息
}

我正在将DemoService原型类注入到单例类GreetingController中。

@RestController
public class GreetingController {

@Autowired
private DemoService demoService;

@GetMapping("/greeting")
public String greeting()

{

demoService. //调用demoService

return "Hello ";
}
}
英文:

I have the below PROTOTYPE class .

1, Will my DemoService objects destroyed by spring?And will it be garbage collected?
2. How can i manually destroy the DemoService so it is Garbage Collected?

@Service
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS) 
public class DemoService {


@Autowired
private DaoCall daoCall;  //call to database.Connection will be closed by the connection pool.

@Autowired
private KafkaTemplate&lt;String, String&gt; template; //used to send message to Kafka
}

I am injecting DemoService prototype class in the singleton class GreetingController

@RestController
public class GreetingController {

@Autowired
private DemoService demoService;

	@GetMapping(&quot;/greeting&quot;)
	public String greeting()
	
	{
	
	demoService. //call to demoService
	
	return  &quot;Hello &quot;;
	}

答案1

得分: 2

如果您以这种方式使用原型bean,它只会在创建单例bean时创建一次,并且只会在Spring上下文关闭时销毁。

您需要使用ApplicationContext获取对它的引用:

public class GreetingController implements ApplicationContextAware {
    private ApplicationContext applicationContext;
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) 
      throws BeansException {
        this.applicationContext = applicationContext;
    }
    
    @GetMapping("/greeting")
    public String greeting() {
        DemoService demoService = applicationContext.getBean(DemoService.class);
        demoService. //调用demoService
        
        return "Hello ";
    }
}

然后,当您停止使用它时,它将像其他Java对象一样被垃圾回收。

还有许多其他方法可以获得原型bean的实例,请参阅 https://www.baeldung.com/spring-inject-prototype-bean-into-singleton

英文:

If you use a prototype bean this way it's only going to be created once when the singleton bean is created and it will only be destroyed when the spring context is shutdown.

You need to get a reference to it using the ApplicationContext

public class GreetingController implements ApplicationContextAware {
private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) 
      throws BeansException {
        this.applicationContext = applicationContext;
    }

    @GetMapping(&quot;/greeting&quot;)
    public String greeting()
    
    {
  
    DemoService demoService = applicationContext.getBean(DemoService.class);
    demoService. //call to demoService

    return  &quot;Hello &quot;;
    }
}

Then it will be garbage collected when you stop using it as any other java object.

Many more ways to get and instance of a prototype bean see https://www.baeldung.com/spring-inject-prototype-bean-into-singleton

huangapple
  • 本文由 发表于 2020年9月15日 06:47:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63892729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定