spring-ioc

Interfaces are annotated with @Component annotation in spring IoC/DI. What could be the reason?

不问归期 提交于 2019-12-20 05:25:35
问题 Some times interfaces are annotated with @Component annotation. Then my obvious reasoning was that classes that implement such interface will be treated as components as well. But if I am right that is not the case. So what is the purpose of @Component annotation on interfaces. 回答1: Annotating an interface with @Component is common for Spring classes, particularly for some Spring stereotype annotations : package org.springframework.stereotype; ... @Component public @interface Service {...} or

How to pass a String (Non managed bean) to a managed bean

放肆的年华 提交于 2019-12-13 03:57:59
问题 I have a spring batch job. There is a step that is calling the reader method. STEP @Bean public Step myStep(FlatFileItemWriter<String> writer, Processor processor, @Value("${com.tableName}") String myTableName) { return stepBuilderFactory.get("step1") .<MyBean, String> chunk(this.chuckSize) .reader(reader(myTableName, this.myRowMapper)) .processor(processor) .writer(writer) .build(); } READER Working @Bean public <T> JdbcCursorItemReader<T> reader(@Value("${com.tableName}") String tableName,

About AnnotationConfigApplicationContext in Spring framework

混江龙づ霸主 提交于 2019-12-13 02:44:52
问题 I have written the following simple stand-alone spring app: package com.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core

Proper way to inject parent class dependencies with Spring annotations

对着背影说爱祢 提交于 2019-12-12 11:08:45
问题 I have following code - Dao.java @Component public class Dao extends NamedParameterJdbcDaoSupport { } dbContext.xml <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db.driver}" /> <property name="url" value="${db.jdbc.url}" /> <property name="username" value="${db.user}" /> <property name="password" value="${db.password}" /> </bean> applicationContext.xml <context:component-scan base-package="com.kshitiz" /

What is exact meaning of Spring Singleton scope?

隐身守侯 提交于 2019-12-11 10:40:10
问题 I defined two beans for a same class with different id names in applicationContext.xml. It working perfectly, So I got confuse what is exact spring singleton scope mean. My code is below. I gone through some other similar questions in stackoverflow. But I am not clear because those not exactly same context and very long explanation. Please share me if you know what it is I read that 1. Single instance for container(context), -- In my case, is it creating two containers for my two instances?

AnnotationConfigApplicationContext.getBean returns a different bean, Spring

青春壹個敷衍的年華 提交于 2019-12-11 10:20:03
问题 I have a problem that I have a ClassA needs RoomService to be injected, and it works fine that I find in ClassA, the roomService's id is the same. While for some reason, I need roomservice to create room instance based on some input param for me, so I use below config to achieve this: @Configuration @EnableAspectJAutoProxy public class Application { private static ApplicationContext ctx = new AnnotationConfigApplicationContext(Application.class); public static ApplicationContext

Autowire doesn't work for custom UserDetailsService in Spring Boot

会有一股神秘感。 提交于 2019-12-11 03:38:35
问题 The spring boot apps starts up, tomcat runs, and then it throws an error before finally dying. Error Running my app gives me this Autowired error: 2016-07-29 02:18:24.737 ERROR 44715 --- [ restartedMain] o.s.boot.SpringApplication : Application startup failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could

Two beans with the same name results in ConflictingBeanDefinitionException despite using @Primary

你离开我真会死。 提交于 2019-12-07 12:19:09
问题 I have an application initializer class that is used to insert application specific data to database. @Component("applicationInitializer") public class ApplicationInitializer { @PostConstruct public void init(){ // some clever code here } } There is also DevApplicationInitializer class that is used to initialize database with some sample data on developer machine (this class is excluded when deploying production code). @Component("applicationInitializer") @Primary public class

How can I inject an instance of List in Spring?

戏子无情 提交于 2019-12-07 07:37:07
问题 What works Suppose I have a spring bean definition of an ArrayList: <bean id="availableLanguages" class="java.util.ArrayList"> <constructor-arg> <bean class="java.util.Arrays" factory-method="asList"> <constructor-arg> <list> <value>de</value> <value>en</value> </list> </constructor-arg> </bean> </constructor-arg> </bean> Now I can inject this into all kinds of beans, e.g. like this: @Controller class Controller { @Autowired public Controller(ArrayList<String> availableLanguages) { // ... } }

How to use Session Scoped Component in Controller

本秂侑毒 提交于 2019-12-06 06:39:29
问题 Count.java: @Component @Scope(value = "session",proxyMode = ScopedProxyMode.TARGET_CLASS) public class Count { Integer i; public Count() { this.i = 0; } Controller: @Controller public class GreetingController { @Autowired private Count count; @RequestMapping("/greeting") public String greetingForm(Model model) { if(count.i == null) i == 0; else i++; model.addAttribute("count",String.valueOf(count.i)); return "greeting"; } } But every i run this controller (/greeting), it always increase the i