If I have a statement in my servlet.xml like this:
<import resource="classes/com/au/curtin/example.xml/>
and an example.xml at that directory location that has Spring bean definitions as well as things like <component:context-scan/> and <tx:annotation-driven/>. Are these statements imported along with the bean definitions into the servlet.xml context or do I need to include duplicate <component:context-scan/> in my servlet.xml file?
The main purpose of import is to avoid duplication. With import the file is completely imported.Everything including bean defination and other configurations are copied.
Related
Can We Have Multiple Spring Configuration Files in One Project? If yes, can someone provide a working example to support this concept?
Yes, in large projects, having multiple Spring configurations increase maintainability and modularity.
You can load multiple files like this:-
#Configuration
#Import({MainConfig.class, SchedulerConfig.class})
public class AppConfig {
You can also upload one XML file that will contain all configs.
ApplicationContext context = new ClassPathXmlApplicationContext("spring-all.xml");
inside the XML file:-
<import resource="main.xml"/>
<import resource="scheduler.xml"/>
I am learner of spring have build my test project with spring IOC container and have configure beans.xml in my project root path and load into my application and get bean from it.
spring.xml in project root directory
BeanFactory bean = new XmlBeanFactory(new FileSystemResource("spring.xml"));
spring.xml in source file
BeanFactory bean = new XmlBeanFactory(new FileSystemResource("src/spring.xml"));
this is another code to load beans.xml file
ApplicationContext context = new GenericXmlApplicationContext("beans.xml");
my question is that is there any standards or conventions for creation of xml file name and location of file in real project.because in some reading articles i also found that there might be multiple xml files for large project like service.xml and dao.xml.
It can be useful to have bean definitions span multiple XML files. Often each individual XML configuration file represents a logical layer such as defining DAO beans etc. in your architecture but you should always place your XML configuration files under src/resources and access them as
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
From the Spring's Manual:
You can use the application context constructor to load bean definitions from all these XML fragments. This constructor takes multiple Resource locations, as was shown in the previous section. Alternatively, use one or more occurrences of the element to load bean definitions from another file or files. For example:
<beans>
<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>
</beans>
My problem - for the study Spring I write a not great application composed of several modules (multimoduls).
module1
src
entity
dao
resource
spring-config.xml ---> This is DataSource, SessionFactory, TransactionManager
module2
src
entity
dao
resource
spring-config.xml ---> This is DataSource, SessionFactory, TransactionManager
service1
src
service_for_module1
resource
spring-config.xml ---> Initialization bean Service1 (used for module1)
service2
src
service_for_module2
resource
spring-config.xml ---> Initialization bean Service2 (used for module2)
web
src
ManagerBeanForJSF
web
pages
WEB-INF
spring
spring-config.xml ---> Import all spring config from modules
I have exception org.hibernate.hql.internal.ast.QuerySyntaxException.
When I did not have module2 - all worked successfully. Error associated with duplication of SessionFactory and TransactionManager?
Can you give an example of application in Spring composed of several modules.
Thanks.
I would suggest you use a maven project and post the poms so we can clearly see the dependencies between the projects.
As your question is a bit incomplete I can only guess you're using maven. Right? Anyway, when using Spring you should definitely, as you already wrote yourself, define the infrastructural part in one common module. Spring provides the possibility to include other context files. Even with wildcards. So you could define one singular main context file that loads those of all others in one go. The best is to have some sort of naming conventions, so that each module can easily contribute its context files.
Is there a way in Spring to discover a
"plugin"-JAR from the classpath,
and load its applicationContext.xml dynamicaly?
I have achieved a plugin-like system with Spring by following this approach:
Each plug-in must contain a spring-context file with a specific name and package prefix (for example, com.example.myApp.whatever containing plugin.xml, or applicationContext.xml if you prefer).
For the plug-in to be detected in the classpath, the host application should dynamically import all the context files contributed by any jar following the previos scheme. This is achieved with a wildcard-based import in spring config:
<import resource="classpath*:/com/example/myApp/**/plugin.xml" />
Provided that each plug-in defines beans of a known interface (e.g., MyInterface). The host application can define a property of type List <MyInterface> and define the bean as autowire="byType" in order to retrieve all the beans of the MyInterfaceType in a list.
I have all my configuration details like (queries bean, url mapping etc) in one file (businesscaliber-servlet.xml) but i want separate out how do i do?
Using
<import resource="..."/>
See documentation. So in your businesscaliber-servlet.xml file, you could have:
<beans>
<import resource="queries-bean.xml"/>
<import resource="url-mappings.xml"/>
... etc ...
</beans>
Spring doesn't care which beans go into which file, they all get combined at runtime.