Tag: spring

在春季测试中请求范围豆

我想在我的应用程序中使用请求范围的bean。 我使用JUnit4进行测试。 如果我尝试在这样的测试中创建一个: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { “classpath:spring/TestScopedBeans-context.xml” }) public class TestScopedBeans { protected final static Logger logger = Logger .getLogger(TestScopedBeans.class); @Resource private Object tObj; @Test public void testBean() { logger.debug(tObj); } @Test public void testBean2() { logger.debug(tObj); } 使用以下bean定义: 我得到: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘gov.nasa.arc.cx.sor.query.TestScopedBeans’: Injection of resource fields failed; nested exception […]

如何处理MaxUploadSizeExceededException

当我上传大小超过允许的最大值的文件时,会出现MaxUploadSizeExceededExceptionexception。 我想在出现此exception时显示错误消息(如validation错误消息)。 如何在Spring 3中处理此exception以执行此类操作? 谢谢。

在我的网络应用程序中从spring获取“未找到线程绑定请求”错误

我在我的网络应用程序中收到“未找到线程绑定请求”错误,并希望得到一些帮助。 我正在尝试使用struts2 + spring + hibernate,并使用spring来管理hibernate会话工厂,并将hibernate会话注入我的struts操作。 我希望这是有道理的。 当应用程序启动时,没有错误,但是当我发出第一个Web请求时,它会弹出“未找到线程绑定请求”错误。 这是我的春季配置: 这是我的行动: package actions.events; import org.hibernate.Session; public class Listing { Session session; public void setHibernateSession(Session value) throws Exception { session = value; } public String execute() { return “success”; } } 我唯一的领导是,如果我删除上面的’setHibernateSession’函数,我不会得到错误,因为如果动作不需要一个(懒惰的实例化),假设spring不打扰创建会话。 这是例外: Unable to instantiate Action, actions.events.Listing, defined for ‘Listing’ in namespace ‘/events’Error creating bean with […]

spring-context.xml的位置

当我在tomcat上运行我的应用程序时,spring-context.xml文件位于 /WEB-inf/spring-context.xml 还行吧。 但运行junit测试我必须提供我的spring-test-context.xml的位置,如下所示: @ContextConfiguration(locations={“classpath:/spring-test-context.xml”}) 这种方法的唯一方法是文件位于 /src/spring-context.xml 如何让我的应用程序在同一位置找到我的spring-context文件? 那么它适用于junit睾丸并部署在tomcat上? 我尝试了这个,它给了我很多关于没有找到任何豆子的错误,但它并没有说它找不到文件.. classpath:/WEB-INF/spring-test-context.xml

spring web,security + web.xml + mvc dispatcher + Bean创建两次

我有如下的Web.xml: mvc-dispatcher org.springframework.web.servlet.DispatcherServlet 1 mvc-dispatcher / springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain /api/secure/* [编辑] 在我添加了spring security后,我收到了错误! java.lang.IllegalStateException:找不到WebApplicationContext:没有注册ContextLoaderListener? 然后我补充道 contextConfigLocation /WEB-INF/mvc-dispatcher-servlet.xml org.springframework.web.context.ContextLoaderListener 然后它似乎工作正常,但然后1)问题是豆被创建两次! 如果我只删除它: contextConfigLocation /WEB-INF/mvc-dispatcher-servlet.xml 但是留下然后Web应用程序根本不运行 [额外] 完整的Web.xml如下: Spring MVC Application mvc-dispatcher org.springframework.web.servlet.DispatcherServlet 1 mvc-dispatcher / springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain /api/secure/* contextConfigLocation /WEB-INF/mvc-dispatcher-servlet.xml org.springframework.web.context.ContextLoaderListener 这是我的mvc-dispatcher-servlet.xml

Hibernate:永远不会调用MyInterceptor#onFlushDirty

问题: 为什么永远不会调用MyInterceptor#onFlushDirty ? 我在xml配置中扩展AbstractEntityManagerFactoryBean MyEntityManagerFactoryBean public class MyEntityManagerFactoryBean extends AbstractEntityManagerFactoryBean implements LoadTimeWeaverAware { private Interceptor entityInterceptor; public Interceptor getEntityInterceptor() { return entityInterceptor; } public void setEntityInterceptor(Interceptor interceptor) { entityInterceptor = interceptor; } } MyInterceptor: public class MyInterceptor extends EmptyInterceptor { public MyInterceptor() { System.out.println(“init”); // Works well } // PROBLEM – is never called @Override […]

使用像Eclipse这样的进度条制作启动画面

我的主类从文件加载配置然后显示一个框架。 我想使用像Eclipse这样的进度条创建一个启动画面,以便在加载文件时进度会增加,并且在加载文件后浮动消失。 然后我的主框架被加载。 MainClass代码: public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( “classpath:/META-INF/spring/applicationContext.xml”); // splash with progress load till this file is loaded UserDao userDao = context.getBean(UserDao.class); isRegistered = userDao.isRegistered(); System.out.println(“registered: ” + isRegistered); if (isRegistered) { // progress finish and hide splash log.debug(“user is registered”); // show frame1 } else { […]

@Aspect方面的Spring autowired bean为null

我有以下弹簧配置: 然后我有一个方面: @Aspect public class SyncLoggingAspect { @Autowired private SimpleEmailSender simpleEmailSender @AfterReturning(value=”execution(* uk.co.mysite.datasync.polling.Poller+.doPoll())”, returning=”pusher”) public void afterPoll(Pusher pusher) { simpleEmailSender.send(new PusherEmail(pusher)); } } 这个方面有效(我可以在afterPoll上打一个断点)但是simpleEmailSender是null。 不幸的是,我无法找到有关其原因的明确文档。 (为了记录,我的simpleEmailSender bean存在并正确连接到其他类)以下事情让我困惑: 上下文:组件扫描应该是@Aspect? 如果它肯定是一个弹簧托管bean,那么自动assembly应该工作吗? 如果context:component-scan不是用于创建方面,那么我的方面是如何创建的? 我认为aop:aspectj-autoproxy只是创建一个beanPostProcessor来代理我的@Aspect类? 如果它不是一个Spring托管bean,它会如何做到这一点? 显然你可以告诉我,我不了解事情应该如何从头开始。

事务性保存而不调用更新方法

我有一个用@Transactional注释的方法。 我从Oracle DB中检索一个对象,更改一个字段,然后从该方法返回。 我忘了保存对象,但发现无论如何数据库都会更新。 的applicationContext 我的方法 @Transactional public void myMethod(long id) { MyObject myObj = dao.getMstAttributeById(id); myObj.setName(“new name”); //dao.update(myObj); } 我的问题是为什么MyObject被持久化到数据库?

Java SE + Spring Data + Hibernate

我正在尝试使用Spring Data + Hibernate启动Java SE应用程序,并且到目前为止完成了以下操作: 配置文件 @Configuration @PropertySource(“classpath:hibernate.properties”) @EnableJpaRepositories @EnableTransactionManagement public class JpaConfiguration { private static final String PROPERTY_NAME_DATABASE_DRIVER = “db.driver”; private static final String PROPERTY_NAME_DATABASE_PASSWORD = “db.password”; private static final String PROPERTY_NAME_DATABASE_URL = “db.url”; private static final String PROPERTY_NAME_DATABASE_USERNAME = “db.username”; private static final String PROPERTY_NAME_HIBERNATE_DIALECT = “hibernate.dialect”; private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = […]