我一直试图设置一个非常简单的控制器/视图,但是无法使其工作.在我web.xml,我已经定义了一个<servlet>名为servlet-context.xml,运行正常.在servlet-context.xml,我设置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
<...other stuff in here... />
<mvc:annotation-driven />
Run Code Online (Sandbox Code Playgroud)
除其他事项外.我的理解是这就是使用@注释所需要的一切.
在我的控制器中,我有:
@RequestMapping(value="/student/{username}/", method=RequestMethod.GET)
public String adminStudent(@PathVariable String username, @RequestParam String studentid) {
return "student";
}
Run Code Online (Sandbox Code Playgroud)
在我student.jsp看来,我有:
<p>This is the page where you would edit the stuff for ${username}.</p>
<p>The URL parameter <code>studentid</code> is set to ${studentid}.</p>
Run Code Online (Sandbox Code Playgroud)
当我发出请求时http://localhost:8080/application/student/xyz123/?studentid=456,我得到了我期望的视图,但所有变量都是空白或为空:
<p>This is the page where you would edit the stuff for .</p>
<p>The URL …Run Code Online (Sandbox Code Playgroud) 我在Web应用程序中使用Spring 3.2,我想.properties在类路径中包含一个包含默认值的文件.用户应该能够使用JNDI定义.properties存储另一个的位置,该位置将覆盖默认值.
只要用户设置了configLocationJNDI属性,以下内容就可以正常工作.
@Configuration
@PropertySource({ "classpath:default.properties", "file:${java:comp/env/configLocation}/override.properties" })
public class AppConfig
{
}
Run Code Online (Sandbox Code Playgroud)
但是,外部覆盖应该是可选的,JNDI属性也应该是可选的.
目前我得到一个例外(java.io.FileNotFoundException: comp\env\configLocation\app.properties (The system cannot find the path specified)当JNDI属性丢失时).
如何定义.properties仅在设置JNDI属性(configLocation)时使用的可选项?这是否可能@PropertySource或有其他解决方案?
我想将Spring 4.1中提供的新注释和功能用于需要JMS侦听器的应用程序.
我仔细阅读了Spring 4.1 JMS改进帖子中的注释,但我仍然错过了我和设置应用程序之间的关系@JmsListener,DestinationResolver以及如何指示正确的Destination或Endpoint.
以下是@JmsListener的建议用法
@Component
public class MyService {
@JmsListener(containerFactory = "myContainerFactory", destination = "myQueue")
public void processOrder(String data) { ... }
}
Run Code Online (Sandbox Code Playgroud)
现在,我不能在我的实际代码中使用它,因为需要使用配置文件读取"myQueue" Environment.getProperty().
我可以设置一个合适的myContainerFactory DestinationResolver但主要是,DynamicDestinationResolver如果你不需要JNDI来查找app服务器中的队列而不需要做一些自定义的回复逻辑,你似乎只会使用它.我只是想了解Spring如何使用@JmsListener注释以参数化方式指示队列的名称.
在博客文章的下方,我找到了对此Configurer的引用:
@Configuration
@EnableJms
public class AppConfig implements JmsListenerConfigurer {
@Override
public void configureJmsListeners(JmsListenerEndpointRegistrar registrar) {
registrar.setDefaultContainerFactory(defaultContainerFactory());
SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
endpoint.setDestination("anotherQueue");
endpoint.setMessageListener(message -> {
// processing
});
registrar.registerEndpoint(endpoint);
}
Run Code Online (Sandbox Code Playgroud)
现在,这使得感一定量的,我可以看到,这将让我设定在一些外部串运行一个目标,但是这似乎是在冲突中使用@JmsListener,因为它似乎是压倒一切赞成注释endpoint.setMessageListener中上面的代码. …
我正在使用带有SpringBoot的Spring 4和带有Java配置的Spring-Web.
要让@PostConstructSpring在启动时执行带注释的方法,必须注册CommonAnnotationBeanPostProcessor上下文,否则将@PostConstruct被忽略.
在基于XML的Spring配置中,文档说要使用(在beans元素下)
<context:annotation-config/>
Run Code Online (Sandbox Code Playgroud)
我还看到了一个示例,其中注册是在单个bean的基础上完成的,如下所示:
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
Run Code Online (Sandbox Code Playgroud)
如果可能,我希望避免这种情况.我的项目不包含任何XML文件,并且在我的构建文件夹中没有为我生成任何文件.
目前,我的解决方案是用我的类来注释@ComponentScan,我认为这会导致Spring检测并注册@Components和@Beans.不知何故,这导致CommonAnnotationBeanPostProcessor被调用,虽然我不知道为什么,但它解决了我的问题!
(这个类有一个@Autowired属性,在启动时为null - 因此需要进行初始化@PostConstruct)
但同样,我的问题是,使用Java配置实现这一目标的正确方法是什么?谢谢!
(还是有点新的春天)
我需要的是在同一时间,服务方法@Scheduled和@Transactional,让我得到调用DAO在里面.
声明式事务已启用,事务管理器org.springframework.orm.hibernate3.HibernateTransactionManager基于hibernate会话工厂.
服务类不实现任何接口,因此使用CGLIB代理.
这个设置一般工作正常(从Web堆栈调用的方法,即Struts),但是这个方法在调度程序调用时引发异常.
以下是相关的代码:
服务方法(调用类ClientWakeAndTerminateManager):
@Scheduled(initialDelay = 5000, fixedRateString = "${rmi.server.threads.clientsScheduleManagement.rate}")
@Transactional(readOnly = true)
public void runCheck(){
//Call a read-only DAO method (the DAO is @Autowired as a class field)
//do some stuff with the data loaded from DB
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序上下文的相关部分:
<!-- switch on the transactional infrastructure -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- Utility class to execute transactional code where use of annotation is not possible -->
<bean class="org.springframework.transaction.support.TransactionTemplate" id="txTemplate">
<constructor-arg …Run Code Online (Sandbox Code Playgroud) 我的问题是带有弹簧安全性的自定义注释的副本,但它没有得到答复,我相信应该有一个简单的问题解决方案.
基本上不是做:
@PreAuthorize("hasPermission(T(fully.qualified.Someclass).WHATEVER, T(fully.qualified.Permission).READ")
Run Code Online (Sandbox Code Playgroud)
我想要做:
@PreAuthorize(Someclass.WHATEVER, Permission.READ)
Run Code Online (Sandbox Code Playgroud)
或者可能是一些自定义注释,可以很容易地与弹簧安全连接
这对我来说似乎更清洁,如果可以,我希望能够做到这一点.
java spring authorization spring-security spring-annotations
我正在尝试设置一个REST端点,允许通过他们的电子邮件地址查询用户.电子邮件地址是路径的最后一部分,因此Spring将其foo@example.com视为值foo@example并截断扩展名.com.
我在这里发现了一个类似的问题Spring MVC @PathVariable with dot(.)被截断
但是,我有一个基于注释的配置使用AbstractAnnotationConfigDispatcherServletInitializer和WebMvcConfigurerAdapter.由于我没有xml配置,这个解决方案对我不起作用:
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="useDefaultSuffixPattern" value="false" />
</bean>
Run Code Online (Sandbox Code Playgroud)
我也试过这个使用正则表达式的解决方案,但它也没有用.
@RequestMapping(value = "user/by-email/{email:.+}")
Run Code Online (Sandbox Code Playgroud)
有没有人知道如何在没有xml的情况下关闭后缀模式截断?
我正在使用Spring 1.0.7,它使用Spring 4.0.7.我使用@Value注释在我的类中自动装配属性.如果属性文件中不存在该属性,我想要一个默认值,因此,我使用":"来指定默认值.以下是示例:
@Value("${custom.data.export:false}")
private boolean exportData = true;
Run Code Online (Sandbox Code Playgroud)
如果属性文件中不存在属性,则应该为变量赋值false.但是,如果文件中存在属性,那么它也会分配默认值并忽略属性值.例如,如果我定义了诸如上述应用属性文件中提到的一个属性有这样的事情custom.data.export=true的话,价值exportData将仍然是假的,而应该是真正的理想.
谁能指导我在这里做错了什么?
谢谢
我想验证我的控制器中的请求参数之一。请求参数应该来自给定值列表之一,如果不是,则应该抛出错误。在下面的代码中,我希望请求参数 orderBy 来自@ValuesAllowed 中存在的值列表。
@RestController
@RequestMapping("/api/opportunity")
@Api(value = "Opportunity APIs")
@ValuesAllowed(propName = "orderBy", values = { "OpportunityCount", "OpportunityPublishedCount", "ApplicationCount",
"ApplicationsApprovedCount" })
public class OpportunityController {
@GetMapping("/vendors/list")
@ApiOperation(value = "Get all vendors")
public ResultWrapperDTO getVendorpage(@RequestParam(required = false) String term,
@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer size,
@RequestParam(required = false) String orderBy, @RequestParam(required = false) String sortDir) {
Run Code Online (Sandbox Code Playgroud)
我编写了一个自定义 bean 验证器,但不知何故这不起作用。即使我为查询 param 传递了任何随机值,它也不会验证并抛出错误。
@Repeatable(ValuesAllowedMultiple.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {ValuesAllowedValidator.class})
public @interface ValuesAllowed {
String message() default "Field value should …Run Code Online (Sandbox Code Playgroud) validation controller spring-annotations http-request-parameters spring-boot
任何人都可以解释我需要做些什么来实现我自己的注释,这将为我的网络请求添加功能?
例如:
@Controller
public class MyController {
@RequestMapping("/abc")
@RequiresSomeSpecialHandling
public void handleSecureRequest() {
}
}
Run Code Online (Sandbox Code Playgroud)
这@RequiresSomeSpecialHandling将是我自己的注释,导致在给定的Web请求之前或之后完成一些特殊工作/abc.
我知道在非常高的层次上我需要编写一个bean后处理器,扫描我的注释类,并在需要时注入自定义mvc拦截器.但是有没有简化此任务的快捷方式?特别是对于上面的两个例子.
提前致谢,
spring ×9
java ×5
spring-boot ×2
spring-mvc ×2
controller ×1
hibernate ×1
rest ×1
spring-3 ×1
spring-jms ×1
validation ×1