Configuration file Two under the SpringJUnit4 Load Class directory (SRC) and Wef-inf directory--Two ways to get injected beans

Source: Internet
Author: User
Tags lenovo

Objective:

The spring container is injected into the bean as XML, which can then be obtained in the class, with two main forms: the first is the simplest-using the @resource or @autowired keyword to inject the bean into the appropriate class when the spring file is loaded The second way is to instantiate the ApplicationContext (Spring container) with Filesystemxmlapplicationcontext, Classpathxmlapplicationcontext, Then call its Getbean method to get.

The following direct code description:

Note: It is in the Springjunit test environment. If it is not a test environment, in practice, if you use @resource or @autowired auto-injection, you also need to add a "sweep package" in the spring container, so that when the project is published, the spring container will scan the class below the specific package, and then inject the bean into the class.

The loading file is placed in the class directory.
Package Com.my.test;import Javax.annotation.resource;import Org.junit.test;import org.junit.runner.runwith;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Org.springframework.context.support.filesystemxmlapplicationcontext;import Org.springframework.test.context.contextconfiguration;import Org.springframework.test.context.junit4.springjunit4classrunner;import com.wql.vo.user;/** * Spring Test * The load file is placed under the class directory * @author Lenovo * */@RunWith (Springjunit4classrunner.class) @ContextConfiguration ("Classpath: Applicationcontext.xml ") public class MyTest {@Resource private User user3;//automatically injects the fetch bean with the @resource keyword.        Note: The reference name is the ID of the injected bean in the spring container and cannot be written blindly! @Test public void Test1 () {//Filesystemxmlapplicationcontext get bean applicationcontext ac = new FILESYSTEMXMLAPPL        Icationcontext ("Classpath:applicationContext.xml"); System.out.println (AC);//org.[email protected]52c603e7:startup Date [Tue Feb 09:38:46 CST 2016]; Root of context hierarchy} @Test public void Test2 () {//Classpathxmlapplicationcontext get Bean Applicationc                Ontext act=new classpathxmlapplicationcontext ("Applicationcontext.xml");        User user= (user) Act.getbean ("User");    SYSTEM.OUT.PRINTLN (user);    } @Test public void Test3 () {System.out.println (USER3); }}

Second, the loading file is placed in the Web-inf directory
Package Com.my.test;import Javax.annotation.resource;import Org.junit.test;import org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.classpathxmlapplicationcontext;import Org.springframework.context.support.filesystemxmlapplicationcontext;import Org.springframework.test.context.contextconfiguration;import Org.springframework.test.context.junit4.springjunit4classrunner;import com.wql.vo.user;/** * Spring Test2 * The load file is placed in the Web-inf directory * @author Lenovo * */@RunWith (Springjunit4classrunner.class) @ContextConfiguration (locations={"file        : Webcontent/web-inf/applicationcontext.xml "}) public class MyTest2 {@Resource private User user3;  @Test public void Test1 () {//filesystemxmlapplicationcontext The file under the load Web-inf directory must be an absolute path ApplicationContext AC = new Filesystemxmlapplicationcontext ("D:/sts_workspace/mynewdpitest/webcontent/web-inf/applicationcontExt.xml ");        System.out.println (AC);    System.out.println (Ac.getbean ("user"));        } @Test public void Test2 () {//classpathxmlapplicationcontext cannot load files under the Web-inf directory, only files under the class directory can be loaded. ApplicationContext act=new classpathxmlapplicationcontext ("d:/sts_workspace/mynewdpitest/webcontent/web-inf/        Applicationcontext.xml ");    SYSTEM.OUT.PRINTLN (Act);//user user= (user) Act.getbean ("user");//System.out.println (user);    } @Test public void Test3 () {System.out.println (USER3); }}

Configuration file Two under the SpringJUnit4 Load Class directory (SRC) and Wef-inf directory--Two ways to get injected beans

Related Article

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.