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. Qt:路径问题小结

    在做Qt项目的时候,我们难免遇到到文件路径问题. 如QFile file("text.txt")加载不成功.QPixmap("../text.png") 加载图 ...

  2. pip安装源和虚拟环境的搭建

    一.pip安装源 1.介绍 采用国内源,加速下载模块的速度 常用pip源: 豆瓣:https://pypi.douban.com/simple 阿里:https://mirrors.aliyun.co ...

  3. A. Blackjack

    A. Blackjack time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. Redis大 key的发现与删除方法全解析

    个推作为国内第三方推送市场的早期进入者,专注于为开发者提供高效稳定的推送服务,经过9年的积累和发展,服务了包括新浪.滴滴在内的数十万APP.由于我们推送业务对并发量.速度要求很高,为此,我们选择了高性 ...

  5. windos下hosts文件

    win+R c:\windows\system32\drivers\etc

  6. Demo:servlet实现图片的上传

    一个简单的servlet例子,实现图片的上传功能,上传的图片给 ?HttpServletResponse 对象 public class BackGroundLogoServlet extends H ...

  7. C++入门经典-例6.2-将二维数组进行行列对换

    1:一维数组的初始化有两种,一种是单个逐一赋值,一种是使用聚合方式赋值.聚合方式的例子如下: int a[3]={1,2,3}; int a[]={1,2,3};//编译器能够获得数组元素的个数 in ...

  8. DAY 3模拟赛

    DAY3 钟皓曦来了! T1 网址压缩 [问题描述] 你是能看到第一题的 friends 呢.           ——hja 众所周知,小葱同学擅长计算,尤其擅长计算组合数,但这个题和组合数没什么关 ...

  9. 统计学_F分布(图文详解和python脚本实现)

    python机器学习-乳腺癌细胞挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...

  10. MyBatis如何传入多个参数

    一.单个参数 mapper public List<Test> getTestList(String id); xml <select id = "getTestList& ...