Spring(五)--autowire自动装配和spel
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的更多相关文章
- spring bean autowire自动装配
转自:http://blog.csdn.net/xiao_jun_0820/article/details/7233139 autowire="byName"会自动装配属性与Bea ...
- Spring系列7:`autowire`自动装配怎么玩
回顾 前几篇我们介绍各种依赖依赖注入,都是显式指定的,配置明确但同时也有些繁杂和重复."很多发明的出发点,都是为了偷懒,懒人是推动社会进步的原动力".Spring 提供了自动注入依 ...
- Spring(六)之自动装配
一.自动装配模型 下面是自动连接模式,可以用来指示Spring容器使用自动连接进行依赖注入.您可以使用元素的autowire属性为bean定义指定autowire模式. 可以使用 byType 或者 ...
- Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件
1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...
- Spring 由构造函数自动装配
Spring 由构造函数自动装配,这种模式与 byType 非常相似,但它应用于构造器参数. Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 ...
- 【面试普通人VS高手系列】Spring Boot中自动装配机制的原理
最近一个粉丝说,他面试了4个公司,有三个公司问他:"Spring Boot 中自动装配机制的原理" 他回答了,感觉没回答错误,但是怎么就没给offer呢? 对于这个问题,看看普通人 ...
- [转载]Spring Autowire自动装配介绍
转自: http://www.cnblogs.com/zhishan/p/3190757.html 在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型 ...
- Spring Autowire自动装配介绍
在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的< ...
- Spring Autowire自动装配
在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的< ...
随机推荐
- shiro框架学习-6-Shiro内置的Filter过滤器及数据加解密
1. shiro的核心过滤器定义在枚举类DefaultFilter 中,一共有11个 ,配置哪个路径对应哪个拦截器进行处理 // // Source code recreated from a .c ...
- mysql 递归查找所有子节点
select dept_id from ( select t1.dept_id,t1.parent_id, if(find_in_set(parent_id, @pids) > 0, @pids ...
- C# 父子窗体 传值
子窗体 public event Action<List<DataGridViewRow>> myEvent; myEvent(List); 子窗体调用 父窗体 创建对象 fr ...
- C++:关键字explicit的用法
预测下面C++程序的输出: #include <iostream> using namespace std; class Complex { private: double real; d ...
- 【IOI2018】组合动作
还是自己水平不够,想了两天没想出来--(然后我就被其他人吊打了) 这种题目看了题解就秒会,自己想就想不出来-- 下面是我的心路历程(我就在想出来又叉掉的不断循环中度过--) 开始把题目看成了查询限制 ...
- layui button按钮点击导致页面重新刷新的解决方案
网友的解决方法:(我只想说,放屁!!!而且大家都在复制粘贴,浪费时间) 方法一:将button标签更换为input <input class="layui-btn test" ...
- MQTT QOS含义
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- var $this = $(this)
jQuery: What’s the Difference Between $(this), $this, and this? What about $this? $this is a little ...
- spark MLlib 概念 4: 协同过滤(CF)
1. 定义 协同过滤(Collaborative Filtering)有狭义和广义两种意义: 广义协同过滤:对来源不同的数据,根据他们的共同点做过滤处理. Collaborative filterin ...
- AC自动机小记
不知不觉这篇博客已经被我咕咕咕了一个月了. 也许我写过AC自动机的博客,但我也不知道我写没写过 前情回顾之\(kmp\) \(kmp\)用来解决一个模式串匹配一个文本串的问题,其思路是设置失配指针,找 ...