Get a bean instance (object) defined in spring .
ClassPathXmlApplicationContext cpx=new ClassPathXmlApplicationContext ("package name (or the full path to save)/configuration file name (that is, xml file)");
cpx.getBean("Bean instance defined in the configuration file");
The classpath: prefix is not required, the default is below the classpath path of the project;
if you want to use an absolute path, you need to add the file: prefix to indicate that this is an absolute path;
for FileSystemXmlApplicationContext :
the default indicates two types:
1, no drive letter is the project working path, that is, the root directory of the project;
2. The drive letter indicates the absolute path of the file.
If you want to use the classpath path, you need to prefix the classpath: .
public class HelloClient {
protected static final Log log = LogFactory.getLog(HelloClient.class);
public static void main(String[] args) {
// Resource resource = new ClassPathResource (" appcontext.xml");
// BeanFactory factory = new XmlBeanFactory(resource);
// You can also use classpath path
// ApplicationContext factory=new ClassPathXmlApplicationContext(" classpath: appcontext.xml");
// ApplicationContext factory=new ClassPathXmlApplicationContext("appcontext.xml");
// ClassPathXmlApplicationContext uses the file prefix and can also use absolute paths
// ApplicationContext factory=new ClassPathXmlApplicationContext("file:G:/1Java Utility Project Resources/2Spring/1 Proficient in Spring's full Jar code /workspace/workspace/example6/src/appcontext.xml");
//The default path of the file system refers to the root path of the project
// ApplicationContext factory=new FileSystemXmlApplicationContext("src/appcontext.xml") ;
//The classpath: prefix is used as a flag, so that FileSystemXmlApplicationContext can also read the relative path under the classpath
// ApplicationContext factory=new FileSystemXmlApplicationContext("classpath:appcontext.xml");
// ApplicationContext factory=new FileSystemXmlApplicationContext(" file:G: /1Java practical project resources/2Spring/1Proficient in Spring full Jar code/workspace/workspace/example6/src/appcontext.xml");
//You can also add no file prefix
ApplicationContext factory=new FileSystemXmlApplicationContext("G:/1Java practical project resources/2Spring/1 Proficient in Spring full Jar code/workspace/workspace/example6/src/appcontext.xml");
IHelloWorld hw = ( IHelloWorld) factory.getBean("helloworldbean");
log.info(hw.getContent("luoshifei"));
}
}