我在為Spring Boot中的SessionScope Bean設定屬性值(即shortname)時遇到麻煩。
這是我的類:
import java.util.Map。
public class LdapUser {
private String shortname = " ;
private Map<String,String> token = null;
private String id = "。
public LdapUser() {
}
public String getshortname() {
return shortname。
}
public void setshortname(String shortname) {
this.shortname = shortname;
}
...其余的獲取器和設定器
我的Bean定義在這里:
import xxx.controllers.SwitchController;
import xxx.isim.LdapUser。
import org.slf4j.Logger。
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean。
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope。
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext;
public RestTemplate restTemplate() {
return new RestTemplate()。
}
我在一個控制器中使用該Bean:
import xxx.errors.IsimConnectionException;
import xxx.isim.IsimConnection;
import xxx.isim.LdapUser;
import xxx.services.IsimRestApiService。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller。
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource。
import java.security.Principal;
public class HomeController {
private static final Logger log = LoggerFactory. getLogger(HomeController.class)。
IsimRestApiService isimConn;
LdapUser sessionScopedLdapUser。
public String index(Principal principal) throws IsimConnectionException {
Authentication authentication = (Authentication) principal。
/
if ((authentication.getPrincipal() != null) && (authentication.isAuthenticated()) {
//設定會話的短名。
String shortname = (String)authentication.getPrincipal() 。
sessionScopedLdapUser.setshortname(shortname); //<-----
我的Bean的shortname的值在帶有箭頭的一行之后仍然是空的,盡管我正確地得到了shortname的字串值,并且該值不是空的。你能不能指出我在設定豆類屬性值時做錯了什么。我按照這里的例子設定了SessionScope Bean
Update:
我也嘗試用autowired代替@Resource(name = "sessionScopedLdapUser"),但在執行sessionScopedLdapUser.setshortname(shortname);
@Autowired
LdapUser sessionScopedLdapUser
同時在日志中我可以看到LdapUser Bean實體被創建了三次。這怎么可能呢?
2021-09-21 10: 55:55,469 INFO [http-nio-8080-exec-4] xxx. config.RestTemplateClient。創建了LdapUser豆類實體
2021-09-21 10:57。 05,247 INFO [http-nio-8080-exec-4] xxx. config.RestTemplateClient。創建了LdapUser豆類實體
2021-09-21 10:58。 08,401 INFO [http-nio-8080-exec-4] xxx. config.RestTemplateClient。創建了LdapUser豆類實體
我們的想法是每個HTTP會話有一個bean。我真的很困惑,希望得到一些提示。我正在閱讀這篇文章,也許這是因為我正試圖將一個會話范圍的Bean注入到一個Singletone Bean中。
我的檔案結構是:
xxx
---auth
---config
---RestRemplateClient
---controllers
---HomeController
---錯誤
---isim
--- LdapUser
---服務
主應用程式
uj5u.com熱心網友回復:
感謝@M. Deinum的幫助,我終于搞清楚了。我在除錯器中查看欄位值,由于我查看的是代理而不是真正的物件,所以欄位值總是為空。
下面是在@Controller類中注入session scoped bean的代碼。它在@Service類中也以同樣的方式正確作業。
public class LdapUser {
private String shortname = "。
private Map<String,String> token = new HashMap<> ()。
private String id = " ;
public LdapUser() {
this.shortname = shortname;
this.token = token;
this.id = id;
}
public String getshortname() {
return 矮名。
}
public void setshortname(String shortname) {
this.shortname = shortname;
}
...其他的獲取器和設定器
我的Bean配置類:
@Configuration
public class RestTemplateClient {
Logger logger = LoggerFactory.getLogger(SwitchController.class)。
@Bean
public RestTemplate restTemplate() {
return new RestTemplate()。
}
@Bean[/span
@SessionScope[/span]。
public LdapUser sessionScopedLdapUser(){
logger.info("LdapUser bean instance created at "/span> LocalDateTime.now())。
return new LdapUser()。
}
我的控制器類:
@Controller
public class HomeController {
private static final Logger log = LoggerFactory. getLogger(HomeController.class)。
@Autowired
IsimRestApiService isimConn;
@Autowired[/span
LdapUser sessionScopedLdapUser。
@RequestMapping("/")
public String index(Principal principal) throws IsimConnectionException {
Authentication authentication = (Authentication) principal。
//System.out.println("******* USER IS " authentication.getPrincipal());
if ((authentication.getPrincipal() != null) && (authentication.isAuthenticated()) {
//設定會話的短名。
String shortname = (String)authentication.getPrincipal() 。
sessionScopedLdapUser.setshortname(shortname)。
uj5u.com熱心網友回復:
你可以按以下方式指定你的配置:-
import org.springframework.context.annotation.Scope;
import java.time.LocalDateTime;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component。
public class LdapUser {
private String shortName = "LdapUser會話范圍"。
//其他屬性
public LdapUser() {
System.out.println("LdapUser SessionScope Constructor Called at "/span> LocalDateTime.now())。
}
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName。
}
}
配置中:
@Configuration
public class RestTemplateClient {
Logger logger = LoggerFactory.getLogger(SwitchController.class)。
@Autowired
private LdapUser ldapUser;
@Bean[/span
public RestTemplate restTemplate() {
return new RestTemplate()。
}
public void setLdapUser(LdapUser ldapUser) {
this.ldapUser = ldapUser。
}
public LdapUser getLdapUser() {
return ldapUser。
}
}
然后進入你的控制器:-
@Controller
public class HomeController {
//其他代碼
@Autowired[/span
private RestTemplateClient restTemplateClient。
private static final Logger log = LoggerFactory. getLogger(HomeController.class)。
@Autowired
IsimRestApiService isimConn;
@RequestMapping("/")
public String index(Principal principal) throws IsimConnectionException {
Authentication authentication = (Authentication) principal。
/
if ((authentication.getPrincipal() != null) && (authentication.isAuthenticated()) {
//設定會話的短名。
String shortname = (String)authentication.getPrincipal() 。
restTemplateClient.getLdapUser().setShortName("LdapUser Session Scope Updated")。
//.... 其他代碼
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/332561.html
標籤:
上一篇:用隨機值對創建串列
