• 04spring注解


    注解功能
    1)配置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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.fz.entity"/>
    </beans>
    2)注解使用
    @Component 如果不写,则使用类名小写作为名称

    @Component("uuu")

    @Data @Component
    public class Book {
    @Value("100")
    private int id;

    @Value("《mysql数据库技术》")
    private String name;
    }

    @Autowired 根据类型自动装配


    @Autowired //根据类型自动装配 byType

    @Autowired @Qualifier("user") //根据名称自动装配 byName
    private User user;

    @Resource(name="book")


    3)注解bean类方法
    注解bean类 com.entity.Db类
    ---------------------------------------------------
    @Component("db")
    public class Db {
    public void show(){
    System.out.println("dbshow");
    }
    }

    @Controller("member")
    public class Member {
    public void save(){
    System.out.println("saveok");
    }
    }

    @Service("m")
    public class Member {
    public void save(){
    System.out.println("saveok");
    }
    }

    @Repository("mm")
    public class Member {
    public void save(){
    System.out.println("saveok");
    }
    }
    4)注解单例 多例
    ---------------------------------------------------
    @Repository("mm") @Scope("singleton") 单例默认
    public class Member {

    }

    @Repository("mm") @Scope("prototype") 多实例
    public class Member {

    }

    5)注解初始化方法
    import javax.annotation.PostConstruct;
    ---------------------------------------------------
    @PostConstruct
    public void a(){
    System.out.println("初始化1");
    }

    @PostConstruct
    public void init(){
    System.out.println("初始化2");
    }

    6)注解销毁方法
    import javax.annotation.PreDestroy;
    ---------------------------------------------------
    @PreDestroy
    public void close(){
    System.out.println("退出");
    }

    @PreDestroy
    public void exit(){
    System.out.println("销毁");
    }


    7)属性注入
    -------------------------------------------------------
    @Data
    @Component("book")
    @Scope("prototype")
    public class Book {
    @Value("BS101")
    private String bid;
    @Value("java书籍")
    private String bname;
    }

    @Data @Repository("mm") @Scope("prototype")
    public class Member {
    @Resource(name="book") 此处的book 就是指定工厂中的 上的那book名@Component("book")
    相当于beans.xml 中的<property name="" ref="u"></property>
    private Book book;

    }

    @Data @Repository("mm") @Scope("prototype")
    public class Member {
    @Autowired 根据类型自动注入对象
    private Book book;
    }

    8)延迟加载
    -----------------------------------------------------------------
    单例模式是默认的,会立即加载 加上@Lazy 不自动加载,第一次使用时再加载
    @Data
    @Component @Scope("singletone") @Lazy
    public class Teacher {
    private String name;

    public Teacher() {
    System.out.println("空构造方法");
    }

    public Teacher(String name) {
    this.name = name;
    System.out.println("有参数name的构造方法");
    }
    public void info(){
    System.out.println("老师姓名:"+this.name);
    }
    }

    @Scope("prototype") 多实例,默认使用的使用时加载

    @Data @Repository("mm") @Scope("singleton") @Lazy 延迟加载,在第一次使用时加载
    public class Member {

    }

    @Data @Repository("mm") @Scope("singleton") @Lazy(false) 立即加载初始化
    public class Member {
    }

    beans.xml

    <beans default-lazy-init="true"> 所有bean都延迟实例化初始化加载

    <beans default-autowire="byName">默认注入相关属性对象

    怕什么真理无穷,进一步有一步的欢喜
  • 相关阅读:
    随意输入一串字符串,显示出现次数最多的字符,以及出现次数
    类似QQ的聊天工程
    Java基础语法
    Java安装环境
    创建纯洁的TableViewCell
    从gitlab下载好cocoapods中遇到的问题
    tableview隐藏多余分割线
    UIBarButtonItem变弹簧
    Dictionary中的结构体转出来
    tableviewcell不允许点击
  • 原文地址:https://www.cnblogs.com/Mkady/p/7201220.html
Copyright © 2020-2023  润新知