zoukankan      html  css  js  c++  java
  • 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">默认注入相关属性对象

    怕什么真理无穷,进一步有一步的欢喜
  • 相关阅读:
    图像处理国际会议
    [2015更新]用Word2007写CSDN博客
    【超详细教程】使用Windows Live Writer 2012和Office Word 2013 发布文章到博客园全面总结
    奇异秀App:奇异秀秀奇异,用大头视频来拜年
    通俗讲解傅里叶级数
    LIBSVM的使用方法
    VC6.0的工程设置解读Project--Settings
    HOG:从理论到OpenCV实践
    如何在 Kaggle 首战中进入前 10%
    linux学习(2)
  • 原文地址:https://www.cnblogs.com/Mkady/p/7201220.html
Copyright © 2011-2022 走看看