我想在我的控制器中使用带注释的原型bean.但是春天正在创造一个单身豆.这是代码:
@Component
@Scope("prototype")
public class LoginAction {
private int counter;
public LoginAction(){
System.out.println(" counter is:" + counter);
}
public String getStr() {
return " counter is:"+(++counter);
}
}
Run Code Online (Sandbox Code Playgroud)
控制器代码:
@Controller
public class HomeController {
@Autowired
private LoginAction loginAction;
@RequestMapping(value="/view", method=RequestMethod.GET)
public ModelAndView display(HttpServletRequest req){
ModelAndView mav = new ModelAndView("home");
mav.addObject("loginAction", loginAction);
return mav;
}
public void setLoginAction(LoginAction loginAction) {
this.loginAction = loginAction;
}
public LoginAction getLoginAction() {
return loginAction;
}
}
Run Code Online (Sandbox Code Playgroud)
速度模板:
LoginAction counter: ${loginAction.str}
Run Code Online (Sandbox Code Playgroud)
Spring config.xml启用了组件扫描:
<context:annotation-config /> …Run Code Online (Sandbox Code Playgroud) 这将是一个简单的,但我找不到它们和使用哪一个,如果我有两个lib包含在我的类路径中?
我经常使用apache HashCodeBuilder和EqualsBuilder使用反射来实现对象相等,但最近我的一位同事告诉我,如果实体包含许多属性,使用反射可能会导致巨大的性能损失.担心我可能使用了错误的实现,我的问题是,您更喜欢以下哪种方法?为什么?
public class Admin {
private Long id;
private String userName;
public String getUserName() {
return userName;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Admin)) {
return false;
}
Admin otherAdmin = (Admin) o;
EqualsBuilder builder = new EqualsBuilder();
builder.append(getUserName(), otherAdmin.getUserName());
return builder.isEquals();
}
@Override
public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder();
builder.append(getUserName());
return builder.hashCode();
}
}
Run Code Online (Sandbox Code Playgroud)
比.
public class Admin {
private Long id;
private String userName;
public String getUserName() {
return userName; …Run Code Online (Sandbox Code Playgroud) 我两者之间有点困惑.据我所知,两者都返回hibernate会话,SessionFactory.getCurrentSession()返回基于<property name="current_session_context_class">hibernate.cfg.xml中设置的属性的上下文会话我们不应该总是采用这种方法吗?
增加了什么附加价值SessionFactory.openSession()?
在Spring 3类型转换API中,首字母缩写"SPI"代表什么?
我正在尝试使用图像裁剪器依赖项作为 aws lambda 上传函数。它在我的本地机器()上完美运行node v14.17.3,但在将其作为 lambda fn 触发时失败并出现以下错误。
"errorType":
"Error"
"errorMessage":
"\nSomething went wrong installing the \"sharp\" module\n\nCannot find module '../build/Release/sharp-linux-x64.node'\nRequire stack:\n- /var/task/node_modules/sharp/lib/sharp.js\n- /var/task/node_modules/sharp/lib/constructor.js\n- /var/task/node_modules/sharp/lib/index.js\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js\n\nPossible solutions:\n- Install with the --verbose flag and look for errors: \"npm install --ignore-scripts=false --verbose sharp\"\n- Install for the current runtime: \"npm install --platform=linux --arch=x64 sharp\"\n- Consult the installation documentation: https://sharp.pixelplumbing.com/install"
"stack":
"Error: "
"Something went wrong installing the \"sharp\" module"
""
"Cannot find module '../build/Release/sharp-linux-x64.node'"
"Require stack:"
"- /var/task/node_modules/sharp/lib/sharp.js"
"- …Run Code Online (Sandbox Code Playgroud) 我想在两个表BookStock和Book之间进行一对多的映射,但是我得到以下异常
Exception in thread "main" org.hibernate.MappingException: Could not determine type for: java.util.List, at table: book_stock, for columns: [org.hibernate.mapping.Column(books)]
Run Code Online (Sandbox Code Playgroud)
bookStock实体是
@Entity
@Table(name = "book_stock")
public class BookStock {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long stock;
private List<Book> books;
@Column(name = "id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "stock")
public Long getStock() {
return stock;
}
public void setStock(Long stock) {
this.stock = stock;
}
@OneToMany
@PrimaryKeyJoinColumn …Run Code Online (Sandbox Code Playgroud) 我一直在阅读干净的代码簿,该代码簿指出该类不应暴露其数据的内部状态,只应暴露该行为.如果一个非常简单和愚蠢的java bean暴露了getter和setter的内部状态,是否值得删除它们并使私有成员公开?或者只是将类视为数据结构?
我在这里有一个非常天真的概念问题需要澄清.在我的应用程序中,我目前正在使用JSR 303 Hibernate Validator来验证带有@NotBlank和@NotNull注释的域模型,比如检查用户名,密码等.
public class Admin {
private Long id;
@NotBlank(message = "Username should not be null")
private String username;
@NotBlank(message = "Username should not be null")
private String password;
...
Run Code Online (Sandbox Code Playgroud)
但是为了验证像现有用户名这样的域逻辑,我仍然使用Spring的Validator接口
@Component
public class AdminValidator implements Validator {
...
@Override
public void validate(Object target, Errors errors) {
Admin admin = (Admin) target;
if (usernameAlradyExist())){
errors.rejectValue("username", null, "This user already exist's in the system.");
}
}
Run Code Online (Sandbox Code Playgroud)
在控制器中,我正在使用它们
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String register(@Valid @ModelAttribute("admin") Admin admin, …Run Code Online (Sandbox Code Playgroud) 我正在使用 REST 模板故意在请求 uri 中发送一个 %,例如 /items/a%b
String responseEntity = restTemplate.exchange("/items/a%b",
requestObj.getHttpMethod(), requestEntity, String.class);
Run Code Online (Sandbox Code Playgroud)
将restTemplate被转换此URI的endoding,它正在成为/items/a%25b这有意义的,因为在默认情况下,其余模板编码的URI。
我尝试UriComponent用于禁用 uri 的编码
UriComponents uriComponents = UriComponentsBuilder.fromPath("/items/a%b").build();
URI uri= uriComponents.toUri();
String responseEntity = restTemplate.exchange(uri,
requestObj.getHttpMethod(), requestEntity, String.class);
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为 uri 再次是进行编码的 URI 类型。我确定我没有以正确的方式使用 UriComponents。
如果有人能指出禁用编码的正确方法,我将不胜感激。
谢谢。
java ×5
spring ×4
spring-mvc ×4
hibernate ×3
aws-lambda ×1
coding-style ×1
hamcrest ×1
jpa ×1
mockito ×1
orm ×1
persistence ×1
resttemplate ×1
terminology ×1