Java Test class Gets the spring Bean method
http://blog.163.com/lizhenming_2008/blog/static/76558333201362094243911/
1. Through the spring contextApplicationContext ctx = new Classpathxmlapplicationcontext ("Applicationcontext.xml");DataSource ds = (DataSource) ctx.getbean ("DataSource");2, Resource classResource rs = new Filesystemresource ("**/**/appliactioncontext.xml");Beanfactory factory = new Xmlbeanfactory (RS);DataSource ds = (DataSource) factory.getbean ("DataSource");3, with the interface class Webapplicationcontext to takePrivate Webapplicationcontext WAC; WAC = Webapplicationcontextutils.getrequiredwebapplicationcontext (His.getservletcontext ()); WAC = Webapplicationcontextutils.getwebapplicationcontext (This.getservletcontext ()); JdbcTemplate JdbcTemplate = (jdbctemplate) ctx.getbean ("JdbcTemplate"), where JdbcTemplate is the ID value of a bean in the spring configuration file. Read Property (properties) file1. Use spring to read the properties filebeandefinitionregistry reg = new Defaultlistablebeanfactory ();Propertiesbeandefinitionreader reader = new Propertiesbeandefinitionreader (reg);reader.loadbeandefinitions (New Classpathresource ("Beanconfig.properties"));Beanfactory factory = (beanfactory) reg;Hellobean Hellobean = (Hellobean) factory.getbean ("Hellobean");2. Read the properties file with Java.util.PropertiesInputStream InputStream = This.getclass (). getClassLoader (). getResourceAsStream ("ipconfig.properties"); Properties P = new properties (); try {p.load (InputStream); } catch (IOException E1) {e1.printstacktrace (); }
Java test class Gets the spring Bean method