视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
spring2.5基于注解和XML等配置应用
2025-10-02 06:09:42 责编:小OO
文档
三种实例化Bean的方法

1.使用类构造器

2.使用静态类工厂

3.使用实例类工厂

控制反转的实现-依赖注入

xml注入

注入其他bean

外部bean

    

        

    

内部bean (最好不要被其他bean使用)

        

            

        

    

注入属性

    

        

    

构造器注入

    

    

        

    

注解注入

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

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

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

    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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    

在要注入的属性中使用@Resource 或set方法上(是javaee的注解方式 推荐使用)

    @Autowire @Qulifier(“person”)(Spring 的注解方式)

自动扫描装配

在相应的类上加上注解

@Service     服务类

@Repository   dao类

@Controller   控制类

@Component  分类不清的类    

@Scope(“prototype”) 随时创建   作用范围(单例/随时创建)

@PostConstruct 初始化方法

AOP

切面

切入点

连接点

通知

    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:p="http://www.springframework.org/schema/p"

    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">

    

基于XML开发

准备普通切面类

public class XMLPersonServiceAOP {

    //前置通知:

    public void doSomethingBefore(String name) {

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

    }

    //后置通知

    public void doSomethingAfter(String personName) {

        System.out.println("后置通知:"+personName);

    }

    //最终通知

    public void doSomethingEnd() {

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

    }

     //例外通知

    public void doThrow(Exception e) {

        System.out.println("例外通知:");

    }

    //环绕通知

    public Object doAround(ProceedingJoinPoint pjp) throws Throwable{

        //if(){//判断用户是否有权限

        Object result=pjp.proceed();

        //}

        System.out.println("环绕通知:");

        return result;

    }

}

Xml配置

    

        

            

            

            

            

            

                    

        

    

基于注解的方式开发

在xml中配置

@Aspect @Component

public class PersonServiceAOP {

    @Pointcut("execution(* com.oseica.service.PersonService.*(..))")

    private void anyMehtod() {}// 声明一个切入点

    

    @Before("anyMehtod() && args(name)")//前置通知:

    public void doSomethingBefore(String name) {

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

    }

    @AfterReturning(pointcut="anyMehtod()",returning="personName")//后置通知

    public void doSomethingAfter(String personName) {

        System.out.println("后置通知:"+personName);

    }

    @After("anyMehtod()")//最终通知

    public void doSomethingEnd() {

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

    }

    @AfterThrowing(pointcut="anyMehtod()",throwing="e") //例外通知

    public void doThrow(Exception e) {

        System.out.println("例外通知:");

    }

    @Around("anyMehtod()")

    public Object doAround(ProceedingJoinPoint pjp) throws Throwable{

        //if(){//判断用户是否有权限

        Object result=pjp.proceed();

        //}

        System.out.println("环绕通知:");

        return result;

    }

}

Spring与 JDBC  事务

Annotation的方式(注解)

文件Jdbc.properties 如下:

driverClassName=org.gjt.mm.mysql.Driver

url=jdbc\\:mysql\\://localhost\\:3306/itcast?useUnicode\\=true&characterEncoding\\=UTF-8

username=root

password=123456

initialSize=1

maxActive=500

maxIdle=2

minIdle=1

Spring xml文件如下

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

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

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

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

       xsi:schemaLocation="http://www.springframework.org/schema/beans

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

           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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">

    

    

    

    

    

    

    

  

    

    

    

使用:

在类前加 @Transactional

对某方法的check例外进行回滚 @Transactional(runbackFor=Exception.Class)

对某方法不执行事务 @Transactional(propagation=Propagation.NOT_SUPPORTED)

XML的事务配置

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

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

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

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

       xsi:schemaLocation="http://www.springframework.org/schema/beans

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

           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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">

    

    

    

    

    

    

    

  

    

      

      

    

    

    

    

    

集成

xml方式

Spring          

JAR 包

aspectjrt.jar

aspectjweaver.jar

cglib-nodep-2.1_3.jar

common-annotations.jar

commons-dbcp.jar

commons-logging.jar

commons-pool.jar

log4j-1.2.15.jar

spring.jar

spring-webmvc-struts.jar

bean.xml

    

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

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

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

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

       xsi:schemaLocation="http://www.springframework.org/schema/beans

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

           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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">

     

            class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

                    value="com.microsoft.sqlserver.jdbc.SQLServerDriver">

        

                    value="jdbc:sqlserver://localhost:1433;databaseName=zf">

        

        

        

        

         

         

         

         

         

         

         

    

    

     

         

         

            

              cn/oseica/entity/Person.hbm.xml

            

         

         

            

                hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

                hibernate.hbm2ddl.auto=update

                hibernate.show_sql=false

                hibernate.format_sql=false

                hibernate.cache.use_second_level_cache=true

                   hibernate.cache.use_query_cache=false

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

              

         

    

    

    

          

    

    

    

        

    

    

    

          

          

     

    

          

            

            

          

    

    

    

    

        

    

启动配置

Web.xml中

contextConfigLocation

classpath:beans.xml

org.springframework.web.context.ContextLoaderListener

或在struts配置文件中

hibernate

    JAR包

    antlr-2.7.6.jar

    commons-collections-3.1.jar

    dom4j-1.6.1.jar

    ehcache-1.2.3.jar

    ejb3-persistence.jar

    hibernate3.jar

    hibernate-annotations.jar

    hibernate-cglib-repack-2.1_3.jar

    hibernate-commons-annotations.jar

    hibernate-entitymanager.jar

    

Struts1.2

  

    action

    org.apache.struts.action.ActionServlet

    

      config

      /WEB-INF/struts-config.xml

    

    

      debug

      3

    

    

      detail

      3

    

    0

  

  

    action

    *.do

  

Struts2

struts2

org.apache.struts2.dispatcher.FilterDispatcher

struts2

/*

OpenSessionInViewFilter

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

OpenSessionInViewFilter

/*

Annotation方式

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

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

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

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

       xsi:schemaLocation="http://www.springframework.org/schema/beans

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

           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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">

     

     

     

         

     

            class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

                    value="com.microsoft.sqlserver.jdbc.SQLServerDriver">

        

                    value="jdbc:sqlserver://localhost:1433;databaseName=zf">

        

        

        

        

         

         

         

         

         

         

         

    

    

     

         

         

            

              cn/oseica/entity/Person.hbm.xml

            

         

         

            

                hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

                hibernate.hbm2ddl.auto=update

                hibernate.show_sql=false

                hibernate.format_sql=false

                hibernate.cache.use_second_level_cache=true

                   hibernate.cache.use_query_cache=false

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

              

         

    

    

    

          

    

    

    

在类中使用注解

如上各知识点

spring提供的乱码解决方案

在web.xml中配置    

     encoding

     org.springframework.web.filter.CharacterEncodingFilter

    

         encoding

         UTF-8

    

     encoding

        /*

open session in view

OpenSessionInViewFilter

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

OpenSessionInViewFilter

            /*

下载本文

显示全文
专题