标签:
spring容器以xml的形式注入bean,然后可以在类中获取,获取的形式主要有二种:第一种最简单--采用@Resource 或@Autowired关键字在加载spring文件时将bean注入到相应的类中;第二种方式是先用FileSystemXmlApplicationContext、ClassPathXmlApplicationContext 实例化ApplicationContext(Spring容器),然后调用其getBean方法获取。
下面直接代码说明:
注意:是在springJUnit的测试环境下。如不是测试环境,在实际中,如果用到@Resource 或@Autowired自动注入的方式,还需在spring容器添加“扫包”,这样项目发布时加载spring容器才会扫描具体的包下面的类,然后将bean注入到该类中。
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 * 加载文件放在类目录下 * @author Lenovo * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class MyTest { @Resource private User user3;//用@Resource关键字自动注入获取bean。注意:引用名是在spring容器中的注入bean的id,不可瞎写! @Test public void test1(){//用FileSystemXmlApplicationContext获取bean ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml"); System.out.println(ac);//org.springframework.context.support.FileSystemXmlApplicationContext@52c603e7: startup date [Tue Feb 02 09:38:46 CST 2016]; root of context hierarchy } @Test public void test2(){//用ClassPathXmlApplicationContext获取bean ApplicationContext act=new ClassPathXmlApplicationContext("applicationContext.xml"); User user=(User)act.getBean("user"); System.out.println(user); } @Test public void test3(){ System.out.println(user3); } }
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 * 加载文件放在WEB-INF目录下 * @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 加载WEB-INF目录下的文件必须是绝对路径 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 不能加载WEB-INF目录下的文件,只能加载类目录下的文件。 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); } }
SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件二--获取注入的bean的二种方式
标签:
原文地址:http://www.cnblogs.com/chenxueling/p/5474745.html