Tag: spring

如何在spring中引用另一个xml文件的bean

我有一个XML文件中定义的Spring bean。 我想从另一个XML文件引用它。 我该怎么办?

春豆 – 如何将null作为构造函数arg?

我定义了下面的bean: <bean id="myBean" class="com.me.myapp.Widget"> <constructor-arg name="fizz" value="null"/> <constructor-arg name="buzz" ref="someOtherBean" /> </bean> 当我运行我的应用程序时,Spring引发一个beanconfigurationexception: [java] Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myBean' defined in class path resource [spring-config.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.me.myapp.Widget]: Could not convert constructor argument value of type [java.lang.String] to required type […]

里面的文件是不可见的spring

所有 我用下面的MANIFEST.MF创build了一个jar文件: Manifest-Version: 1.0 Ant-Version: Apache Ant 1.8.3 Created-By: 1.6.0_25-b06 (Sun Microsystems Inc.) Main-Class: my.Main Class-Path: . lib/spring-core-3.2.0.M2.jar lib/spring-beans-3.2.0.M2.jar 在它的根目录下有一个名为my.config的文件,在我的spring-context.xml中被引用,如下所示: <bean id="…" class="…"> <property name="resource" value="classpath:my.config" /> </bean> 如果我运行的jar,一切看起来很好,除了加载该特定的文件: Caused by: java.io.FileNotFoundException: class path resource [my.config] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/work/my.jar!/my.config at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:205) […]

Hibernate表没有映射错误

我有一个Web应用程序使用Hibernate在数据库上进行CRUD操作。 我得到一个错误,说这个表没有被映射。 查看Java文件: 错误信息: org.springframework.orm.hibernate3.HibernateQueryException: Books is not mapped [SELECT COUNT(*) FROM Books]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: Books is not mapped [SELECT COUNT(*) FROM Books] at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:660) at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411) … Caused by: org.hibernate.hql.ast.QuerySyntaxException: Books is not mapped [SELECT COUNT(*) FROM Books] at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181) at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:111) at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93) … 这是我的DAO.java方法: public int getTotalBooks(){ return […]

在Spring MVC中传递date来请求参数

我是新的Spring MVC – 我试图从我的javascript作为请求parameter passing一个date 我的控制器看起来像 – public @ResponseBody List<RecordDisplay> getRecords( @RequestParam(value="userID") Long userID, @RequestParam(value="fromDate") Date fromDate, @RequestParam(value="toDate") Date toDate) { 我现在的问题是如何从JavaScript调用 – 如应该在什么样的url for eg. – /getRecords?userID=1&fromDate=06022013&toDate=08022013' 我是否需要一种方法来parsingdate,以便Spring可以识别它?

如何使用@RunWith和@ContextConfiguration注解的jUnittesting中访问Spring上下文?

我有以下的testing课 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/services-test-config.xml"}) public class MySericeTest { @Autowired MyService service; … } 是否有可能通过编程方式访问services-test-config.xml中的某个方法? 喜欢: ApplicationContext ctx = somehowGetContext();

从ProceedingJoinPoint获取java.lang.reflect.Method?

问题是简短的:有没有办法从apsectj ProceedingJoinPoint获取Method对象? 目前我在做 Class[] parameterTypes = new Class[joinPoint.getArgs().length]; Object[] args = joinPoint.getArgs(); for(int i=0; i<args.length; i++) { if(args[i] != null) { parameterTypes[i] = args[i].getClass(); } else { parameterTypes[i] = null; } } String methodName = joinPoint.getSignature().getName(); Method method = joinPoint.getSignature() .getDeclaringType().getMethod(methodName, parameterTypes); 但我不认为这是要走的路

创造springrest服务,不用spring引导

我已经按照spring.io入门教程构build了REEST服务https://spring.io/guides/gs/rest-service/ 。 问题在于本教程仅解释如何使用spring引导来embeddedtomcat来生成独立运行的jar。 有没有办法从头开始创build一个项目来产生一个战争部署在一个已经存在的tomcat实例? PS:我已经find了一个以前的线程的Spring REST风格的服务作为一个战争而不是JAR在Tomcat的stackoverflow关于同样的问题。 问题是,接受的答案和build议并不完全解决我的问题,因为我不想find方法来修改独立应用程序弹簧引导项目,以便它可以在外部的Tomcat容器上工作,但希望find“干净”的解决scheme根本不涉及弹簧启动。 (我不确定如何在这里performance,在stackoverflow上还是比较新的,我希望能够提出一个新的问题是正确的方法)。

如何在Spring上下文中注入模拟

我有一个使用一些Spring上下文的testing。 在这些情况下,宣布了一些bean。 我希望testing使用上下文bean的实际实现,除了其中之一,我想要使用MOCK。 我试图使testingconfiguration组件(使用@Configuration注释),但XML似乎优先于@Bean注释,所以它不起作用,这样: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"context1.xml", "context2.xml", …}) @Configuration public class MyTest{ @Inject private MyTargetBean target; private AnotherBean myMock = mock(AnotherBean.class); @Bean public AnotherBean myMock() { return myMock; } ….. 我知道我可以在XML中定义Mocks,但为此我需要为每个testing添加一个额外的XML文件。 我想避免这种复杂性。 有没有办法在一个上下文中注入一个bean(像一个模拟),而不是通过XML? 谢谢!

Spring:确保一个特定的bean首先被初始化

我有一个库做运行时设置和configurationlog4j(没有log4j.properties或log4j.xml)。 我已经定义了一个名为MyLoggerFactory的类,我希望这是第一个使用spring初始化的bean。 我已经看到spring已经提出了一个问题来支持初始化的顺序,但是我想知道是否有一种方法将bean标记为第一个被spring容器初始化的bean?