Java测试类获取spring bean方法

Java测试类获取spring bean方法

http://blog.163.com/lizhenming_2008/blog/static/76558333201362094243911/

1、通过spring上下文

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationcontext.xml");

DataSource ds = (DataSource) ctx.getBean("dataSource");

2、Resource类

Resource rs = new FileSystemResource("**/**/appliactioncontext.xml");

BeanFactory factory = new XmlBeanFactory(rs);

DataSource ds = (DataSource) factory.getBean("dataSource");

3、用接口类WebApplicationContext来取

private WebApplicationContext wac;

wac = WebApplicationContextUtils.getRequiredWebApplicationContext(his.getServletContext());

wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");

其中,jdbcTemplate为spring配置文件中的一个bean的id值。

读取属性(properties)文件

1、利用spring读取properties 文件

BeanDefinitionRegistry 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、利用java.util.Properties读取属性文件

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ipConfig.properties");

Properties p = new Properties();

try {

p.load(inputStream);

} catch (IOException e1)  {

e1.printStackTrace();

}

时间: 2024-08-04 14:39:15

Java测试类获取spring bean方法的相关文章

static关键字,引发的spring普通类获取spring的bean的思考

在c++和java中static关键字用于修饰静态成员变量和成员函数 举例一个普通的javabean class AA { int a; static int b; geta/seta;//此处省略getset getb/setb; } 如果创建了一个对象AA, AA a =new AA(); 这个时候只会在内存中给这个对象分配四个字节,也就是a变量所占的字节数,因为static申明的全局变量在全局区中,是所有这个类的对象共有的,例如: a.setB(10); AA b =new A(); Sy

java测试类调用LDAP服务器客户端信息实行增删改查

如下是我自写的java测试类调用客户端配置好的用户信息: package com; import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingEnumeration; import javax.naming.NamingException; impor

普通java类获取spring容器bean的方法

很多时候,我们在普通的java类中需要获取spring的bean来做操作,比如,在线程中,我们需要操作数据库,直接通过spring的bean中构建的service就可以完成.无需自己写链接..有时候有些好的东西,拿到用就好了. 这里是多种方式中的一种. 通过实现ApplicationContextAware获取bean.这里有个问题,就是,如果spring容器没有启动完成的时候,不能通过这个方法获取,因为这样,会报空指针,因为 private static ApplicationContext

获取Spring Bean工具类SpringContextUtil

有时候需要在非Spring环境获取Spring的Bean import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import o

161018、springMVC中普通类获取注解service方法

1.新建一个类SpringBeanFactoryUtils 实现 ApplicationContextAware package com.loiot.baqi.utils; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware;

Springboot 测试类没有找到bean注入

其他乱七八糟配置就不扯了,先上项目结构图 配置好参数后我再src/test/java类测试访问数据库时发现bean没有正确的注入.值得注意的是,这个项目的启动类是叫App.java 所以我们必须在这个测试类上面加上注解: @RunWith(SpringRunner.class) @SpringBootTest(classes = App.class) 注意:SpringBoot(classes = App.class) classes后面跟的是启动类的class,千万不要随便抄网上的配置,写一些

Java从控制台获取数据的方法

一.使用System.in.read()一次获取一个字节 输入再多数据,只会获取第一个字节的int形式.获取的是字节,而不是字符,所以如果输入中文字符,强转后会得到乱码 1 try { 2 int in_num=System.in.read(); //获取的是一个字节的int类型 3 System.out.println("强转前:"+in_num); 4 System.out.println("强转后:"+(char)in_num); 5 } catch (IOE

java Thread 类 run 和 start 方法区别

public class ThreadModle { public static void main(String[] args) throws InterruptedException { Thread t = new Thread(new Runnable(){ @Override public void run() { System.out.println("Run threadID: " + Thread.currentThread().getId()); } }); Syst

JAVA 测试类

@Test 是Junit 测试类的标志 . 在Junit4中还有的测试注解有:  @BeforeClass ,@Before,@Test,@After,@AfterClass 1.其中:@BeforeClass,@AfterClass是Junit4中新添加进去的 2.如果Run as --->Junit Test,运行含有@Test注释的方法是,那么所有注解方法都将被执行,所含的执行顺序是: @BeforeClass ,@Before,@Test,@After,@AfterClass 注释有@T