spring2.5 注解

一.  spring注解

1.准备工作
(1)导入common-annotations.jar
(2)导入schema文件 文件名为spring-context-2.5.xsd
(3)在xml的beans节点中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans 
       .......
       xmlns:context="http://www.springframework.org/schema/context"

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

.....
   <!--将针对注解的处理器配置好  -->
  <context:annotation-config />
.....
<beans>

2.在java代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:
@Autowired默认按类型装配,@Resource默认按名称装配,当找不到名称匹配的bean才会按类型装配。


默认注解
@Resource  private PersonDao persondao;
<bean id="personDao" class="com.hf.dao.impl.PersonDaoBean"></bean>
首先是判断persondao是否与xml里的personDao名字相同,相同则注入,
不同则判断persondao是否是com.hf.dao.impl.PersonDaoBean类型,是则注入不是则返回null.

@Resource(name="personDao")  private PersonDao dao;
<bean id="personDao" class="com.hf.dao.impl.PersonDaoBean"></bean>
判断name名称是否与bean中id相同不同则返回null

Spring自带注解方式
@Autowired @Qualifier("personDao") private PersonDao persondao;
默认是按类型注入  加上@Qualifier("personDao")则按名称注入

3.通过在classpath 自动扫描方式把组件纳入spring容器中管理
spring2.5为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@Component @Service @Controller @Repository
注解的类,并将这些类纳入进spring容器中管理。它们的作用和xml文件中使用bean 节点配饰组件是一样的。
(1)使用到了注解的功能(需要注解准备工作的内容)
(2)在xml中加入
<context:component-scan base-package="com.hf" />
其中base-package 为需要扫描的包(包含子包)
(3)
@Service用于标注业务层组件
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件 ,即DAO 组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

(4)
业务类
@Service
public class PersonServiceBean implements PersonService {.....}
输出类
AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService= (PersonService)cxt.getBean("personServiceBean");
System.out.println(personService);
cxt.close();
使用注解中bean的id默认名称为类名称的首字母小写名称
--------------------------------------------------
自己指定名称
@Service("aa") //默认作用域范围 是单例范围
public class PersonServiceBean implements PersonService {.....}
输出类
AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService= (PersonService)cxt.getBean("aa");
System.out.println(personService);
cxt.close();
--------------------------------------------------
@Service("aa") @Scope("prototype")//修改bean的作用域
public class PersonServiceBean implements PersonService {....}


-----------------------------------------------------------
   @PostConstruct
   public void init(){ 
    System.out.println("初始化");
   }
   @PreDestroy
   public void destory(){  
    System.out.println("释放资源");
   }

 

4.AOP注解方式
(1)准备工作:
.导入common-annotations.jar  aspectjrt.jar aspectweaver.jar cglib-nodep-2.13.jar
.导入schema文件 文件名为spring-aop-2.0.xsd
.在xml的beans节点中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans 
       .......
       xmlns:aop="http://www.springframework.org/schema/aop"

       xsi:schemaLocation="
           .......
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"  >

.....
    <!-- 配置解释处理器 为@AspectJ注解提供支持  -->
  <aop:aspectj-autoproxy />
.....
<beans>

(2)
<bean id="myInterceptor" class="com.hf.service.impl.MyInterceptor"></bean>
<bean id="personService" class="com.hf.service.impl.PersonServiceBean" ></bean>
将切面和被拦截的类交给spring管理
(3)切面类

@Aspect //定义切面类
public class MyInterceptor {
   /**
    *  @Pointcut("execution(* com.hf.service..*.*(..))")表达式含义
    * 第一个* 表示返回值类型为任意类型
    * com.hf.service..  两个点表示包路径下的子包的类也要拦截
    * com.hf.service..*.* 子包的所有类中的所有方法 第一个*是方法第二个*是类
    * (..)代表方法参数随意 可有可无可多可少
    * **/

   @Pointcut("execution (* com.hf.service.impl.PersonServiceBean.*(..))")// 定义切入点
   private void andMethod()//声明一个切入点
   {}
  
/*   @Before("andMethod()")
   public void doAccessCheck(){
    System.out.println("前置通知");   
   }
   */
  
   @Before("andMethod() && args(name)") //带参数 只拦截符合参数类型的方法
   public void doAccessCheck(String name){
    System.out.println("前置通知"+name);   
   }
  
  
/*   @AfterReturning("andMethod()")
   public void doFaterReturning(){   
    System.out.println("后置通知");
   }*/
  
   @AfterReturning(pointcut="andMethod()",returning="result")//带返回值的 无返回值的方法 result为null
   public void doFaterReturning(String result){//拦截方法执行后 获取返回值对象
    System.out.println("后置通知:"+result);
   }
  
  
   @After("andMethod()")
   public void doAfter(){
    System.out.println("最终通知"); 
   }
  
/*   @AfterThrowing("andMethod()")
   public void doAfterThrowing(){   
    System.out.println("例外通知");
   }*/
  
   @AfterThrowing(pointcut="andMethod()" , throwing="e") //获取例外并打印
   public void doAfterThrowing(Exception e){  
    System.out.println("例外通知:"+e);
   }
  
  
   @Around("andMethod()")//环绕通知
   public Object doBasecProfiling(ProceedingJoinPoint pjp )throws Throwable{
   //if(){//判断是否与权限
    System.out.println("进入通知");
    Object result = pjp.proceed();
    System.out.println("离开 通知");
   //}
    return result;
   
   
   }
}

(4)业务类 PersonServiceBean
public class PersonServiceBean implements PersonService {

 public void save(String name){
      throw new RuntimeException("纯属例外");
      // System.out.println("我是Save方法"+name);
    }
 public String update() { 
  return "我是update方法";
 }
}
----------------------------------------------------------

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Configuration注解是一种用于配置Spring应用程序的注解方式。它提供了一种简化配置的方式,可以通过注解来声明和管理Spring Bean,以及配置和解决Bean之间的依赖关系。 Spring提供了多个@Configuration注解相关的注解,包括@ComponentScan、@EnableAutoConfiguration和@Import等。 @ComponentScan注解用于自动扫描指定的包路径,将标有@Component、@Service、@Repository等注解的类注册为Spring Bean。它还可以通过指定excludeFilters和includeFilters来控制扫描过程中的过滤规则。 @EnableAutoConfiguration注解用于启用Spring Boot自动配置机制,它会根据classpath中的jar包和类路径来自动配置Spring应用程序所需的Bean和环境。它基于@SpringBootApplication注解,并通过@SpringBootApplication注解的派生性质来实现。 @Import注解用于将其他的配置类导入到当前配置类中,从而实现配置类的组合和复用。被导入的配置类可以是普通的@Configuration注解标注的类,也可以是带有@Bean注解的方法所在的类。 除了上述几个主要的注解外,Spring Configuration注解还包括了@Bean注解用于声明一个方法可以生成一个Spring Bean,@PropertySource注解用于加载外部的配置文件,@Value注解用于注入配置文件中的属性值等。 总的来说,Spring Configuration注解提供了一种简洁而灵活的方式来配置和管理Spring应用程序,可以通过注解的方式来声明和注册Spring Bean,实现依赖注入,同时还可以通过其他注解来加载配置文件和注入属性值,进一步提高应用程序的可维护性和扩展性。 ### 回答2: Spring注解配置是指使用注解来配置Spring应用程序的一种方式。在过去,Spring配置主要使用XML文件进行,但自从Spring 2.5版本推出以来,通过注解配置的方式逐渐成为主流。 使用注解配置可以简化开发人员的工作,使配置更加集中和易于管理。在使用注解配置时,只需在需要配置的类或方法上添加特定的注解,就能实现相应的配置。 常见的Spring注解配置包括以下几类: 1. @Configuration: 它是一个类级别的注解,用于标记一个类作为配置类。配置类中的方法通过@Bean注解来声明一个 bean 对象。 2. @ComponentScan: 它是一个类级别的注解,用于自动扫描并注册Spring bean。在注解中可以指定要扫描的包路径或类路径,Spring会自动扫描该路径下的所有类,并将其注册为Spring bean。 3. @Autowired: 它是一个字段级别的注解,用于自动装配Spring bean。当一个类中需要依赖其他类的实例时,使用该注解可以让Spring自动将依赖对象注入到类中。 4. @Value: 它是一个字段级别的注解,用于注入外部配置的值。通过该注解可以将配置文件中的值注入到类的字段中。 通过使用Spring注解配置,可以更加方便地管理和维护Spring应用程序。它减少了繁琐的XML配置,使开发人员能够更专注于业务逻辑的实现。同时,注解配置也使代码更加简洁和易于阅读。但需要注意的是,过度使用注解可能会导致代码可读性下降,所以在使用注解时要慎重选择合适的场景。 ### 回答3: Spring Configuration注解是一种用于配置和管理Spring应用程序的方式。它可以帮助我们简化配置,提高开发效率。 Spring Configuration注解主要包括以下几种: 1. @Configuration:将类标记为配置类,用于替代传统的XML配置文件。在配置类中,我们可以使用其他注解来定义Bean和配置项。 2. @ComponentScan:启用组件扫描,可以自动扫描并注册带有@Component注解的类作为Bean。可以通过指定扫描路径来限制扫描的范围。 3. @Bean:用于定义Bean,在被@Configuration注解标记的类中的方法上使用。方法返回的对象将作为Bean注册到Spring容器中,可以通过依赖注入的方式使用。 4. @Autowired:用于进行依赖注入,将被注解的字段、构造方法或者Setter方法的参数与容器中的Bean进行关联。它可以根据类型进行自动匹配或者通过@Qualifier注解根据名称进行匹配。 5. @Value:用于将配置项注入到Bean中,可以用来注入配置文件中的值,也可以用来注入通过Spring EL表达式计算的值。 除了以上的注解Spring Configuration还提供了一些其他注解,如@Profile用于定义不同环境下的配置,@Import用于导入其他配置类,@PropertySource用于指定配置文件等。 通过使用Spring Configuration注解,我们可以摆脱繁琐的XML配置文件,更加简洁地配置和管理Spring应用程序。它使得代码结构更加清晰,便于维护和扩展。同时,通过注解方式配置的应用程序更加便于测试和集成,提高了整体开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值