面试问题

来源:互联网 发布:淘宝店铺二维码名片 编辑:程序博客网 时间:2024/04/28 01:30

spring的配置的主要标签是什么?有什么作用?
<beans>
   <bean id=”” class=”” init=”” destroy=”” singleton=””>
    <property name=””>
     <value></value>
    </property>
    <property name=””>
     <ref local></ref>
    </property>
   </bean>
</beans>
Spring如何实现资源管理?
使用
applicationContext.getResource(“classpath:文件名”):在src根目录下,在类路径下
applicationContext.getResource(“classpath:/chap01/文件名”): 以src根目录下的基准往下走。
applicationContext.getResource(“file:c:/a.properties”):在系统文件目录下。
Spring中ApplicationContext的作用是什么?
beanFactory
国际化(getMesage)
资源管理:可以直接读取一个文件的内容(getResource)
加入web框架中(加入一个servlet或监听器)
事件处理
spring中的核心类有那些,各有什么作用?
BeanFactory:产生一个新的实例,可以实现单例模式
BeanWrapper:提供统一的get及set方法
ApplicationContext:提供框架的实现,包括BeanFactory的所有功能
aop中的关键名词有些那些,相互关系是什么?
aop代理: 拦截器:
处理:(advice)
目标对象:
关切点:条件
连接点:方法、属性
切面:
Spring和Struts的区别?
strusts:是一种基于MVC模式的一个web层的处理。
Spring:提供了通用的服务,ioc/di aop,关心的不仅仅web层,应当j2ee整体的一个服务,可以很容易融合不同的技术struts hibernate ibatis ejb remote springJDBC springMVC
在Spring框架中如何更加高效的使用JDBC
使用Spring框架提供的模板类JdbcTemplete可以是JDBC更加高效
代码如下:JdbcTemplate template = new JdbcTemplate(myDataSource);
DAO类的例子:
public class StudentDaoJdbc implements StudentDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
more..
}
配置文件:
<bean id=”jdbcTemplate” class=”org.springframework.jdbc.core.JdbcTemplate”>
<property name=”dataSource”>
<ref bean=”dataSource”/>
</property>
</bean>
<bean id=”studentDao” class=”StudentDaoJdbc”>
<property name=”jdbcTemplate”>
<ref bean=”jdbcTemplate”/>
</property>
</bean>
<bean id=”courseDao” class=”CourseDaoJdbc”>
<property name=”jdbcTemplate”>
<ref bean=”jdbcTemplate”/>
</property>
</bean>
Spring如何创建一个数据连接池
<bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource”>
<property name=”driver”>
<value>${db.driver}</value>
</property>
<property name=”url”>
<value>${db.url}</value>
</property>
<property name=”username”>
<value>${db.username}</value>
</property>
<property name=”password”>
<value>${db.password}</value>
</property>
</bean>
请介绍一下Spring框架中Bean的作用域
在spring2.0之前bean只有2种作用域即:singleton(单例)、non-singleton(也称 prototype),Spring2.0以后,增加了session、request、global session三种专用于Web应用程序上下文的Bean。因此,默认情况下Spring2.0现在有五种类型的Bean。
<bean id=”role” class=”spring.chapter2.maryGame.Role” scope=”singleton”/>
这里的scope就是用来配置spring bean的作用域,它标识bean的作用域。
在spring2.0之前bean只有2种作用域即:singleton(单例)、non-singleton(也称 prototype),Spring2.0以后,增加了session、request、global session三种专用于Web应用程序上下文的Bean。因此,默认情况下Spring2.0现在有五种类型的Bean。当然,Spring2.0对 Bean的类型的设计进行了重构,并设计出灵活的Bean类型支持,理论上可以有无数多种类型的Bean,用户可以根据自己的需要,增加新的Bean类型,满足实际应用需求。
1、singleton作用域
当一个bean的作用域设置为singleton,那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。换言之,当把一个bean定义设置为singleton作用域时,Spring IOC容器只会创建该bean定义的唯一实例。这个单一实例会被存储到单例缓存(singleton cache)中,并且所有针对该bean的后续请求和引用都将返回被缓存的对象实例,这里要注意的是singleton作用域和GOF设计模式中的单例是完全不同的,单例设计模式表示一个ClassLoader中只有一个class存在,而这里的singleton则表示一个容器对应一个bean,也就是说当一个bean被标识为singleton时候,spring的IOC容器中只会存在一个该bean。
配置实例:
<bean id=”role” class=”spring.chapter2.maryGame.Role” scope=”singleton”/>
或者
<bean id=”role” class=”spring.chapter2.maryGame.Role” singleton=”true”/>
2、prototype
prototype作用域部署的bean,每一次请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)都会产生一个新的bean实例,相当于一个new的操作,对于prototype作用域的bean,有一点非常重要,那就是Spring不能对一个 prototype bean的整个生命周期负责,容器在初始化、配置、装饰或者是装配完一个prototype实例后,将它交给客户端,随后就对该prototype实例不闻不问了。不管何种作用域,容器都会调用所有对象的初始化生命周期回调方法,而对prototype而言,任何配置好的析构生命周期回调方法都将不会被调用。清除prototype作用域的对象并释放任何prototype bean所持有的昂贵资源,都是客户端代码的职责。(让Spring容器释放被singleton作用域bean占用资源的一种可行方式是,通过使用 bean的后置处理器,该处理器持有要被清除的bean的引用。)
配置实例:
<bean id=”role” class=”spring.chapter2.maryGame.Role” scope=”prototype”/>
或者
<beanid=”role” class=”spring.chapter2.maryGame.Role” singleton=”false”/>
3、request
request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效,配置实例:
request、session、global session使用的时候,首先要在初始化web的web.xml中做如下配置:
如果你使用的是Servlet 2.4及以上的web容器,那么你仅需要在web应用的XML声明文件web.xml中增加下述ContextListener即可:
<web-app>

<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

</web-app>
如果是Servlet2.4以前的web容器,那么你要使用一个javax.servlet.Filter的实现:
<web-app>
..
<filter>
<filter-name>requestContextFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>requestContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>
接着既可以配置bean的作用域了:
<bean id=”role” class=”spring.chapter2.maryGame.Role” scope=”request”/>
4、session
session作用域表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP session内有效,配置实例:
配置实例:
和request配置实例的前提一样,配置好web启动文件就可以如下配置:
<bean id=”role” class=”spring.chapter2.maryGame.Role” scope=”session”/>
5、global session
global session作用域类似于标准的HTTP Session作用域,不过它仅仅在基于portlet的web应用中才有意义。Portlet规范定义了全局Session的概念,它被所有构成某个 portlet web应用的各种不同的portlet所共享。在global session作用域中定义的bean被限定于全局portlet Session的生命周期范围内。如果你在web中使用global session作用域来标识bean,那么,web会自动当成session类型来使用。
配置实例:
和request配置实例的前提一样,配置好web启动文件就可以如下配置:
<bean id=”role” class=”spring.chapter2.maryGame.Role” scope=”global session”/>
6、自定义bean装配作用域
在spring 2.0中作用域是可以任意扩展的,你可以自定义作用域,甚至你也可以重新定义已有的作用域(但是你不能覆盖singleton和 prototype),spring的作用域由接口org.springframework.beans.factory.config.Scope来定义,自定义自己的作用域只要实现该接口即可,下面给个实例:
我们建立一个线程的scope,该scope在表示一个线程中有效,代码如下:
publicclass MyScope implements Scope …{
privatefinal ThreadLocal threadScope = new ThreadLocal() …{
protected Object initialValue() …{
returnnew HashMap();
}
};
public Object get(String name, ObjectFactory objectFactory) …{
Map scope = (Map) threadScope.get();
Object object = scope.get(name);
if(object==null) …{
object = objectFactory.getObject();
scope.put(name, object);
}
return object;
}
public Object remove(String name) …{
Map scope = (Map) threadScope.get();
return scope.remove(name);
}
publicvoid registerDestructionCallback(String name, Runnable callback) …{
}
public String getConversationId() …{
// TODO Auto-generated method stub
returnnull;
}
}
Spring如何获取Bean
通过xml配置文件
bean配置在xml里面,spring提供多种方式读取配置文件得到ApplicationContext.
第一种方式:FileSystemXmlApplicationContext
通过程序在初始化的时候,导入Bean配置文件,然后得到Bean实例:
ApplicationContext ac = new FileSystemXmlApplicationContext(”applicationContext.xml”)
ac.getBean(”beanName”);
第二种方式:WebApplicationContextUtil
在B/S系统中,通常在web.xml初始化bean的配置文件,然后由WebAppliCationContextUtil得到ApplicationContext.例如:
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ctx =   WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
其中 servletContext sc 可以具体 换成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext();
另外,由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 对象:
WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
请介绍一下Spring框架中Bean的生命周期
一、Bean的定义
Spring通常通过配置文件定义Bean。如:
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd”>
<bean id=”HelloWorld” class=”com.pqf.beans.HelloWorld”>
<property name=”msg”>
<value>HelloWorld</value>
</property>
</bean>
</beans>
这个配置文件就定义了一个标识为 HelloWorld 的Bean。在一个配置文档中可以定义多个Bean。
二、Bean的初始化
有两种方式初始化Bean。
1、在配置文档中通过指定init-method 属性来完成
在Bean的类中实现一个初始化Bean属性的方法,如init(),如:
public class HelloWorld{
public String msg=null;
public Date date=null;
public void init() {
msg=”HelloWorld”;
date=new Date();
}
……
}
然后,在配置文件中设置init-mothod属性:
<bean id=”HelloWorld” class=”com.pqf.beans.HelloWorld” init-mothod=”init” >
</bean>
2、实现 org.springframwork.beans.factory.InitializingBean接口
Bean实现InitializingBean接口,并且增加 afterPropertiesSet() 方法:
public class HelloWorld implement InitializingBean {
public String msg=null;
public Date date=null;
public void afterPropertiesSet() {
msg=”向全世界问好!”;
date=new Date();
}
……
}
那么,当这个Bean的所有属性被Spring的BeanFactory设置完后,会自动调用afterPropertiesSet()方法对Bean进行初始化,于是,配置文件就不用指定 init-method属性了。
三、Bean的调用
有三种方式可以得到Bean并进行调用:
1、使用BeanWrapper
HelloWorld hw=new HelloWorld();
BeanWrapper bw=new BeanWrapperImpl(hw);
bw.setPropertyvalue(”msg”,”HelloWorld”);
system.out.println(bw.getPropertyCalue(”msg”));
2、使用BeanFactory
InputStream is=new FileInputStream(”config.xml”);
XmlBeanFactory factory=new XmlBeanFactory(is);
HelloWorld hw=(HelloWorld) factory.getBean(”HelloWorld”);
system.out.println(hw.getMsg());
3、使用ApplicationConttext
ApplicationContext actx=new FleSystemXmlApplicationContext(”config.xml”);
HelloWorld hw=(HelloWorld) actx.getBean(”HelloWorld”);
System.out.println(hw.getMsg());
四、Bean的销毁
1、使用配置文件中的 destory-method 属性
与初始化属性 init-methods类似,在Bean的类中实现一个撤销Bean的方法,然后在配置文件中通过 destory-method指定,那么当bean销毁时,Spring将自动调用指定的销毁方法。
2、实现 org.springframwork.bean.factory.DisposebleBean接口
如果实现了DisposebleBean接口,那么Spring将自动调用bean中的Destory方法进行销毁,所以,Bean中必须提供Destory方法。
Spring框架有哪几部分组成?
Spring框架有七个模块组成组成,这7个模块(或组件)均可以单独存在,也可以与其它一个或多个模块联合使用,主要功能表现如下:
☞ Spring 核心容器(Core):提供Spring框架的基本功能。核心容器的主要组件是BeanFactory,她是工厂模式的实现。BeanFactory使用控制反转(Ioc)模式将应用程序的配置和依赖性规范与实际的应用代码程序分开。
☞ Spring AOP:通过配置管理特性,Spring AOP模块直接面向方面的编程功能集成到了Spring框架中,所以可以很容易的使Spring框架管理的任何对象支持 AOP。Spring AOP模块为基于Spring的应用程序中的对象提供了事务管理服务。通过使用Spring AOP,不用依赖于EJB组件,就可以将声明性事务管理集成到应用程序中。
☞ Spring ORM:Spring框架集成了若干ORM框架,从而提供了ORM的对象关系工具,其中包括 JDO、Hibernate、iBatis和TopLink。所有这些都遵从Spring的通用事务和DAO异常层结构。
☞ Spring DAO:JDBC DAO抽象层提供了有意义的异常层次的结构,可用该结构来管理异常处理和不同数据供应商抛出的异常错误信息。异常层次结构简化了错误处理,并且大大的降低 了需要编写的异常代码数量(例如,打开和关系连接)。Spring DAO的面向JDBC的异常遵从通用的DAO异常层结构。
☞ Spring WEB:Web上下文模块建立在上下文模块(Context)的基础之上,为基于Web服务的应用程序提供了上下文的服务。所以Spring框架支持 Jakarta Struts的集成。Web模块还简化了处理多部分请求及将请求参数绑定到域对象的工作。
☞ Spring上下文(Context):Spring上下文是一个配置文件,向Spring框架提供上下文信息。Spring上下文包括企业服务,例如 JNDI、EJB、电子邮件、国际化校验和调度功能。
☞ Spring MVC:Spring的MVC框架是一个全功能的构建Web应用程序的MVC实现。通过策略接口,MVC框架变成为高度可配置的,MVC容纳的大量视图技术,包括JSP、Velocity、Tiles、iText和Pol

原创粉丝点击