spring-ioc

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

孤者浪人 提交于 2019-12-05 21:44:34
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 DevApplicationInitializer extends ApplicationInitializer { @PostConstruct @Override public void init(){ super.init();

How can I inject an instance of List in Spring?

∥☆過路亽.° 提交于 2019-12-05 10:45:38
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) { // ... } } This works great. How it breaks However if I change my controller a tiny bit and use the type List

Environment abstraction

允我心安 提交于 2019-12-05 07:32:34
##Environment abstraction (环境抽象) Environment是容器重要的抽象,它集成了应用两个重要的方面:profiles和properties; 一个Profile是一组已命名的有逻辑的bean定义,只有当特定的profile启动时,它们才在容器里注册.Beans可以通过xml或者注解来指定其profile值.Environment对象在profiles中的角色决定目前哪个profile会被启动,哪个profile是默认启动. Properties在所有的应用中都起重要作用,它可以从以下资源中产生:properties文件,JVM系统属性,系统环境变量,JNDI,servlet上下文参数,特定的Properties对象,Maps,等.Environment对象与属性的关系是提供使用者一个合适的服务接口来配置属性资源,并从中释放属性. ###7.13.1 Bean definition profiles bean定义中的Profiles bean定义的profiles是一个核心容器的机制,允许在不同的环境里注册不同的bean.这个单词"environment"对不同的用户代表不同的事物,这个功能在很多场景下给你提供帮助,包括: 开发中使用内存数据库 VS 在QA或production中使用从JNDI中查找相同的数据源.

spring configuration files relationship?

百般思念 提交于 2019-12-04 19:06:54
I am using spring3.x. i have below configuration files in my application. applicationContext.xml spring-ws-servlet.xml web.xml <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>spring-ws</servlet-name> <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring-ws-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name

How to use Session Scoped Component in Controller

ε祈祈猫儿з 提交于 2019-12-04 12:50:10
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 even when I close the browser, so how can i use this Session Scoped Component in Singleton Controller?

How to check for Request Scope availability in Spring?

谁说我不能喝 提交于 2019-12-03 23:36:52
I'm trying to setup some code that will behave one way if spring's request scope is available, and another way if said scope is not available. The application in question is a web app, but there are some JMX triggers and scheduled tasks (i.e. Quartz) that also trigger invocations. E.g. /** * This class is a spring-managed singleton */ @Named class MySingletonBean{ /** * This bean is always request scoped */ @Inject private MyRequestScopedBean myRequestScopedBean; /* can be invoked either as part of request handling or as part of a JMX trigger or scheduled task */ public void someMethod(){ if(/

Spring bean scopes: session and globalSession

北战南征 提交于 2019-12-03 02:34:43
问题 What is the difference between session and globalSession in Spring framework? <bean id="exampleBean" class="com.test.baen.ExampleBean" scope="session"/> <bean id="exampleBean" class="com.test.baen.ExampleBean" scope="globalSession"/> As per my study, both are valid in the context of a web-aware Spring ApplicationContext. Now, session bean scope will remain until the user session, but will globalSession bean scope be available throughout the whole application? Is it the application scope? I am

Spring bean scopes: session and globalSession

江枫思渺然 提交于 2019-12-02 16:06:53
What is the difference between session and globalSession in Spring framework? <bean id="exampleBean" class="com.test.baen.ExampleBean" scope="session"/> <bean id="exampleBean" class="com.test.baen.ExampleBean" scope="globalSession"/> As per my study, both are valid in the context of a web-aware Spring ApplicationContext. Now, session bean scope will remain until the user session, but will globalSession bean scope be available throughout the whole application? Is it the application scope? I am unable to understand the term "global HTTP Session"; will it be available throughout the global HTTP

Difference between @injectMocks and @Autowired usage in mockito?

我是研究僧i 提交于 2019-11-30 13:43:51
When I was writing test case using the Mockito and Junit, I was using the @InjectMocks for the class to be tested. In other parts of project, I also see @Autowired is being used for the class to be tested. When can I use @InjectMocks and @Autowired ? What is the difference between two when we are trying to use them with class to be tested ? @InjectMocks is a Mockito mechanism for injecting declared fields in the test class into matching fields in the class under test . It doesn't require the class under test to be a Spring component. @Autowired is Spring's annotation for autowiring a bean into

Spring default behavior for lazy-init

戏子无情 提交于 2019-11-28 16:57:42
I am beginner to spring, ESP Inversion of control. I was puzzled understanding the difference between the following <bean id="demo" class="Demo" lazy-init="false"/> <bean id="demo" class="Demo" lazy-init="true"/> <bean id="demo" class="Demo" lazy-init="default"/> To my understanding : lazy-init=false creates the bean at the startup and lazy-init=true doesn't create a bean at the startup rather creates the bean upon request for a particular bean. Correct me here, If my interpretation is wrong. what exactly the default behavior of lazy-init is? How would it instantiate? The default behaviour is