漠北空城

听海观潮


  • 首页

  • 关于

  • 标签

  • 归档

  • 搜索

注解

发表于 2017-11-22

注解

1、起一个标志的作用

2、注解不能单独存在

3、注解使用的位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public enum ElementType {
// 类与接口
TYPE,
// 属性
FIELD,
// 方法
METHOD,
// 参数
PARAMETER,
// 构造器
CONSTRUCTOR,
// 局部变量
LOCAL_VARIABLE,
// 注解类型Annotation type declaration
ANNOTATION_TYPE,
// 包类型 Package declaration
PACKAGE
}
阅读全文 »

spring DI-setter注入

发表于 2017-11-21

spring DI-setter注入

1、对象定义

Student类和Person类:

1
2
3
4
5
6
7
8
9
10
11
12
public class Student{

}
public class Person{
private Long pid;
private String name;
private Student student;
private List list;
private Set set;
private Map map;
private Properties properties;
}

2、装置属性配置

装载属性配置如下:

  • 基本属性
  • list
  • set
  • map
  • object
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    <bean id="student" class="..." scope="prototype"></bean>
    <bean id="person" class="...">
    <!--
    是用来描述Person的属性
    name为属性的名称
    value就给name的属性赋值
    基本属性可以这样赋值:
    String类型和基本类型(数据类型)
    ref是给引用类型赋值的
    <property name="student" ref="student"/>
    <property name="student">
    <ref bean="student"/>
    </property>
    -->
    <property name="pid" value="1"></property>
    <property name="name" value="王二麻子"></property>
    <property name="student">
    <ref bean="student"/>
    </property>
    <property name="list">
    <list>
    <value>list1</value>
    <value>list2</value>
    <ref bean="student"/>
    </list>
    </property>
    <property name="set">
    <set>
    <value>set1</value>
    <value>set2</value>
    <ref bean="student"/>
    </set>
    </property>
    <property name="map">
    <map>
    <entry key="map1">
    <value>map1</value>
    </entry>
    <entry key="map2">
    <ref bean="student"/>
    </entry>
    </map>
    </property>
    <property name="properties">
    <props>
    <prop key="p1">afd</prop>
    <prop key="p2">p2</prop>
    </props>
    </property>
    </bean>

注意:
如果在student对象scope对象配置为prototype,在student类的构造方法中写入输出student语句,将会打印出4个student。
第一个因为有ref引用,spring将对象创建提前了,创建了一个student对象;
后面三个是一个ref创建一个student对象;

Spring DI简介

发表于 2017-11-21

Spring DI简介

DI:依赖注入
概念:给属性赋值

  • 1、调用set方法
    基础类型
    引用类型
  • 2、调用构造器
  • 3、利用注解的形式
public class Student{

}    
public class Person{
  private Long pid;
  private String name;
  private Student student;
  private List list;
  private Set set;
  private Map map;
  private Properties properties;
}

步骤:

  • 1、创建一个Student类
  • 2、创建一个Person类
  • 3、创建一个spring的配置文件,把student类和person类 放入到spring容器中
  • 4、装配各个属性

执行流程:

  • 1、启动spring容器
  • 2、创建依赖对象(Student对象)
  • 3、创建Person对象
  • 4、给person对象中的属性进行装配的操作

    注意:
    如果把Student的配置方式改成scope=”prototype”,
    因为其他的类的创建和属性的装配的过程全部发生在spring容器启动的时候,所以Student对象的创建被提前了
    ,但是还是用一次即创建一次对象

Spring IOC创建对象配置初始化方法和销毁方法

发表于 2017-11-21

Spring IOC创建对象配置初始化方法和销毁方法

1、创建一个类

public class HelloWorld {
    public HelloWorld(){
        System.out.println("hello world");
    }

    public void init(){
        System.out.println("init");
    }


    public void destroy(){
        System.out.println("destroy");
    }

    public void hello(){
        System.out.println("hello");
    }
}

2、spring配置文件中

<bean id="helloWorld" 
      class="com.ithiema07.spring.ioc.initdestroy.HelloWorld"
      init-method="init"
      destroy-method="destroy"></bean>

3、执行顺序:

  • 1、启动spring容器
  • 2、创建对象
  • 3、执行init方法
    该方法是由spring容器内部调用的
  • 4、执行destroy方法
    该方法是由spring容器内部调用的
    前提:在spring容器关闭的时候执行,仅单例
    注意点:如果一个bean是多例,则spring容器不负责销毁

    4、到目前为止spring容器的执行顺序:

  • 1、启动spring容器

  • 2、如果该bean为单例,并且lazy-init为”default/false”,这个时候为该bean创建对象
  • 3、执行该bean的init方法
  • 4、在客户端利用context.getBean得到该对象
    如果是多例,或者lazy-init为”true”,则在这步创建对象
  • 5、在客户端对象调用方法
  • 6、当spring容器关闭的时候,如果该bean是单例,则调用destroy方法

Spring IOC产生对象的时机

发表于 2017-11-20

Spring IOC产生对象的时机

1、配置文件如下:

1
2
<bean id="helloWorld" class="..."  ></bean>
<bean id="helloWorld2" class="..." ></bean>

说明:
1、只要声明一个bean,spring容器就会为之创建对象,不管class是否相同
2、在默认情况下,在启动spring容器的时候创建对象
3、把service层和dao层所有的类放入spring容器中,在启动spring容器的时候创建对象
4、可以过早的检查发现错误
5、缺点:如果该类中有属性,并且属性为一个集合,那么在创建对象的时候,集合中如果有数据了,那么该数据会过早的加入到内存中

为了避免上述情况的发生,尽量把数据放在局部变量中
(通过代理来做,需要的时候再加载)

1
2
3
4
5
6
7
8
9
10
@Component
public class SystemProperties{

private Properties properties;

@PostConstruct
public void initProperties(){
properties = ...;
}
}

系统初始化的时候,加载数据库系统配置文件到Properties类中

2、配置延迟加载:

1
<bean id="helloWorld" class="..."  lazy-init="true"></bean>

default:默认=否
false:否,spring启动时创建对象
ture:是,调用时才创建对象

在多例的情况下,不论是否设置lazy-init,都是在调用的时候才创建对象

1
<bean id="helloWorld" class="..."  scope="prototype" lazy-init="ture"></bean>

1…345…14
漠北空城

漠北空城

69 日志
19 标签
链接
  • xyz327
© 2024 漠北空城
由 Hexo 强力驱动
|
主题 — NexT.Gemini v5.1.4
粤ICP备18054530号-2   |     |