有一种情况我想在一段预定时间后调用一个方法,比如30秒或5分钟.
需要在运行时配置时间,这意味着我应该能够动态地将时间从5分钟更改为3分钟.(以编程方式将计时器重置为新的时间间隔)
我正在使用Spring框架开发一个动态Web项目.
我检查了Quartz和Timer类,但它们似乎读取了诸如delay,repeatInterval等参数的XML配置.
我怎样才能做到这一点?Spring是否为此提供任何支持?
先感谢您.
我首先没有提到这个问题的关键组成部分:我在这里使用TestNG.
我有一个执行持久性的DAO层.它作为我的小网络应用程序的一部分工作得很好(我有一个经典的控制器,服务,DAO层设计).如果需要,我可以使用我的XML更新此问题.
我的服务层
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public GoodVibeUserDetails getUser(String username) throws UsernameNotFoundException {
GoodVibeUserDetails user = userDao.getDetailsRolesAndImagesForUser(username);
return user;
}
// more methods...
}
Run Code Online (Sandbox Code Playgroud)
我的DAO层
@Repository
public class UserDaoImplHibernate implements UserDao {
@Autowired
private SessionFactory sessionFactory;
// My methods using sessionFactory & "talking" to the Db via the sessionFactory
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试类
@Component
public class UserDaoImplHibernateTests{
@Autowired
private UserDao userDao;
private GoodVibeUserDetails user;
@BeforeMethod
public void beforeEachMethod() throws ParseException{
user = new …Run Code Online (Sandbox Code Playgroud) 我有以下配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:component-scan base-package="com.abc" />
<task:annotation-driven executor="executor"/>
<task:executor id="executor" pool-size="2"/>
</beans>
Run Code Online (Sandbox Code Playgroud)
然后是下面的课
public class SomeClassImpl implements SomeClass {
@Async
@Override // overridden from the interface
public void doSomething(){
System.out.println("doing something async");
}
}
Run Code Online (Sandbox Code Playgroud)
一个测试:
@ContextConfiguration("classpath:test-config.xml") // with the xml config above
@RunWith(value = SpringJUnit4ClassRunner.class)
public class SomeClassTest {
@Autowired
private SomeClass someClass;
@Test
public void testSomething() throws Exception {
System.out.println("Calling doSomething");
someClass.doSomething();
Thread.sleep(5000);
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,一切都按预期工作.但是当我调用someClass.doSomething()时,我附加了调试器以逐步执行实际发生的事情,我注意到以下内容:

为什么SimpleAsyncTaskExecutor正在创建4个线程?我知道如果我从task:annotation-driven …
今天我正在研究从STS仪表板下载的Spring MVC展示
我对Spring 3.2版本引入的@MatrixVariable新注释以及URI路径中的Matrix变量的使用有所怀疑.
在我的home.jsp视图中,我有以下链接:
<a id="matrixVar" class="textLink" href="<c:url value="/data/matrixvars;foo=bar/simple" />">Matrix variable</a>
Run Code Online (Sandbox Code Playgroud)
在这里阅读有关@MatrixVariable注释的官方文档:http://static.springsource.org/spring-framework/docs/3.2.0.M2/reference/html/mvc.html#mvc-ann-matrix-variables
我了解以下内容:
1)矩阵变量是一对2)我可以将这些变量放在任何URI路径段中
所以我对之前的链接有些怀疑:
点击previus链接时调用的URI是什么?
是/ spring-mvc-showcase/data/matrixvars/simple?!?!(使用spring-mvc-showcase只是应用程序名称)
如果我把"foo"矩阵变量放在其他位置,那会无动于衷吗?
例如,如果我以这种方式放置"foo"变量:
"/数据;富=酒吧/ matrixvars /简单的"
或以这种方式:
"/数据/ matrixvars /简单;富=栏"
它有同样的含义吗?
好的,现在这是通过单击链接处理生成的HTTP请求的控制器方法:
@RequestMapping(value="{path}/simple", method=RequestMethod.GET)
public @ResponseBody String withMatrixVariable(@PathVariable String path, @MatrixVariable String foo) {
return "Obtained matrix variable 'foo=" + foo + "' from path segment '" + path + "'";
}
Run Code Online (Sandbox Code Playgroud)
此方法使用URI模板模式加入URI的特定位置.
因此,如果我的URI是data/matrixvars/simple,并且因为我的整个类都被@RequestMapping("/ data")注释注释,我的控制器方法被注释为:@RequestMapping(value ="{path}/simple",method = RequestMethod.GET)这意味着什么?
路径URI模板变量与第二个URI位置(在当前URI中是矩阵变量)相关,因此该方法也可以处理如下URI: …
我有一个bean包含两个相同组件的自动装配实例:
@Component
public SomeBean {
@Autowired
private SomeOtherBean someOtherBean1;
@Autowired
private SomeOtherBean someOtherBean2;
...
}
Run Code Online (Sandbox Code Playgroud)
SomeOtherBean有一个原型范围:
@Component
@Scope("prototype")
public SomeOtherBean {
@Value("...")
private String configurable;
}
Run Code Online (Sandbox Code Playgroud)
每个自动装配的SomeOtherBean的可配置值需要不同,并且将通过属性占位符提供:
configurable.1=foo
configurable.2=bar
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想使用注释来指定可配置属性的值.
通过XML执行此操作很容易,但我想知道是否这样
我正在使用SpringMVC 3.1.3.如果@PathVariable最后有空格,则会截断/修剪它.有没有办法阻止修剪
@RequestMapping(value="deleteConfig/{id}/", method=RequestMethod.DELETE)
public @ResponseBody JsonResponse<?> deleteConfig(@PathVariable("id") String id)
Run Code Online (Sandbox Code Playgroud)
如果来自客户端的id最后有一个空格,比如 - "abc"或者甚至是"abc%20"那么我在控制器中得到的变量id只是"abc"而不是"abc"
你能建议一种方法来解决这个问题
我有一个定制的Spring,它捆绑在一个jar中,然后设置为我的Grails应用程序的依赖项.我在resoueces.groovy中使用importBeans语句加载bean的app-context
beans = {
importBeans('classpath:app-context-my-component.xml')
}
Run Code Online (Sandbox Code Playgroud)
将app-context-my-component.xml具有一定的bean定义,并以下线
<context:annotation-config />
<context:property-placeholder location="classpath:my-component.properties" />
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用Grails的组件使用注释@Component("myComponent").
Grails正在加载外部应用程序上下文.我知道这是因为我首先在classpath上没有.properties文件,并且在我的@Value声明中我们没有丢失属性的回退机制.
在Grails控制器中,组件用作
class MyController {
def myComponent
def someaction() {
myComponent.doSomething()
}
}
Run Code Online (Sandbox Code Playgroud)
结果是NullPointerException,即组件的自动装配根本不起作用.我尝试在控制器中使用@Autowired,但是这给了我一些奇怪的问题,我认为这不是我想要的道路.
正在使用的Grails版本是2.3.6 Spring组件也设置为使用Spring版本3.2.7以避免不兼容.
UPDATE
现在再次掌握这个问题的时间,我设置了Spring日志记录,以便弄清楚会发生什么.好吧,Spring上下文加载会产生大量的日志,但这就是我设法从整个过程中获取的内容
INFO xml.XmlBeanDefinitionReader Loading XML bean definitions from class path resource [app-context-my-component.xml]
INFO support.PropertySourcesPlaceholderConfigurer Loading properties file from class path resource [my-component.properties]
DEBUG framework.CglibAopProxy Unable to apply any optimisations to advised method: public myapp.external.MyComponent myapp.MyService.getMyComponentClient()
DEBUG framework.CglibAopProxy Unable to apply any optimisations to …Run Code Online (Sandbox Code Playgroud) 我有以下类头:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:WEB-INF/applicationContext.xml")
public class OwnerTerminalsControllerTest
Run Code Online (Sandbox Code Playgroud)
以及以下项目结构:
当我运行应用程序时,我看到以下堆栈跟踪:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94)
at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:259)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:261)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:219)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path …Run Code Online (Sandbox Code Playgroud) java configuration unit-testing spring-test spring-annotations
我正在尝试将Spring Security 3 @Secured(“ admin”)注释转换为与Spring Security 4兼容的方式。
这是我的 usersService.java
@PreAuthorize("hasAuthority('admin')")
public List<User> getAllUsers() {
return usersDao.getAllUsers();
}
Run Code Online (Sandbox Code Playgroud)
然后在security-context.xml我有:
<security:intercept-url pattern="/admin" access="permitAll" />
...
<security:global-method-security pre-post-annotations="enabled" />
Run Code Online (Sandbox Code Playgroud)
getAllUsers() 被一个叫 LoginController.java
@RequestMapping("/admin")
public String showAdmin(Model model) {
List<User> users = usersService.getAllUsers();
model.addAttribute("users", users);
return "admin";
}
Run Code Online (Sandbox Code Playgroud)
在mySql数据库中,有两个表,用户表和权限。authorities有两列,用户名和权限。administrator有权力admin。
现在,如果我尝试访问/admin,我将被重定向到/login,但是使用登录后administrator,我仍然会收到“访问被拒绝”的信息。
我想我一定错过了一些非常基本的东西,但是由于我是Spring的新手,所以我无法弄清楚。任何帮助,将不胜感激。谢谢。
更新:我尝试将注释更改为@PreAuthorize(“ hasRole('ROLE_ADMIN')”),并且还将mySql中的admin的“ authority”列从“ admin”更改为“ ROLE_ADMIN”,但它仍然给我403。没有这多少信心,因为这个错误之前,我不得不改变hasRole('admin')在securityContext.xml给hasAuthority('admin')。
有没有办法让IntelliJ将自动生成的JPA实体注释(@ Column,@ Id等)放在属性本身中,而不是在getter中?
有一个答案指出了适用于基于xml的配置的集线器问题,但是有两个后续注释(其中一个是我的),询问如何使用@Configuration(无xml)执行此操作,并且没有答案。
谢谢
spring ×8
java ×5
spring-mvc ×3
annotations ×1
grails ×1
jpa ×1
spring-test ×1
testing ×1
testng ×1
timer ×1
unit-testing ×1