Spring的配置:applicationContext.xml常规配置及依赖注入详解、Bean的作用域

1.起别名

alias 设置别名 , 为bean设置别名 , 可以设置多个别名

<!--设置别名:在获取Bean的时候可以使用别名获取--> 
<alias name="userT" alias="userNew"/>

2.Bean的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--使用Spring来创建对象,在Spring这些都称为Bean

        类型 变量名  = new 类型();
        Hello hello = new Hello();

        id = 变量名
        class = new 的对象;
        property 相当于给对象中的属性设置一个值!
        -->
        <bean id="hello" class="com.loey.Hello">
            <property name="str" value="mySpring"/>
        </bean>
</beans>

3. import

团队的合作通过import来实现 .

<import resource="{path}/beans.xml"/>

4.依赖注入(DI)

  • 依赖注入(Dependency Injection,DI)。
  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .

1、构造器注入

1.通过无参构造方法来创建

1. User.java

public class User {
    private String name;

2. beans.xml

<bean id="user" class="com.loey.User">        
    <property name="name" value="kuangshen"/>    
</bean>

3. 测试类

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

UserService userService = (UserService) context.getBean("userServiceImpl");

userService.getUser();

结果可以发现,在调用getUser方法之前,User对象已经通过无参构造初始化了

2.通过有参构造方法来创建

beans.xml 有三种方式编写

<!--    第一种根据index参数下标设置-->
    <bean id="user" class="com.loey.User">   
<!--      index指构造方法 , 下标从0开始 -->
        <constructor-arg index="0" value="pcy"/>
    </bean>

  <!-- 第二种根据参数名字设置 -->
  <bean id="user1" class="com.loey.User">
    <!-- name指参数名 -->
    <constructor-arg name="name" value="pcy2"/>
  </bean>

  <!-- 第种根据参数类型设置 -->
  <bean id="user2" class="com.loey.User">
    <constructor-arg type="java.lang.String" value="pcy3"/>
  </bean>

结论:在配置文件加载的时候。其中管理的对象都已经初始化了!

2 set注入 (重点)

要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is .
测试pojo类 :
Address.java

public class Address {

    private String address;
Student.java
public class Student {

    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

1、常量注入

<bean id="address" class="com.loey.pojo.Address" scope="prototype">
    <property name="address" value="hanguo"></property>
</bean>

2、Bean注入

注意点:这里的值是一个引用,ref

<bean id="address" class="com.loey.pojo.Address" scope="prototype">
    <property name="address" value="hanguo"></property>
</bean>

<bean id="student" class="com.loey.pojo.Student">

    <property name="name" value="pcy"></property>

    <property name="address" ref="address"></property>
</bean>

3、数组注入

<bean id="student" class="com.loey.pojo.Student">

    <property name="name" value="pcy"></property>
    <property name="address" ref="address"></property>
<property name="books">
    		<array>
       		 <value>1</value>
       		 <value>2</value>
        		<value>3</value>
    		</array>
	</property>
</bean>

4、List注入

<property name="hobbys">
    <list>
        <value>a</value>
        <value>b</value>
        <value>c</value>
        <value>d</value>
    </list>
</property>

5、Map注入

<property name="card">
    <map>
        <entry key="1" value="11"/>
        <entry key="2" value="22"/>
        <entry key="3" value="33"/>
    </map>
</property>

6、set注入

<property name="games">
    <set>
        <value>aa</value>
        <value>bb</value>
    </set>
</property>

7、Null注入

<property name="wife">
    <null/>
</property>

8、Properties注入

<property name="info">
    <props>
        <prop key="url">com.mysql.jdbc.Driver</prop>
        <prop key="username">url</prop>
        <prop key="password">1127</prop>
    </props>
</property>

3 拓展注入实现

User.java : 【注意:这里必须要有参构造器!】

1、P命名空间注入 : 需要在头文件中加约束文件

xmlns:p=“http://www.springframework.org/schema/p”

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

<!--    p命名-->
    <bean id="stu" class="com.loey.pojo.Student" p:name="pcy" p:address-ref="address" p:books="1,2,3"/>

2、c 命名空间注入 : 需要在头文件中加约束文件

xmlns:c="http://www.springframework.org/schema/c"
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

<!--    c命名-->
    <bean id="stu1" class="com.loey.pojo.Student" c:name="pcyc" c:address-ref="address"/>

发现问题:爆红了,刚才我们没有写有参构造!
解决:把有参构造器加上,这里也能知道,c 就是所谓的构造器注入

5. Bean的作用域

在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。简单地讲, bean就是由IoC容器初始化、装配及管理的对象 .
在这里插入图片描述
几种作用域中,request、session作用域仅在基于web的应用中使用(不必关心你所采用的是什么web 应用框架),只能用在基于web的Spring ApplicationContext环境
1 Singleton
当一个bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对 bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。Singleton是单例类型,就是 在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象 都是同一个对象。 注意,Singleton作用域是Spring中的缺省作用域。要在XML中将bean定义成 singleton,可以这样配置

<bean id="ServiceImpl" class="cn.csdn.service.ServiceImpl" scope="singleton">

2 Prototype
当一个bean的作用域为Prototype,表示一个bean定义对应多个对象实例。Prototype作用域的bean会 导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法) 时都会创建一个新的bean实例。Prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。在 XML中将bean定义成prototype,可以这样配置:

<bean id="account" class="com.foo.DefaultAccount" scope="prototype"/> 
  或者 
<bean id="account" class="com.foo.DefaultAccount" singleton="false"/>

3 Request
当一个bean的作用域为Request,表示在一次HTTP请求中,一个bean定义对应一个实例; 即每个HTTP 请求都会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。考虑下面bean定义:

<bean id="loginAction" class="cn.csdn.LoginAction" scope="request"/>

针对每次HTTP请求,Spring容器会根据loginAction bean的定义创建一个全新的LoginAction bean实 例,且该loginAction bean实例仅在当前HTTP request内有效,因此可以根据需要放心的更改所建实例 的内部状态,而其他请求中根据loginAction bean定义创建的实例,将不会看到这些特定于某个请求的 状态变化。当处理请求结束,request作用域的bean实例将被销毁。
4 Session
==当一个bean的作用域为Session,表示在一个HTTP Session中,一个bean定义对应一个实例。该作用域 仅在基于web的Spring ApplicationContext情形下有效。==考虑下面bean定义

<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>

针对某个HTTP Session,Spring容器会根据userPreferences bean定义创建一个全新的 userPreferences bean实例,且该userPreferences bean仅在当前HTTP Session内有效。与request作 用域一样,可以根据需要放心的更改所创建实例的内部状态,而别的HTTP Session中根据 userPreferences创建的实例,将不会看到这些特定于某个HTTP Session的状态变化。当HTTP Session 终被废弃的时候,在该HTTP Session作用域内的bean也会被废弃掉。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值