Java test class Gets the spring Bean method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.