在我最近一直在研究的一些大型项目中,选择其中一个(XML或Annotation)似乎变得越来越重要.随着项目的增长,一致性对于可维护性非常重要.
我的问题是,人们更喜欢什么.您更喜欢基于XML还是基于注释?或两者?每个人都在谈论XML配置地狱以及如何将注释作为答案,那么注释配置到底是什么?
我的公司一直在评估Spring MVC,以确定我们是否应该在下一个项目中使用它.到目前为止,我喜欢我所看到的内容,现在我正在查看Spring Security模块,以确定它是否可以/应该使用.
我们的安全要求非常基本; 用户只需提供用户名和密码即可访问网站的某些部分(例如获取有关其帐户的信息); 并且网站上有一些页面(常见问题解答,支持等),应该授予匿名用户访问权限.
在我创建的原型中,我一直在Session中为经过身份验证的用户存储"LoginCredentials"对象(其中只包含用户名和密码); 例如,某些控制器检查此对象是否在会话中以获取对登录用户名的引用.我正在寻找用Spring Security取代这个本土逻辑,这将有很好的好处,可以删除任何类型的"我们如何跟踪登录用户?" 和"我们如何验证用户?" 来自我的控制器/业务代码.
似乎Spring Security提供了一个(每个线程)"上下文"对象,可以从应用程序的任何位置访问用户名/主体信息...
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
Run Code Online (Sandbox Code Playgroud)
...在某种程度上,这个对象是一个(全局)单例,这似乎非常不像Spring.
我的问题是:如果这是在Spring Security中访问有关经过身份验证的用户的信息的标准方法,那么将Authentication对象注入SecurityContext的可接受方式是什么,以便在单元测试需要时可用于我的单元测试认证用户?
我是否需要在每个测试用例的初始化方法中进行连接?
protected void setUp() throws Exception {
...
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(testUser.getLogin(), testUser.getPassword()));
...
}
Run Code Online (Sandbox Code Playgroud)
这似乎过于冗长.有没有更简单的方法?
该SecurityContextHolder物体本身似乎非常联合国春天般的...
我们使用下面的代码为Spring bean注入属性文件中的属性.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:/my.properties"/>
</bean>
<bean id="blah" class="abc">
<property name="path" value="${the.path}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
有没有办法以编程方式访问属性?我正在尝试做一些没有依赖注入的代码.所以我想要一些像这样的代码:
PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer();
props.load("classpath:/my.properties");
props.get("path");
Run Code Online (Sandbox Code Playgroud) @GetMapping和之间有什么区别@RequestMapping(method = RequestMethod.GET)?
我在一些Spring Reactive示例中看到过,
@GetMapping而不是使用它@RequestMapping
我正在寻找在Spring MVC中绑定和转换数据的最简单和最简单的方法.如果可能,不进行任何xml配置.
到目前为止,我一直在使用PropertyEditors:
public class CategoryEditor extends PropertyEditorSupport {
// Converts a String to a Category (when submitting form)
@Override
public void setAsText(String text) {
Category c = new Category(text);
this.setValue(c);
}
// Converts a Category to a String (when displaying form)
@Override
public String getAsText() {
Category c = (Category) this.getValue();
return c.getName();
}
}
Run Code Online (Sandbox Code Playgroud)
和
...
public class MyController {
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Category.class, new CategoryEditor());
}
...
}
Run Code Online (Sandbox Code Playgroud)
它很简单:两个转换都在同一个类中定义,绑定很简单.如果我想在我的所有控制器上进行通用绑定,我仍然可以在我的xml配置中添加3行 …
我需要根据不同的当前环境配置文件编写不同的逻辑.如何从Spring获取当前的活动和默认配置文件?
我很难决定是否应该坚持使用Hibernate来完成一个新项目,或者尝试使用JPA和新的Spring Data实现.
Spring Data框架是针对具有适度查询要求的大型项目还是小型项目?
虽然我通过使用@Query注释确实看到了减少代码的优势,但您对动态查询有何看法?当你想要实现一个非常复杂的save()方法时呢?
文档说明要创建主存储库实现的自定义接口和实现,但是如果需要访问crud存储库本身的任何超级方法呢?crud存储库实现了自定义存储库 - 而不是相反.这似乎是一个奇怪的设计.
我很不确定这个框架是否能够应对复杂和大型应用程序的挑战.我从未遇到过Hibernate的许多问题,而且我正在考虑坚持使用旧的可靠而不是使用Spring Data JPA.
我该怎么办?如果我使用Spring Data JPA,我会遇到哪些无法预料的并发症和成本?
Is there a way to get the complete path value after the requestMapping @PathVariable values have been parsed?
这就是:
/{id}/{restOfTheUrl}应该可以解析/1/dir1/dir2/file.html成id=1和restOfTheUrl=/dir1/dir2/file.html
任何想法,将不胜感激.
我想将以下(工作)curl片段转换为RestTemplate调用:
curl -i -X POST -d "email=first.last@example.com" https://app.example.com/hr/email
Run Code Online (Sandbox Code Playgroud)
如何正确传递电子邮件参数?以下代码导致404 Not Found响应:
String url = "https://app.example.com/hr/email";
Map<String, String> params = new HashMap<String, String>();
params.put("email", "first.last@example.com");
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity( url, params, String.class );
Run Code Online (Sandbox Code Playgroud)
我试图在PostMan中制定正确的调用,我可以通过将body参数指定为正文中的"form-data"参数来使其正常工作.在RestTemplate中实现此功能的正确方法是什么?
spring ×10
java ×8
spring-mvc ×3
annotations ×1
data-binding ×1
hibernate ×1
jpa ×1
properties ×1
rest ×1
resttemplate ×1
security ×1
spring-4 ×1
unit-testing ×1
xml ×1