最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

spring2.5基于注解和XML等配置应用

来源:动视网 责编:小OO 时间:2025-10-02 06:09:42
文档

spring2.5基于注解和XML等配置应用

三种实例化Bean的方法1.使用类构造器2.使用静态类工厂3.使用实例类工厂控制反转的实现-依赖注入xml注入注入其他bean外部bean内部bean(最好不要被其他bean使用)注入属性构造器注入注解注入在要注入的属性中使用@Resource或set方法上(是javaee的注解方式推荐使用)@Autowire@Qulifier(“person”)(Spring的注解方式)自动扫描装配在相应的类上加上注解@Service服务类@Repositorydao类@Controller控制类@Comp
推荐度:
导读三种实例化Bean的方法1.使用类构造器2.使用静态类工厂3.使用实例类工厂控制反转的实现-依赖注入xml注入注入其他bean外部bean内部bean(最好不要被其他bean使用)注入属性构造器注入注解注入在要注入的属性中使用@Resource或set方法上(是javaee的注解方式推荐使用)@Autowire@Qulifier(“person”)(Spring的注解方式)自动扫描装配在相应的类上加上注解@Service服务类@Repositorydao类@Controller控制类@Comp
三种实例化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

            /*

文档

spring2.5基于注解和XML等配置应用

三种实例化Bean的方法1.使用类构造器2.使用静态类工厂3.使用实例类工厂控制反转的实现-依赖注入xml注入注入其他bean外部bean内部bean(最好不要被其他bean使用)注入属性构造器注入注解注入在要注入的属性中使用@Resource或set方法上(是javaee的注解方式推荐使用)@Autowire@Qulifier(“person”)(Spring的注解方式)自动扫描装配在相应的类上加上注解@Service服务类@Repositorydao类@Controller控制类@Comp
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top