spring的详细复习讲解

来源:互联网 发布:2017高仿耐克淘宝店 编辑:程序博客网 时间:2024/05/01 00:00

Spring的终极复习版本

Spring的官方网站:http://www.springsource.org

1、 控制反转

2、 依赖注入:在运行期,由外部容器动态的将依赖对象注入到组建中。

3

4

5spring的开发包的一些解释

6

7、实例化spring

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContext.xml");//这里读取的、/applicationContext.xml名字可以改变,也可以写成applicationContext.xml不要前面的/

Person per = (Person) applicationContext.getBean("//person");//如果//person来得到bean那么在配置的时候,不能用id,用到特殊字符需要用name来标示。

8、实例化的三种方法

<1>、普通的方法不讲了

<2>、静态工厂实例化

<!-- 静态工厂的方法配置bean -->

<bean id="createPerson" class="com.factory.bean.PersonFactory" factory-method="createPerson">class:这是静态工厂的那个类

factory-method:是静态工厂要创建的那个对象

<property name="id" value="123"></property>这些属性值都是要静态工厂创建的对象的一些属性。

<property name="name" value="曹欢"></property>

</bean>

<3>、非静态工厂实例化

<!-- 工厂的方法配置bean -->

<bean id="createPerson1" class="com.factory.bean.PersonFactory"></bean>//这是配置工厂bean

<bean id="person" factory-bean="createPerson1" factory-method="createPerson1">第一个参数是要工厂bean创建的对象的id,第二个参数是工厂beanid,第三个参数是工厂bean创建对象的方法。

<property name="id" value="123124"></property>//这是要工厂bean创建的对象的一些属性

<property name="name" value="caohuan"></property>

</bean>

9bean的作用域(scope)

如果作用域选择的是singleton那么这个bean只会被初始化一次,以后每次调用的都是同一个实例。而且该种状态下容器实例化的时候就会初始化;如果使用的是prototype那么每次调用的时候都会初始化一个实例。

如果想bean在调用的时候在初始化,可以在bean中配置lazy-init="true 如过想所有的beanq全部,可以在beans中配置default-lazy-init="false",但是这些属性一般在开发的时候不配置。还可以在bean中配置初始化方法:init-method="init"

这个init是某个java类中的一个方法,那么在容器初始化那个bean之后,就会执行这个init方法。如果要执行在spring容器关闭之后执行某个操作,destroy-method="destroy" 

关闭spring容器的方法:AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

applicationContext.close();AbstractApplicationContext只有这个类有关闭spring容器的方法。

10、在一个对象中注入另外一个对象

<1>ref注入

<bean name="person"  class="com.pojos.Person" scope="singleton" init-method="display" destroy-method="destroy">

<property name="id" value="1234"></property>

<property name="name" value="曹欢"></property>

<property name="animal" ref="animal"></property>//注入下面那个对象,千万记得在一个对象中注入另外一个对象,一个要在这个对象中设置set(方法)

</bean>

<bean id="animal" class="com.pojos.Animal"></bean>

<2>内部bean注入的方法

<bean name="person"  class="com.pojos.Person" scope="singleton" init-method="display" destroy-method="destroy">

<property name="id" value="1234"></property>

<property name="name" value="曹欢"></property>//value是直接注入值

<property name="animal">

<bean class="com.pojos.Animal"></bean>

</property>

</bean>

11、几种常见的集合的注入值的方法

<property name="set">

<set>

<value>caohuan</value>

<value>zhangsan</value>

<value>你好</value>

</set>

</property>

<property name="list">

<list>

<value>asd</value>

<value>曹欢</value>

<value>曹娟</value>

</list>

</property>

<property name="properties">

<props>

<prop key="key1">a</prop>

<prop key="key2">b</prop>

<prop key="key3">c</prop>

</props>

</property>

<property name="map">

<map>

<entry key="1"><value>a</value></entry>

<entry key="2"><value>b</value></entry>

<entry key="3"><value>c</value></entry>

</map>

</property>

12、关于构造函数注入值的方法

<bean id="dog" class="com.pojos.Dog">

<constructor-arg index="0"><value>asd</value></constructor-arg>

<constructor-arg index="1"><value>12</value></constructor-arg></bean>

13、使用注解的方式注入(手工注入)

<context:annotation-config></context:annotation-config><!-- 这是打开注解器 -->要打开注解器必须要把命名空间打开。

1@Autowired(required=false)[required属性默认情况下为true,表示注解的时候,必须要注入一个值,否者会出现异常;如果为false,表示注解的时候可以为null.]注解是根据类型来查找的; @Autowired(required=false@Qualifier("person")两个注解的结合使用可以让它们按名字来查找。但是只能按名字来查找,如果名字不对,不会再按类型来查找。这是它们与@resource(name=””)的区别。Qualifier---->限定语

2@resource(name=””)如果没有name这个属性,首先是根据名字来查找,然后再根据类型来查找.如果有name这个属性,那么只能按照名字来查找。

注入有两种:1、字段注入 @Resource private Person person;

2、 属性的setter方法注入  

@Resource 

public void setPerson(Person person){ this.person = person;} 

14、自动注入

这是在Dog对象中注入person对象

<bean id="dog" class="com.pojos.Dog" autowire="byName"></bean>

红色部分就是自动注解的地方,有四个值byTypebyNameconstructor、autodetect

千万记得自动注解,也需要set方法,否则无法正确注入。

<bean id="person" class="com.pojos.Person"></bean>

15、扫描器扫描各个类

<context:annotation-config/><1>

<context:component-scan base-package="com"></context:component-scan>会扫描comcom下的所有子包。<2>

它们的工作原理:是对那些注解的都有一个相应的注解解析器,对注解(@resource)进行解析,因为<2>包涵了1的所有的注解,因此只要<2>存在的时候,<1>那句代码不需要写。

扫描的四个注解有:@service主要用于扫描服务层 @Component 可以扫描那些不知道用哪个扫描,就用它 @Controller主要用于扫描控制层 @Repository主要用于扫描Dao层 

其实这四个扫描可以互换,spring现在没有区分它们的区别,可能以后会区分。

这四个都可以指明名字如:@Service(person)如果不指明,那么默认就是该类的名字(第一个大写的字母变为小写,还可以指定该类的作用域:@Scope(singletonprototype),还可以指明该类在被spring容器实例化后,在被调用之前,指明初始化方法

@PostConstruct 指明在容器关掉之后要调用的方法,可以再该方法上注解 @PreDestroy

16、动态代理(动态代理的方法必须都实现接口)

<1>JDK动态代理

public class TestProxy implements InvocationHandler{

private Object object;

public Object createTestProxyObject(Object object)

{  

this.object = object;

return Proxy.newProxyInstance(this.object.getClass().getClassLoader(), this.object.getClass().getInterfaces(), this);

//第一个参数是得到代理对象的类加载器,第二个参数是得到代理对象的接口(所有如果是代理对象,那么代理对象的所有方法必须实现接口编程。第三个参数是要实现InvocationHandler这个接口)

}

public Object invoke(Object proxy, Method method, Object[] args)

throws Throwable {

Object result = null;

TestService service = (TestService) object;

Test test = service.getTest();

if(test.getName() != null)

{

result = method.invoke(object, args);

//将代理对象(proxy)的所有方法带上参数传递给目标对象。 

}

return result;

}

}

<2>CGlib的动态代理(需要导入cglib这个包)这种代理是没有实现接口的时候,可以使用。

千万要记得,用这种方法代理,那么代理的对象必须要有一个不带参数的构造函数。否者会出错。但是上序那种方法代理不会出现这种情况。

public class CGLibTestProxy implements MethodInterceptor{

private Object targetObject;

public Object createProxyIntance(Object targetObject){

this.targetObject = targetObject;

Enhancer enhancer = new Enhancer();//创建一个Enhance

enhancer.setSuperclass(this.targetObject.getClass());//代理对象的final方法的才能被代理,这句代码是将代理对象付给enhancer的父类,它的父类已经将代理对象的所有方法进行处理。

enhancer.setCallback(this);//这是一个回调方法

return enhancer.create();//这是创建一个被enhancer处理后的代理对象

}

public Object intercept(Object proxy, Method method, Object[] args,

MethodProxy  methodProxy) throws Throwable {

//第一参数是要代理的对象,第二个参数是代理对象的一些方法,第三个参数代理对象方法的一些参数,第四个参数是代理对象的方法。

TestService bean = (TestService) this.targetObject;

Object result = null;

result = methodProxy.invoke(targetObject, args);

return result;

}

}

17aop的切面

18aop切面的详细

<aop:aspectj-autoproxy></aop:aspectj-autoproxy><!-- 这是打开切面注解器的,不需要,就算有<context:component-scanbase-package="com"></context:component-scan>

也必须要 -->

@Component  @Aspect//这个是声明该类为一个切面

public class AllAspect {

//这是生命一个切面

@Pointcut ("execution(* com.service..*.*(..))")//千万拦截方法的方式不能写错,连续两点的地方表示,拦截该包即子包下的类,如果只写一点,表示只拦截它下面的包,不拦截子包 类

private void anyMethod(){}//给切面名名一个名字,只不过这个名字比较特殊,是一个方法

@Before("anyMethod()")//千万记得在括号内写切面的名字的时候一定要加上()

public void beforeAdvice()

{

System.out.println("这是前置通知");

}

}

19、这是关于代码切面的详细//这个例子是关于做权限的

@Component  @Aspect//这个是声明该类为一个切面

public class AllAspect {

//这是生命一个切面

@Pointcut ("execution(* com.service..*.*(..))")//千万拦截方法的方式不能写错,连续两点的地方表示,拦截该包即子包下的类,如果只写一点,表示只拦截它下面的包,不拦截子包 类

private void anyMethod(){}//给切面名名一个名字,只不过这个名字比较特殊,是一个方法

@Before("anyMethod() && args(name)")//这是前置通知,在前置通知里写上参数后,表示拦截的是("execution(* com.service..*.*(..))")并且该方法带有一个参数,并且该参数的类型是String

public void beforeAdvice(String name)

{

System.out.println("这是前置通知" + name);

}

@AfterReturning(pointcut = "anyMethod()", returning = "result")//这是后置通知,加上returning参数表示返回的结果,返回的是String类型

public void AfterAdvice(String result)

{

System.out.println("这是后置通知" + result);

}

@After("anyMethod()")//这是最终通知

public void finalAdvice() {

System.out.println("这是最终通知");

}

@AfterThrowing(pointcut = "anyMethod()", throwing = "e")//这是例外通知

public void exceptionAdvice(Exception e) {

System.out.println("这是例外通知" + e);

}

@Around("anyMethod()")

public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {

System.out.println("这是进入方法");

Object object = pjp.proceed();//如果有几个切面。首先会调用其他的切面,环绕通知最好做权限问题。

System.out.println("这是退出方法");

return object;

}

}

这是只有一个切面的结果。

这是有两个切面的结果

切面的bean

<bean id="allAspect" class="com.aspect.AllAspect"></bean>

 <!--  xml切面的配置-->

 <aop:config>

 <aop:aspect id="mycut" ref="allAspect">

 <aop:pointcut expression="execution(* com.service.Person.*(..))" id="mypoint"/>

 <aop:before method="beforeAdvice" pointcut-ref="mypoint" />

 <aop:after-returning method="AfterAdvice"  pointcut-ref="mypoint"/>

 <aop:after method="finalAdvice" pointcut-ref="mypoint"/>

 <aop:after-throwing method="exceptionAdvice" pointcut-ref="mypoint" />

 <aop:around method="aroundAdvice" pointcut-ref="mypoint"/>

 </aop:aspect>

 </aop:config>

//注意:execution(void com.service.Person.*(..))//拦截没有返回值的方法

execution(void com.service.Person.*(..))//拦截非没有返回值的方法

execution(* com.service.Person.*(java.lang.String, ..))//拦截第一个参数

String类型,后面不管有几个参数

<bean id="allAspect" class="com.aspect.AllAspect"></bean>

 <!--  xml切面的配置-->是上面的一些补充

 <aop:config>

 <aop:aspect  ref="allAspect">

 <aop:pointcut expression="execution(* com.service.Person.*(..))" id="mypoint"/>

 <aop:before method="beforeAdvice"  pointcut-ref="mypoint"/>

 <aop:after-returning method="AfterAdvice"  pointcut-ref="mypoint" returning="name"/>//这是后置通知有返回值,一定得记得在写的后置通知的方法里面必须要要有一个参数

 <aop:after method="finalAdvice" pointcut-ref="mypoint"/>

 <aop:after-throwing method="exceptionAdvice" pointcut-ref="mypoint" throwing="e"/>//这是例外通知有异常,一定得记得在写的例外通知的方法里面必须要要有一个参数

 <aop:around method="aroundAdvice" pointcut="execution(* com.service.Person.*(java.lang.String)) and args(name)"/>

 </aop:aspect>//这是环绕通知,本来只可以用那个固定的方法,接收固定的参数,这样配置可以接收其他的参数

 </aop:config>

<!可惜这里的前置通知里面接收参数没有测试出来 -->

20Spring + jdbc继承开发

<1>xml文件的配置

<!-- 这是配置数据源 -->

<1>这是从jdbc.Properties文件中读取文件

<context:property-placeholder location="classpath:jdbc.properties"/>

这是打开从jdbc.Properties读取文件的注解器。

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="${driverClassName}"></property>

<property name="url" value="${url}"></property>

<property name="username" value="${username}"></property>

<property name="password" value="${password}"></property>

<property name="initialSize" value="${initialSize}"></property>

<property name="maxActive" value="${maxActive}"></property>

<property name="maxIdle" value="${maxIdle}"></property>

<property name="minIdle" value="${minIdle}"></property>

</bean>

---------------------------------------------------------------------

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>

<property name="url" value="jdbc:mysql://localhost/caohuan?useUnicode=true&characterEncoding=GBK"></property>

<property name="username" value="root"></property>

<property name="password" value="caohuan"></property>

<property name="initialSize" value="2"></property>

<property name="maxActive" value="500"></property>

<property name="maxIdle" value="2"></property>

<property name="minIdle" value="5"></property>

</bean>

<bean id="testDao" class="com.dao.TestDao">

<property name="dataSource" ref="dataSource"></property>

</bean>

<!-- 事物管理器 -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"></property>

</bean>

<!-- 打开事物注解 -->

<tx:annotation-driven/>

<2>dao的写法

package com.dao;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.RowMapper;

import org.springframework.transaction.annotation.Transactional;

import com.pojos.Test;

@Transactional

public class TestDao {

private DataSource dataSource;

private  JdbcTemplate jdbcTemplate  = null;

public void setDataSource(DataSource dataSource) {

jdbcTemplate  = new JdbcTemplate(dataSource);

}

public void save(Test test) {

//注意保存时是用的update方法

jdbcTemplate.update("insert into test(name) values(?)"new Object[]{test.getName()}, new int[]{java.sql.Types.VARCHAR});

}

public void update(Test test) {

jdbcTemplate.update("update test set name = ?  where id = ?"new Object[]{test.getName(), test.getId()}, new int[]{java.sql.Types.VARCHAR, java.sql.Types.INTEGER});

}

@Transactional()

//通过设置设置noRollbackFor可以设置不管什么异常,都不会回滚,rollbackFor则相反

//noRollbackFor = RuntimeException.class

//rollbackFor = Exception.class;

//rollbackForClassName = "Exception"

public void delete(Test test) {

//注意删除时是用的update方法

jdbcTemplate.update("delete  from test  where id = ?"new Object[]{ test.getId()}, new int[]{ java.sql.Types.INTEGER});

//throw new RuntimeException();//抛出一个运行时的错误,事物会回滚,不会去删除

//throw new Exception();//抛出一个非运行时的错误,还是会去删除数据。}

@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)public Test getPerson(Test test) {

//new RowMapper()是方法的回调,返回一个值

return (Test)jdbcTemplate.queryForObject("select * from test  where id = ?"new Object[]{ test.getId()}, new int[]{ java.sql.Types.INTEGER}, new RowMapper(){

public Object mapRow(ResultSet rs, int arg1) throws SQLException {

Test test = new Test();

//可能会问为什么不需要rs.next()这是因为当被调用的时候,会自动调用这句代码。

test.setName(rs.getString("name"));

return test;

}

});

}

@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)

//数据只可读,因为查询不需要事物,所以设置为不需要事物  propagation = Propagation.NOT_SUPPORTED

public List<Test> getPersons() {

return (List<Test>)jdbcTemplate.query("select * from test"new RowMapper(){

public Object mapRow(ResultSet rs, int arg1) throws SQLException {

Test test = new Test();

//可能会问为什么不需要rs.next()这是因为当被调用的时候,会自动调用这句代码。

test.setName(rs.getString("name"));

return test;

}

});

}

}

//注意:spring对运行期异常(check)会回滚,但是如果不是运行期异常(uncheck)不会回滚

@Transactional(readOnly = true)//里面参数的一些解释

//timeout = 10000事物的超时时间

//readOnly = true数据只可读

//isolation = Isolation.READ_UNCOMMITTED

//propagation = Propagation.NESTED

事物的传播途径

nested的事物传播途径,内部事务不会影响到外部事物。

事物隔离级别(熟悉脏读、不可重复读、幻读)

21ssh的整合

<1>整合需要的包

对由监听器其对spring容器进行实例化

Spring继承struts的关键点

Spring的配置文件的写法

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" >

<!-- 这是扫描器 -->

<context:component-scan base-package="com"></context:component-scan>

<!-- 这是从jdbc.properties文件中读取数据的监听器 -->

<context:property-placeholder location="classpath:jdbc.properties"/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="${driverClassName}"></property>

<property name="url" value="${url}"></property>

<property name="username" value="${username}"></property>

<property name="password" value="${password}"></property>

<property name="initialSize" value="${initialSize}"></property>

<property name="maxActive" value="${maxActive}"></property>

<property name="maxIdle" value="${maxIdle}"></property>

<property name="minIdle" value="${minIdle}"></property>

</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource"></property>

<property name="mappingResources">

<list>

<value>com/pojos/Person.hbm.xml</value>

</list>

</property>

<property name="hibernateProperties">

<value>

hibernate.dialect = org.hibernate.dialect.MySQLDialect

hibernate.show_sql = true

hibernate.hbm2ddl.auto = create

hibernate.format_sql = true

<!-以下两句是使用二级缓存,还需要在需要缓存的pojos的类下配置<cache usage="read-only"/> -->

hibernate.cache.use_second_level_cache = true

hibernate.cache.provider_class = org.hibernate.cache.EhCacheProvider

</value>

</property>

</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

<!-- 这是事物监听器的打开 -->

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean name="/action" class="com.actions.PersonAction"></bean>

</beans>

Web.xml配置文件的写法

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <servlet>

    <servlet-name>action_tmp</servlet-name>

    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

    <init-param>

      <param-name>config</param-name>

      <param-value>/WEB-INF/struts-config.xml</param-value>

    </init-param>

    <init-param>

      <param-name>debug</param-name>

      <param-value>3</param-value>

    </init-param>

    <init-param>

      <param-name>detail</param-name>

      <param-value>3</param-value>

    </init-param>

    <load-on-startup>0</load-on-startup>

  </servlet>

  <servlet-mapping>

  <servlet-name>action_tmp</servlet-name>

  <url-pattern>*.do</url-pattern>

  </servlet-mapping>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:app*.xml</param-value>

  </context-param>

  <listener>

  <listener-class>

  org.springframework.web.context.ContextLoaderListener

  </listener-class>

  </listener>

  

  <filter>

    <filter-name>chineaseFilter</filter-name>

    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

    <init-param>

      <param-name>encoding</param-name>

      <param-value>utf-8</param-value>

    </init-param>

  </filter>

  <filter-mapping>

    <filter-name>chineaseFilter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <filter>

    <filter-name>openSessionInView</filter-name>

    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

  </filter>

  <filter-mapping>

    <filter-name>openSessionInView</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

</web-app>

这是struts-config.xml的写法

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>

<action-mappings>

<!--type="org.springframework.web.struts.DelegatingActionProxy"是将action交给spring管理  -->

<action path="/action" type="org.springframework.web.struts.DelegatingActionProxy" validate="false">

<forward name="showAllPerson" path="/WEB-INF/showAllPerson.jsp"></forward>

</action>

</action-mappings>

<!--这是将action交给spring管理,上面再type里面写的也是交给spring管理,两种方式写任何一种都可以  -->

<!--  <controller>

<set-property value="org.springframework.web.struts.DelegatingRequestProcessor" property="processorClass"/>

</controller>-->

<!--  千万记得资源文件的书写,否则做国际化的时候会出现问题 -->

<message-resources parameter="com.yourcompany.struts.ApplicationResources.properties"></message-resources>

</struts-config>

这是dao的写法

package com.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import com.pojos.Person;

@Transactional @Service

public class PersonDao {

@Resource private SessionFactory factory;

public void save(Person person) {

//factory.getCurrentSession()这是得到由sessionFactory管理的session,不能用openSession因为那不是由sessionFactory管理的类

factory.getCurrentSession().save(person);

}

public void update(Person person) {

factory.getCurrentSession().update(person);

}

public void delete(Person person) {

factory.getCurrentSession().delete(person);

}

public Person getPerson(Person person) {

Person per = (Person) factory.getCurrentSession().load(Person.class, person.getId());

return per;

}

public List<Person> getPersons() {

List<Person> list = factory.getCurrentSession().createQuery("from Person").list();

return list;

}

}

下面两句诗在action中实例化spring的写法。

ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());

//ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());

//只看到低28个视屏。

注意:用myEclipse自带的tomacat(5.0)不能监听到spring的打开。会出现异常

原创粉丝点击