Spring 学习笔记(四)IOC之注解方式

用注解向IOC容器增加javaBean配置,还有一些注入的注解

第一步:添加注解的jar包

spring-aop-4.3.3.RELEASE.jar

第二步:applicationContext.xml文件中开启注解

注意beans标签内增加了context的名称空间和约束

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <!--引入context的名称空间-->
    <!--开启注解,配置要扫描的包-->
    <context:component-scan base-package="org.spring.exampleAOP" />
</beans>

第三步:就可以使用注解喽

  • 创建对象,四种注解的效果是一样的,估计就是传说中的约定吧
  1. @Component  创建对象 普通Spring项目
  2. @Repository   创建对象 持久层  分层项目
  3. @Service        创建对象 Service层   分层项目
  4. @Controll       创建对象 web层   分层项目

@Scope("参数"),还是单例/多例/session/request的意思

@Component //相当于bean标签, beanName默认首字母小写
@Scope("singleton") //等同bean标签中的scope
public class Sadamu implements People {
  • 注入

1. 普通注入:@Value("${people}"),支持EL表达式, 这个用的挺多的还,比如项目中经常在配置文件中设置全局变量啥的(比如站点名称、jdbc配置啥的)

现在applicationContext.xml中装入properties文件,然后EL表达式就可以取到啦。

 <!--装入配置文件 properties-->
    <context:property-placeholder location="classpath:my.properties" />

2. 对象注入

@Autowired 自动装配(按类型) , 可以在描述接口,自动装配一个接口的实现类, 只能是一个,不能没有实现类。

扫描二维码关注公众号,回复: 4525717 查看本文章

当出现多个实现类的时候,必须用@Qualifer(value="XXX")注解进行说明要注入哪个实现类,否则会报错。

@Component //相当于bean标签, beanName默认首字母小写
@Scope("singleton") //等同bean标签中的scope
public class Sadamu implements People {

    //自动装载,自动根据变量类型进行注入, 如果IOC容器没有这个类就会报错, 有多个也不行
    @Autowired
    @Qualifier(value = "kazhafei") //当Autowired出现多个实现的时候, 必须用Qualifier指定注入名称
    public People people;

@Resource 注解是@Autowired和@Qualifer注解的合体,既能按类型注入也能按名称注入,推荐使用

    @Resource(name = "kazhafei") //@Autowired(根据类型注入)和@Qualifier(选择名称)两个注解的合体,
    public Kazhafei kazhafei;

另:@Autowired来自spring,@Resource来自javaEE

package org.springframework.beans.factory.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
    boolean required() default true;
}
-----------------------------------------------------------------------------
package javax.annotation;

import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*; 
@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)
public @interface Resource {....
  • 其他注解(部分,日后慢慢填吧)
    @PostConstruct //初始化方法注解
    public void init(){
        System.out.println("IOCinit:萨达姆初始化");
    }

    @PreDestroy  //销毁方法注解
    public void destroy(){
        System.out.println("IOCdestroy:萨达姆销毁");
    }

猜你喜欢

转载自blog.csdn.net/baitianmingdebai/article/details/84997714