前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring表达式和自动装配

Spring表达式和自动装配

原创
作者头像
爱撒谎的男孩
发布2018-05-10 18:40:08
8663
发布2018-05-10 18:40:08
举报
文章被收录于专栏:码猿技术专栏

Spring表达式和自动装配

【重要】spring表达式

作用

  • 通过spring表达式可以在配置Y节点时,如果Y的某些属性需要注入值,可以是已经配置的好的X类的节点中的值
  • 直接使用#{id.属性名}

前提

  • 必须为每个属性都要设置set方法

实现

  • 新建两个类
  • Message
代码语言:javascript
复制
public class Message {
    private String name;
    private List<String> cities;   //城市 。List集合
    private Set<String> friend;   //Set集合
    private Map<Integer,String> bookes;  //Map集合
    private Properties properties;   //Properties集合
    private String[] names;
​
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String[] getNames() {
        return names;
    }
    public void setNames(String[] names) {
        this.names = names;
    }
    public List<String> getCities() {
        return cities;
    }
    public void setCities(List<String> cities) {
        this.cities = cities;
    }
    public Set<String> getFriend() {
        return friend;
    }
    public void setFriend(Set<String> friend) {
        this.friend = friend;
    }
    public Map<Integer, String> getBookes() {
        return bookes;
    }
    public void setBookes(Map<Integer, String> bookes) {
        this.bookes = bookes;
    }
    public Properties getProperties() {
        return properties;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}
  • ValueBean的类
代码语言:javascript
复制
public class ValueBean {
    private String username;
​
    public String getUsername() {
        return username;
    }
​
    public void setUsername(String username) {
        this.username = username;
    }
​
}
​
获取不是集合类型的值
  • 在sprig的配置文件中配置如下:
代码语言:javascript
复制
    <util:list id="cities">
        <value>徐州</value>
        <value>无锡</value>
        <value>常州</value>
    </util:list>
​
    <util:set id="friends">
        <value>Jack</value>
        <value>Tom</value>
        <value>陈加兵</value>
    </util:set>
​
​
    <util:map id="bookes">
        <entry key="1001" value="java编程基础"></entry>
        <entry key="1002" value="java编程思想"></entry>
    </util:map>
​
    <!-- 引入外部的Properties文件,location指定的就是位置 -->
    <util:properties id="properties" location="classpath:jdbc.properties"></util:properties>
​
    <bean id="message" class="cn.tedu.spring.beans.Message">
        <property name="name" value="陈加兵"></property>
        <!-- List集合的注入 ref指定的上面定义的List的id -->
        <property name="cities" ref="cities"></property>
​
        <!-- Set集合的注入 -->
        <property name="friend" ref="friends"></property>
​
        <!-- Map集合的注入 -->
        <property name="bookes" ref="bookes"></property>
​
        <!-- properties的集合的注入 -->
        <property name="properties" ref="properties"></property>
​
        <!-- 为数组赋值 -->
        <property name="names">
            <array>
                <value>Alex</value>
                <value>Billy</value>
            </array>
        </property>
    </bean>
​
    <!-- 配置ValueBean -->
    <bean id="valueBean" class="cn.tedu.spring.beans.ValueBean">
        <!-- value的值是使用spring表达式获取的,形式为:#{前面定义好的id.属性名} -->
        <property name="username" value="#{message.name}"></property>
    </bean>
引用数组集合或者List的值
  • 直接使用 #{bean的id.数组名[index]}
代码语言:javascript
复制
    <!-- 配置ValueBean -->
    <bean id="valueBean" class="cn.tedu.spring.beans.ValueBean">
        <!-- value的值是使用spring表达式获取的,形式为:#{前面定义好的id.属性名} -->
        <property name="username" value="#{message.names[0]}"></property>
    </bean>
ValueBean的name属性设置为Person中的Address对象的city值
  • #{person.address.city}
  • Person类中有一个成员变量是Address类的对象
  • 这里不再写这三个类了,直接在spring中配置
代码语言:javascript
复制
<!-- 创建一个Address的实例 -->
    <bean id="address" class="cn.tedu.spring.beans.Address">
        <property name="city" value="无锡"></property>
        <property name="pro" value="江苏"></property>
    </bean>
​
    <bean id="person" class="cn.tedu.spring.beans.Person">
        <property name="name" value="陈加兵"></property>
        <property name="age" value="22"></property>
        <!-- 引用上面address -->
        <property name="address" ref="address"></property>
    </bean>
​
    <!-- 配置ValueBean -->
    <bean id="valueBean" class="cn.tedu.spring.beans.ValueBean">
        <!-- value的值是使用spring表达式获取的,形式为:#{前面定义好的id.属性名} -->
        <property name="username" value="#{person.address.city}"></property>
    </bean>
引用Map集合中的某个value
  1. #{id.Map名称.key名称}
  2. #{id.Map名称['key名称']}

【了解】spring表达式支持方法的调用

【了解】 自动装装配(autowire)

  • <bean id="" class="" autowire="">
  • 自动装配表现为不需要配置<property>节点来注入,spring会自动的为属性注入值
  • bean节点中添加autowire属性以配置自动装配,当值为byName,表示根据名称自动装配,即spring会检查这个bean的所有属性名称,然后在搜平日那个管理的所有Bean中查找bean-id一致的Bean对象,如果找到,则自动赋值
  • 当取值为byType时,表示根据类型自动装配,及自动化赋值的标准是找数据类型匹配的Bean对象,需要注意的是:如果根据类型装配,必须保证可以匹配上的,由spring自动管理的Bean只有一个,如果有2个或者更多,会导致异常

实例

  • UserDao
代码语言:javascript
复制
public class UserDao(){
    public void reg(){
​
}
}
  • UserService
代码语言:javascript
复制
public class UserService{
    private UserDao userDao;
    private void reg(){
        userDao.reg();
}
}
​
  • spring 的配置文件
代码语言:javascript
复制
<bean id="userDao" class="cn.tedu.spring.UserDao"/>
​
<!--这里不需要配置property节点来ref上面定义的bean,只需要使用自动装配即可-->
<bean id="userService" class="cn.tedu.spring.UserService" autowire="byName">

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Spring表达式和自动装配
    • 【重要】spring表达式
      • 作用
      • 前提
      • 实现
    • 【了解】spring表达式支持方法的调用
      • 【了解】 自动装装配(autowire)
        • 实例
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档