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注入它依赖的对象.但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的< ...
随机推荐
- 套接字之msghdr结构
用户端在使用sendmsg/recvmsg发送或者接收数据时,会使用msghdr来构造消息,其对应的内核结构为user_msghdr:其中msg_iov向量指向了多个数据区,msg_iovlen标识了 ...
- js中数组的经典特性
数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限,是长 ...
- 【Spark机器学习速成宝典】模型篇01支持向量机【SVM】(Python版)
目录 支持向量机原理 支持向量机代码(Spark Python) 支持向量机原理 详见博文:http://www.cnblogs.com/itmorn/p/8011587.html 返回目录 支持向量 ...
- Springboot2.x使用redis作为缓存
一.Springboot2.x关于配置redis作为缓存. 基本配置如下: (1)在application.properties文件中 spring.redis.database=2 //第几个数据库 ...
- __doPostBack function
__doPostBack function Hi everyone. Today I am going to talk about the __doPostBack function, because ...
- shiro 注解不生效
springboot集成shiro时,使用@RequiresRoles注解不生效. shiro @RequiresRoles底层使用springaop技术: 1.@RequiresRoles修饰ser ...
- leetcode-easy-listnode-237 Delete Node in a Linked List
mycode # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # sel ...
- leetcode-easy-string- 8 String to Integer (atoi)
mycode 98.26% 易错点: while循环式,and判断的地方先判断下标会不会超出范围 class Solution(object): def myAtoi(self, str): &qu ...
- Charles解析https,unknown错误解决办法
之前一直使用Charles,抓包https的没有问题,enable ssl的时候出现unknown消息,disable ssl的时候出现乱码,解决办法: 在设置–>通用–>关于本机–> ...
- Xmanager power suit 6 最新版注册激活附注册机
转自:https://blog.csdn.net/the_liang/article/details/82708907 Xmanager Power Suit 6.0.0109 最新版注册激活,长期更 ...