applicationcontext

Java annotation scanning with spring

女生的网名这么多〃 提交于 2019-12-01 11:18:56
问题 I have few classes that I need to annotate with a name so I defined my annotation as @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface JsonUnmarshallable { public String value(); } Now the class that needs this annotation is defined as @JsonUnmarshallable("myClass") public class MyClassInfo { <few properties> } I used below code to scan the annotations private <T> Map<String, T> scanForAnnotation(Class<JsonUnmarshallable> annotationType) {

Spring ApplicationContext with multiple XML Files from Jar

馋奶兔 提交于 2019-12-01 11:14:04
I need to create a ApplicationContext with the "main" applicationContext-a.xml from the current Maven build. The other one wires classes from another maven build and is preset in the jar included by a Maven Dependency. Here the idea: ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "classpath*:applicationContext-*.xml"}); This should load applicationContext-a.xml from the Classpath because it's in the same Project. This works. Then applicationContext-b.xml should be loaded from the dependency-jar. This doesn't work. Note that "classpath*:applicationContext-*.xml"

Failed to load ApplicationContext with @ContextConfiguration(classes={ … })

我是研究僧i 提交于 2019-12-01 09:24:03
I am trying to run some spring test using Java and annotations configuration. I have two configuration classes in the test packages: @Configuration @ComponentScan(basePackages = "com.mypackages") public class TestConfig { @Bean public MyService getMyService() { return new MyServiceImpl(); } } @Configuration @EnableTransactionManagement public class JpaTestConfig { @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){ LocalContainerEntityManagerFactoryBean lcemfb = new LocalContainerEntityManagerFactoryBean(); lcemfb.setDataSource(this.dataSource()); lcemfb

Spring ApplicationContext with multiple XML Files from Jar

亡梦爱人 提交于 2019-12-01 08:20:39
问题 I need to create a ApplicationContext with the "main" applicationContext-a.xml from the current Maven build. The other one wires classes from another maven build and is preset in the jar included by a Maven Dependency. Here the idea: ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "classpath*:applicationContext-*.xml"}); This should load applicationContext-a.xml from the Classpath because it's in the same Project. This works. Then applicationContext-b.xml

Failed to load ApplicationContext with @ContextConfiguration(classes={ … })

时光毁灭记忆、已成空白 提交于 2019-12-01 07:22:49
问题 I am trying to run some spring test using Java and annotations configuration. I have two configuration classes in the test packages: @Configuration @ComponentScan(basePackages = "com.mypackages") public class TestConfig { @Bean public MyService getMyService() { return new MyServiceImpl(); } } @Configuration @EnableTransactionManagement public class JpaTestConfig { @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){ LocalContainerEntityManagerFactoryBean lcemfb =

经典Java面试题-Spring BeanFactory

拟墨画扇 提交于 2019-12-01 01:08:26
[ 面试原题] ApplicationContext和BeanFactory的区别 [ 正确答案] 1.如果使用ApplicationContext,则配置的bean如果是singleton不管你用还是不用,都被实例化。好处是可以预先加载,坏处是浪费内存。它也可以为Bean配置lazy-init=true来让Bean延迟实例化。 2. BeanFactory ,当使用BeanFactory实例化对象时,配置的bean不会马上被实例化。当你使用该bean时才会被实例化(getBean)。好处是节约内存,缺点是速度比较慢。多用于移动设备的开发上。 3.一般没有特殊要求,应当使用ApplicationContext完成。 [ 面试技术点] Spring Bean 初始化的方法。 [ 解读] 1. 从ApplicationContext中取bean ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 当我们实例化bean.xml时,该文件中配置的bean都会被实例化。(该bean scope是singleton) 2. 从BeanFactory中取bean BeanFactory factory= new XmlBeanFactory(new ClassPathResource("bean.xml"

Spring : how to expose SimpMessagingTemplate bean to root context ?

霸气de小男生 提交于 2019-11-30 18:24:35
问题 I'm developing a Java webapp with Spring as the main framework (Spring core, Spring mvc, Spring security, Spring data, Spring websocket are notably used). Declaring a message-broker in a Spring context like this provides a SimpMessagingTemplate bean to the context : <websocket:message-broker> <websocket:stomp-endpoint path="/stomp"> <websocket:sockjs/> </websocket:stomp-endpoint> <websocket:simple-broker prefix="/topic,/queue"/> </websocket:message-broker> I have to put this tag in dispatcher

Spring creating multiple instances of a singleton?

心已入冬 提交于 2019-11-30 17:24:17
I have a graph of Spring beans which autowire each other. Heavily simplified illustration: <context:annotation-config/> <bean class="Foo"/> <bean class="Bar"/> <bean class="Baz"/> ... public class Foo { @Autowired Bar bar; @Autowired Baz baz; } public class Bar { @Autowired Foo foo; } public class Baz { @Autowired Foo foo; } All of these beans don't have scope specified which imply they are singletons (making them explicit singletons doesn't change anything, I've tried). The problem is that after the instantiation of a single application context , instances of Bar and Baz contain different

Read an environment variable from applicationContext.xml

自作多情 提交于 2019-11-30 13:24:28
I need read an environment variable defined in my web.xml <env-entry> <description>Path Repositorio NFS</description> <env-entry-name>PATH_ENV</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>C:/V3</env-entry-value> </env-entry> from my applicationContext.xml <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="${PATH_ENV}/myprop.properties" /> </bean> How can I do this ? Finally I have done the next: 1 Define environment variable in context.xml: <Environment name="PATH

基于Groovy实现Spring Bean的动态加载

元气小坏坏 提交于 2019-11-30 03:54:04
Spring对Groovy有着良好的支持,能把Groovy实现类作为Bean来使用,在前一篇Blog《Spring对Groovy Bean的支持》有详细的描述 http://my.oschina.net/joshuazhan/blog/137940 。基于Groovy Bean可以实现Bean的动态修改,但还有一个缺陷,即无法动态的加载/卸载Bean,本文基于Spring ApplicationContext的层级关系(Hierarchy )提供一个实现动态类加载的思路。 1. Spring ApplicationContext的层级关系 在ApplicationContext抽象类AbstractApplicationContext中,Spring引入了Context(后文将ApplicationContext简称为Context)的层级关系,通过ApplicationContext的构造函数,即可设置其父Context。 /** * Create a new AbstractApplicationContext with the given parent context. * @param parent the parent context */ public AbstractApplicationContext(ApplicationContext parent) {