Eri*_*ric 4 java spring-mvc spring-boot
我有一个带有 @Service 和 @Scope("protoype") 的服务类。我希望该服务的行为类似于控制器类中的原型。我的使用方法如下:
@Controller
@RequestMapping(value="/")
public class LoginController {
@Autowired
private EmailService emailService;
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String register(){
System.out.println(emailService);
emailService.sendConfirmationKey();
}
@RequestMapping(value = "/resetKey", method = RequestMethod.POST)
System.out.println(emailService);
emailService.sendResetKey();
}
Run Code Online (Sandbox Code Playgroud)
这是服务类别:
@Service
@Scope("prototype")
public class EmailService {
@Autowired
private JavaMailSender mailSender;
public void sendConfirmationKey(){
...
}
public void sendResetKey(){
...
}
}
Run Code Online (Sandbox Code Playgroud)
我使用自动配置属性运行 spring boot。我比较“emailService”对象是否相同,并且得到相同的一个对象。这意味着 @Scope("prototype") 不能按预期与 @Service 一起工作。你看到这里有什么问题吗?我是否忘记添加一些代码?
编辑:回复@Janar,我不想使用额外的代码来使其工作,例如 WebApplicationContext 属性和额外的创建方法。我知道有一种更短的方法,仅使用注释。
您必须在注释中指定代理模式scope。
这应该可以解决问题:
@Service
@Scope(value="prototype", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class EmailService {}
Run Code Online (Sandbox Code Playgroud)
LoginController或者,您也可以定义as 原型。
| 归档时间: |
|
| 查看次数: |
10596 次 |
| 最近记录: |