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>
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 have developed a Spring MVC web application. In this application I have two containers and the location of the spring bean configuration file is:
/WEB-INF/dispatcher-servlet.xml
I have changed the name of the spring bean cfg file but I also want to change the location to:
/com/nt/cfg/applicationContext.xml
However, Spring is not recognizing any location other than /WEB-INF/
You just need to declare de route when you create the ClassPathXmlApplicationContext:
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
The default location is the resources folder.
Hope this help you.
The answer is YES, you can change the name and location of the configuration file but you have to make spring aware of the new name and location.
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
It will load the context from context.xml file (context.xml should be present in classpath).
You can create new Applicationcontext by passing desired XML file as parameter to constructor.
So after changing name and location of the file you have to register here for spring reference so that spring can find the configuration file.
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.
I am creating an application which uses Spring beans and I need to initialize ApplicationContext from xml. As it is not a server application, it doesn't have WEB-INF folder.So, where I should put the xml file?
Use ClassPathXmlApplicationContext:
ApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext.xml");
Or consider migrating to #Configuration:
ApplicationContext ctx =
new AnnotationConfigApplicationContext(AppConfig.class);
where AppConfig is annotated with #Configuration and no XML is needed.
The spring documentation for this topic is a little overwhelming at first. A handy starting point in the spring documentation for application context config is here
A file system xml application context is probably the easiest to start with. So:
ApplicationContext appCtx =
new FileSystemXmlApplicationContext("/path/to/springconfig.xml");
Use ClassPathApplicationContext if you have the xml configuration in your application class path. In this example it might be a springconfig.xml under a project directory src/config/springconfig.xml.
ApplicationContext appCtx =
new ClassPathXmlApplicationContext("config/springconfig.xml");
If you are unsure where your springconfig.xml ends up after you have built your application, you can use the following command to list the contents of your jar:
jar -tvf myapp.jar
For a default eclipse java project, the project-home/bin directory is the start of the classpath.
A related question was asked here
Check this example Spring Setter Injection
If the XML is in the applications classpath then use like this
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Otherwise if the XML is in File system then use
ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");
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.