autowired

Spring autowire null pointer exception [duplicate]

丶灬走出姿态 提交于 2019-12-13 10:01:26
问题 This question already has answers here : Why is my Spring @Autowired field null? (16 answers) Closed 2 years ago . xml configuration - <bean id="DS" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name="driverClassName" value="${DB.DRIVERCLASS}" /> <property name="url" value="${TABLEMAINT.URL}" /> <property name="username" value="${TABLEMAINT.USER}" /> <property name="password" value="${TABLEMAINT.PASSWORD}" /> </bean> @Component class AbcDAO{ @Autowired

Autowired Bean in two classes not being updated after accessing it from the second class

老子叫甜甜 提交于 2019-12-13 08:34:41
问题 I have the following scenario: A bean Autowired in two classes, I populate the bean in one class, then I check the bean in the second class, it is not populated. Whereas, when I add a getter in the class where I populated the bean and call the getter, it returns the bean populated. Why can't I access that bean directly in the other class, shouldn't it be populated since it's acting as a singleton in spring context? A JUnit4 test class loading the spring context from an xml file: @RunWith

Spring @Autowired bean giving null

拜拜、爱过 提交于 2019-12-13 07:28:12
问题 In the same request at one part, I am getting NullPointerException and at other part of code it is ok. @Controller @RequestMapping(value="/event") public class EventController { @Autowired EventValidator eventValidator; @Autowired private EventService eventService; @InitBinder private void initBinder(WebDataBinder binder) { System.out.println(eventValidator.toString()); <<—— NULL HERE binder.setValidator(eventValidator); } @RequestMapping(value="/add_event",method = RequestMethod.POST

@Autowired bean is null in Test Listener class

北慕城南 提交于 2019-12-13 04:59:24
问题 This question was asked before Using Autowired in a TestExecutionListener class for BeforeClass junit however it wasn't answered. I am facing the same problem but haven't figured out the solution Example: I am getting null mapper. public class CustomExecutionListener extends AbstractTestExecutionListener { @Autowired private Mapper mapper; @Override public void beforeTestClass(TestContext testContext) {} ... some code... } Test Class: Note: AppConfig contains the Mapper Bean defined. @RunWith

Spring Boot - interfaces and implementations [duplicate]

血红的双手。 提交于 2019-12-13 04:54:20
问题 This question already has answers here : Why are interface variables static and final by default? (14 answers) Closed 4 years ago . Java/Spring newbie here. I've a question about auto wiring through SpringBoot. I have an interface , an implementation , a main class and a configuration class like so : ServiceInterface.java public interface ServiceInterface { static String serviceName = "service"; public void displayMessage(); public String getServiceName(); } ServiceImpl1.java public class

Why Autowired on irrelevant method is ignored

こ雲淡風輕ζ 提交于 2019-12-13 04:26:10
问题 What happens when you use @Autowired on irrelevant method, as @Test in Spring context: @Autowired @Test public void testDao() { assert("something", dao.read()); } It seems that nothing happens and Test succeeded, is there a way getting an error/warning in such case? For auto wiring a field I'm getting expected exception, e.g. @Autowired String nothing; Produces an exception: FAILED CONFIGURATION: @BeforeClass springTestContextPrepareTestInstance org.springframework.beans.factory

Autowiring issue with JUnit in a Spring Data JPA environment

China☆狼群 提交于 2019-12-13 04:12:14
问题 i have issues with @autowired in my unit tests. here are the relevant classes and xml files: The Service Class: @Service(value = "statusService") public class StatusService implements DefaultService<Status> { @Autowired(required = true) private StatusRepository statusRepository; public void save(Status value) { statusRepository.save(value); } } The Interface: public interface DefaultService<T> { @Transactional void save(T value); } (i also tested a non generic interface, but the result was

Spring autowire trouble with generic parameter

别等时光非礼了梦想. 提交于 2019-12-13 03:41:18
问题 I try to use generic parameter in autowire but it doesn't work. My goal is to create a generic JSON controller with Spring 4.1.3 and Hibernate I have a generic abstract class , and I use it to create service via a model objec(the same as my DAO) as generic parameter. The code of my AbstractService public interface IGenericService<T extends Serializable> extends IOperations <T>{} public interface IOperations<T extends Serializable> { T findOne(final long id); List<T> findAll(); void create

New instance from autowired class [duplicate]

陌路散爱 提交于 2019-12-13 02:58:45
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: getting new bean instances of referencing beans every time What is a very simple way to get a new instance of an autowired class every time? I've explored session, request, prototype, @resource. None seem to provide the desired effect. 回答1: Use @Scope("prototype") @Service @Scope("prototype") public class YourService{} 来源: https://stackoverflow.com/questions/14388480/new-instance-from-autowired-class

why is autowired bean null in filter in this spring application? [duplicate]

血红的双手。 提交于 2019-12-13 02:25:51
问题 This question already has answers here : How can I get a Spring bean in a servlet filter? (5 answers) Closed 4 years ago . I have simplified our production application to reproduce the errors. This is a demo application to calculate the counts of incoming requests. when I debugged the project, I can see Null Pointer Exception being thrown at manager.incrementRequestCounter(url) line in CounterMetricsFilter class shown below. That is because manager I have autowired is NULL. However, the same