Spring 基本配置部分

五、别名

于数据库和mybatis差不多;

 <alias name="UserServiceImpl" alias="service"/>

前面是名字,后面是别名;但是的话,还有一种的方法是在bean下直接可以写别名:

<bean id="UserServiceImpl" class="com.saxon.Service.UserServiceImpl" name="service2">
        <property name="userDao" ref="OralceUserImpl"/>
        <constructor-arg type="com.saxon.Dao.UserDao" ref="OralceUserImpl"/>
    </bean>

并且可以通过分隔符来进行一个分割,实现取出多个的别名的目的;

六、import

把多个配置文件合成一个,如果文件内容有完全一样的部分就会合并;

多个文件合并后,使用一个总文件就可以访问;

七、依赖注入(DI)

直接上代码:

pojo.student:

public class Student {
    private String name;
    private Address address;
    private Map<String,String> card;
    private String[] books;
    private List<String> teacher;
    private Set<String> games;
    private Properties info;
    private String wife;
   @Override
    public String toString () {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", card=" + card +
                ", books=" + Arrays.toString (books) +
                ", teacher=" + teacher +
                ", games=" + games +
                ", info=" + info +
                ", wife='" + wife + '\'' +
                '}';
    }
}
//setter getter 略,但是必须要写

applicationContext:

<bean id="student" class="com.saxon.pojo.Student">
        <!--            第一种 直接注入-->
        <property name="name" value="saxon"/>
        <!--            第二种 对象注入-->
        <property name="address" ref="address"/>
        <!--        第三种 数组-->
        <property name="books">
            <array>
                <value>关于我是如何变帅的那些事 1</value>
                <value>关于我是如何变帅的那些事 2</value>
                <value>关于我是如何变帅的那些事 3</value>
            </array>
        </property>
        <!--        map集合-->
        <property name="card">
            <map>
                <entry key="author" value="saxon"/>
                <entry key="author" value="saxon 2"/>
            </map>
        </property>
        <!--            list 集合-->
        <property name="teacher">
            <list>
                <value>李老师</value>
                <value>马老师</value>
            </list>
        </property>
        <!--        set 集合-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>The Honor OF KING</value>
            </set>
        </property>
        <!--        properties-->
        <property name="info">
            <props>
                <prop key="Lover">null</prop>
            </props>
        </property>
        <!--        关于空值的设置 null和""-->
        <property name="wife">
            <null/>
        </property>
    </bean>
    <bean id="address" class="com.saxon.pojo.Address">
        <property name="country" value="CHINA"/>
        <property name="province" value="YUNNAN"/>
    </bean>

显示出来的内容:

Student{name='saxon', address=Address{country='CHINA', province='YUNNAN'}, card={author=saxon 2}, books=[关于我是如何变帅的那些事 1, 关于我是如何变帅的那些事 2, 关于我是如何变帅的那些事 3], teacher=[李老师, 马老师], games=[LOL, The Honor OF KING], info={Lover=null}, wife='null'}

c命名空间和p命名空间:

这两个需要引入我们的配置;
p命名空间,使用setter注入:

xmlns:p="http://www.springframework.org/schema/p"
    <bean id="student" class="com.saxon.pojo.Student" p:name="saxon">

c命名空间,使用构造器注入:

xmlns:c="http://www.springframework.org/schema/c"
<bean id="student" class="com.saxon.pojo.Student" c:name="saxon">

七、bean的作用域

一共有6种,但是后面的四种是基于web应用的;

Scope Description
singleton (Default) Scopes a single bean definition to a single object instance for each Spring IoC container.
prototype Scopes a single bean definition to any number of object instances.
request Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
session Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
application Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
websocket Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.

测试代码:

 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext.xml");
        Student student1 = (Student)context.getBean ("student");
        Student student2 = (Student)context.getBean ("student");
        System.out.println (student1==student2);

1.sigleton:无论创建几个都始终只有一个并且是默认的作用域

  <bean id="student" class="com.saxon.pojo.Student" c:name="saxon" scope="singleton">

结果:true

2.prototype:每次创建都会新建一个对象,就是浪费资源

   <bean id="student" class="com.saxon.pojo.Student" c:name="saxon" scope="prototype">

结果:false;

八、自动装配

1.显示自动装配 xml 装配

applicationContext.xml

2.Java文件自动装配

3.隐式自动装配

autowrite:

1.autowire="byName"

它会自动寻找我们属性中还没有注入的属性,选取ID 为setXXX的XXX 自动装配

比如:

public void setAddress (Address address) {
        this.address = address;
    }
<bean id="address" class="com.saxon.pojo.Address">
        <property name="country" value="CHINA"/>
        <property name="province" value="YUNNAN"/>
 </bean>

这个的set后缀就是address,那么就去找ID为address的bean;如果ID不一致就不会自动装配;区分大小写,id唯一;

2.autowire="byType"

会自动寻找我们属性中还没有注入的属性,选取类的类型为setXXX的XXX 自动装配

public void setAddress (Address address) {
        this.address = address;
    }
//类就是address
<bean id="address" class="com.saxon.pojo.Address">
        <property name="country" value="CHINA"/>
        <property name="province" value="YUNNAN"/>
 </bean>

com.saxon.pojo.Address类型唯一自动装配,于id无关;id可以不写;
还有一种使用resource,Java原生的注入方式,那个默认使用的是ByName,在byName找不到的时候就去byType按照id找,他的方式与spring的自动装配的默认方式相反

自学总结
学习地址:狂神说Java

上一篇:设计模式随笔(三):单例模式


下一篇:SinGAN: Learning a Generative Model from a Single Natural Image - 1 - 论文学习