applicationcontext

How to create TestContext for Spring Test?

本小妞迷上赌 提交于 2019-12-03 11:47:06
问题 I have a relatively small Java library that implements a few dozen beans (no database or GUI). I have created a Spring Bean configuration file that other Java projects use to inject my beans into their stuff. I am now for the first time trying to use Spring Test to inject some of these beans into my junit test classes (rather than simply instantiating them). I am doing this partly to learn Spring Test and partly to force the tests to use the same bean configuration file I provide for others.

Failed to load ApplicationContext from Unit Test: FileNotFound

别来无恙 提交于 2019-12-03 08:14:58
问题 I am creating a Maven Spring project, which includes MVC, Data and Security. My Spring applicationContext-*.xml files are located at \src\main\resources\spring\ My TestCase is placed at \src\test\java\my\package\controller\ and its code is: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring/applicationContext.xml", "classpath:spring/applicationContext-jpa.xml", "classpath:spring/applicationContext-security.xml" }) public class MyControllerTest

Initializing Spring bean from static method from another Class?

被刻印的时光 ゝ 提交于 2019-12-03 04:04:55
问题 I was trying to create Hibernate Validator bean, and run into this problem creating a bean from static factory method in another Class. I found a Spring way to get my Validator bean initialized (solution at the bottom), but the problem itself remains unsolved. Validator is used as example case here. This is how I create the Validator instance in Java import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; ValidatorFactory factory =

How to create TestContext for Spring Test?

别等时光非礼了梦想. 提交于 2019-12-03 02:07:40
I have a relatively small Java library that implements a few dozen beans (no database or GUI). I have created a Spring Bean configuration file that other Java projects use to inject my beans into their stuff. I am now for the first time trying to use Spring Test to inject some of these beans into my junit test classes (rather than simply instantiating them). I am doing this partly to learn Spring Test and partly to force the tests to use the same bean configuration file I provide for others. In the Spring documentation is says I need to create an application context using the "TestContext"

Failed to load ApplicationContext from Unit Test: FileNotFound

纵然是瞬间 提交于 2019-12-02 23:31:13
I am creating a Maven Spring project, which includes MVC, Data and Security. My Spring applicationContext-*.xml files are located at \src\main\resources\spring\ My TestCase is placed at \src\test\java\my\package\controller\ and its code is: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring/applicationContext.xml", "classpath:spring/applicationContext-jpa.xml", "classpath:spring/applicationContext-security.xml" }) public class MyControllerTest extends TestCase { @Autowired private MyController myController; @Test public void myMethod_test() { } } When

Initializing Spring bean from static method from another Class?

旧城冷巷雨未停 提交于 2019-12-02 17:25:31
I was trying to create Hibernate Validator bean, and run into this problem creating a bean from static factory method in another Class. I found a Spring way to get my Validator bean initialized (solution at the bottom), but the problem itself remains unsolved. Validator is used as example case here. This is how I create the Validator instance in Java import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); This is how I

no bean named “userDao” is defined

那年仲夏 提交于 2019-12-02 13:30:03
问题 I got this exception. This is my code: ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:/servlet-context.xml"); UserDao userDao = (UserDao) context.getBean("userDao"); And this is my servlet-context.xml file located in /myproject/WebContent/WEB-INF/spring/appServlet/servlet-context.xml <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www

@Autowired is not working with jersey and spring

橙三吉。 提交于 2019-12-02 07:57:52
When I am running test at that time @Autowired is working but when I run the web app and try to fetch data at that time its throwing null pointer exception. this is my controller In this BuyerRepo is always null import com.retail.exception.InvalidIdException; import com.retail.model.Buyer; import com.retail.repository.BuyerRepo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("/buyer") @Component public class BuyerController { @Autowired private

Bean order when autowired into list

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 07:44:05
I have defined an interface IWorker and some implementations of it as WorkerA and WorkerB , both annotated with @Component . I then autowire them into my app via: @Autowired private List<IWorker> workers = new ArrayList<IWorker>(); From what does the order the workers are put into the list depend on? How can I let additional WorkerC and WorkerD (also implementations of IWorker ) not annotated with @Component be autowired into the same list via my applicationContext.xml ? Is the order of WorkerC and WorkerD from the xml preserved? Is there a rule I can rely on in which order worker A, B, C and

no bean named “userDao” is defined

这一生的挚爱 提交于 2019-12-02 07:28:34
I got this exception. This is my code: ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:/servlet-context.xml"); UserDao userDao = (UserDao) context.getBean("userDao"); And this is my servlet-context.xml file located in /myproject/WebContent/WEB-INF/spring/appServlet/servlet-context.xml <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi