【Java基础】Java根据Class获取对象实例
Spring在代码中获取bean的⼏种⽅式
⽅法⼀:在初始化时保存ApplicationContext对象
⽅法⼆:通过Spring提供的utils类获取ApplicationContext对象
⽅法三:继承⾃抽象类ApplicationObjectSupport
⽅法四:继承⾃抽象类WebApplicationObjectSupport
⽅法五:实现接⼝ApplicationContextAware
⽅法六:通过Spring提供的ContextLoader
获取spring中bean的⽅式总结
⽅法⼀:在初始化时保存ApplicationContext对象
ApplicationContext ac = new FileSystemXmlApplicationContext("l");
说明:这样的⽅式适⽤于採⽤Spring框架的独⽴应⽤程序,须要程序通过配置⽂件⼿⼯初始化Spring的情况。
⽅法⼆:通过Spring提供的⼯具类获取ApplicationContext对象
ApplicationContext ac1 = RequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContext(ServletContext sc);
说明:这样的⽅式适合于採⽤Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象。然后在通过它获取须要的类实例。上⾯两个⼯具⽅式的差别是,前者在获取失败时抛出异常。后者返回null。
⽅法三:继承⾃抽象类ApplicationObjectSupport
说明:抽象类ApplicationObjectSupport提供getApplicationContext()⽅法。能够⽅便的获取ApplicationContext。
Spring初始化时。会通过该抽象类的setApplicationContext(ApplicationContext context)⽅法将ApplicationContext 对象注⼊。
⽅法四:继承⾃抽象类WebApplicationObjectSupport
说明:类似上⾯⽅法。调⽤getWebApplicationContext()获取WebApplicationContext
⽅法五:实现接⼝ApplicationContextAware
说明:实现该接⼝的setApplicationContext(ApplicationContext context)⽅法,并保存ApplicationContext 对象。Spring初始化时,会通过该⽅法将ApplicationContext对象注⼊。
下⾯是实现ApplicationContextAware接⼝⽅式的代码,前⾯两种⽅法类似:
public class SpringContextUtil implements ApplicationContextAware {
/
/ Spring应⽤上下⽂环境
private static ApplicationContext applicationContext;
/**
* 实现ApplicationContextAware接⼝的回调⽅法。设置上下⽂环境
*
* @param applicationContext
*/
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextUtil.applicationContext = applicationContext;
}
/**
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 获取对象
*
* @param name
* @return Object
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
Bean(name);
}
}
尽管,spring提供的后三种⽅法能够实如今普通的类中继承或实现对应的类或接⼝来获取spring 的ApplicationContext对象,可是在使⽤是⼀定要注意实现了这些类或接⼝的普通
java类⼀定要在Spring 的配置⽂件l⽂件⾥进⾏配置。否则获取的ApplicationContext对象将为null。
IOC容器有beanFactory 和ApplicationContext.通常建议使⽤后者,因为它包含了前者的功能。Spring的核⼼是ApplicationContext.它负责管理 beans 的完整⽣命周期。我们可以从applicationContext⾥通过bean名称获取安装的bean.进⾏某种操作。不能直接使⽤applicationContext.⽽需要借助applicationContextAware.具体⽅法如下:
@Component
public class ApplicationContextGetBeanHelper implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public static Object getBean(String className) throws BeansException,IllegalArgumentException {
if(className==null || className.length()<=0) {
throw new IllegalArgumentException("className为空");
}
String beanName = null;
if(className.length() > 1) {
beanName = className.substring(0, 1).toLowerCase() + className.substring(1);
} else {
beanName = LowerCase();
}
return applicationContext != null ? Bean(beanName) : null;
}
}
⽅法六:通过Spring提供的ContextLoader
WebApplicationContext wac = CurrentWebApplicationContext();
最后提供⼀种不依赖于servlet,不须要注⼊的⽅式。可是须要注意⼀点,在server启动时。Spring容器初始化时,不能通过下⾯⽅法获取Spring 容器,细节能够查看spring源代码org.t.ContextLoader。springframework依赖
spring – 使⽤@Autowired绑定不在使⽤’new’启动的实例内部⼯作
如果要以编程⽅式⾃动装配,可以使⽤:
private @Autowired AutowireCapableBeanFactory beanFactory;
public void process() {
MyBean obj = new MyBean();
beanFactory.autowireBean(obj);
// obj will now have its dependencies autowired.
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。