autowire自动装配和spel

1。需要的实体类

2。需要的配置文件

<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--自动装配autowire
01.byName: spring容器会根据实体类中的属性名称,去寻找有没有bean的id是属性名称的
如果有则自动注入 *****
02.byType: spring容器会根据实体类中的属性的类型,去寻找有没有bean的类型(class)相同的
如果有则自动注入 之后使用注解替换autowire
01.@Resource *****
02.@Autowired
-->
<!--配置人类bean-->
<bean id="person" class="com.xdf.Person" autowire="byName">
<property name="name" value="小黑"/>
<property name="age" value="13"/>
<!-- <property name="dog" ref="dog"/>-->
</bean> <!--配置Dog对应的bean-->
<bean id="dog" class="com.xdf.Dog">
<property name="name" value="小黑狗1"/>
<property name="age" value="1"/>
</bean>
<!--验证 byType 不能出现超过1个的相同类型-->
<bean id="dog2" class="com.xdf.Dog">
<property name="name" value="小黑狗2"/>
<property name="age" value="2"/>
</bean>
<!--
Spring el 是 spring3.0之后出现的!
-->
<bean id="person2" class="com.xdf.Person" autowire="byName">
<property name="name" value="#{person.name}"/>
<property name="age" value="#{person.age>15?18:12}"/>
</bean> </beans>

3。测试类

public class PersonDemo {

    @Test
public void test1(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Person person=context.getBean("person",Person.class);
System.out.println(person);
}
@Test
public void test2(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Person person=context.getBean("person2",Person.class);
System.out.println(person);
} @Test
public void test3(){
//创建el表达式对象
ExpressionParser parser=new SpelExpressionParser();
Date date=new Date(); //获取当前日期的年份
int year= parser.parseExpression("year").getValue(date,int.class);
System.out.println(year+1900);
//获取int类型最大值
System.out.println(Integer.MAX_VALUE);
int max=parser.parseExpression("T(java.lang.Integer).MAX_VALUE").getValue(int.class);
System.out.println(max);
//求数字之和
int sum=parser.parseExpression("1+2+3*5").getValue(int.class);
System.out.println(sum);
//逻辑运算符 和 boolean值
boolean value =parser.parseExpression("1>2 or 2<3").getValue(Boolean.class);
System.out.println(value);
}
}

  未完待续!!!

Spring(五)--autowire自动装配和spel的更多相关文章

  1. spring bean autowire自动装配

    转自:http://blog.csdn.net/xiao_jun_0820/article/details/7233139 autowire="byName"会自动装配属性与Bea ...

  2. Spring系列7:`autowire`自动装配怎么玩

    回顾 前几篇我们介绍各种依赖依赖注入,都是显式指定的,配置明确但同时也有些繁杂和重复."很多发明的出发点,都是为了偷懒,懒人是推动社会进步的原动力".Spring 提供了自动注入依 ...

  3. Spring(六)之自动装配

    一.自动装配模型 下面是自动连接模式,可以用来指示Spring容器使用自动连接进行依赖注入.您可以使用元素的autowire属性为bean定义指定autowire模式. 可以使用 byType 或者  ...

  4. Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...

  5. Spring 由构造函数自动装配

    Spring 由构造函数自动装配,这种模式与 byType 非常相似,但它应用于构造器参数. Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 ...

  6. 【面试普通人VS高手系列】Spring Boot中自动装配机制的原理

    最近一个粉丝说,他面试了4个公司,有三个公司问他:"Spring Boot 中自动装配机制的原理" 他回答了,感觉没回答错误,但是怎么就没给offer呢? 对于这个问题,看看普通人 ...

  7. [转载]Spring Autowire自动装配介绍

    转自: http://www.cnblogs.com/zhishan/p/3190757.html 在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型 ...

  8. Spring Autowire自动装配介绍

    在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的< ...

  9. Spring Autowire自动装配

    在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的< ...

随机推荐

  1. 文件操作:fopen()

    r 打开只读文件,该文件必须存在. r+ 打开可读写的文件,该文件必须存在.   rb+ 读写打开一个二进制文件,只允许读写数据. rt+ 读写打开一个文本文件,允许读和写.    w 打开只写文件, ...

  2. Springboot 注册拦截器

    拦截器 创建myInterceptor类 import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSer ...

  3. 轮播图和xadmin后台管理

    一.数据库设计 轮播图 1.安装依赖 pip install Pillow 2.模型类:home/models.py class Banner(models.Model): ""& ...

  4. Mybatis笔记总结

    第一.Mybatis介绍 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改 ...

  5. 解决zbx的web界面zabbix服务器端运行中 显示为不(启动命令)

    zabbix装完,发现server和agent服务都起来了,端口监听了,但是web界面zabbix服务器端运行中为 不 解决: 打开浏览器,到zabbix的setup.php界面 一般输入 ip/za ...

  6. docker容器的学习

    什么是docker   Docker 最初是 dotCloud 公司创始人 Solomon Hykes 在法国期间发起的一个公司内部项目,于 2013 年 3 月以 Apache 2.0 授权协议开源 ...

  7. 微信小程序底部菜单栏的使用方法

    1.找到项目根目录的配置文件 app.json,在配置文件中加入配置代码.例如: "tabBar": { <!--底部的导航配置属性--> "color&qu ...

  8. LeetCode 39. 组合总和(Combination Sum)

    题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限 ...

  9. 二、PHP链接mongodb

    <?php $db=new Mongo("mongodb://sa:sa@localhost:27017"); $c=$db->selectDB("TestD ...

  10. leetcode-easy-string-242. Valid Anagram

    mycode   71.97% class Solution(object): def isAnagram(self, s, t): """ :type s: str : ...