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. Mybatis-Plus的BaseMapper的用法

    1.如何使用BaseMapper进行数据库的操作. 2.使用BaseMapper进行插入实体时如何让UUID的主键自动生成. Student实体类,其中id属性主键为UUID package com. ...

  2. 获取URL值带参数跳转

    //要跳转界面 api.openWin({ name: 'PayOrder', url: 'PayOrder.html?id=2', pageParam:{name:pr} }); //跳转后的界面 ...

  3. R_Studio(学生成绩)绘制频率分布直方图、分布饼图、折线比较图

    对“Gary.csv”中的成绩数据进行分布分析 (1)按0-59,60-69,70-79,80-89,90-100分组绘制高级语言程序设计成绩的频率分布直方图. (2)按0-59,60-69,70-7 ...

  4. Java容器——Set和顺序存储

    当Set使用自己创建的类型时,存储的顺序如何维护,在不同的Set实现中会有不同,而且它们对于在特定的Set中放置的元素类型也有不同的要求: Set(interface) 存入Set的每个元素都必须是唯 ...

  5. 重温IOC,DI的理解

    IOC和DI其实它们是同一个概念的不同角度描述 IOC强调的是程序控制对象(创建销毁),变换成了容器来控制对象(创建销毁) DI:即IoC容器帮对象找相应的依赖对象通过反射注入     从Spring ...

  6. 02.替换空格 (Java)

    题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 思路 感觉这题对于Java意义 ...

  7. TODO: Java虚拟机 初始化过程

    Java虚拟机 初始化过程: 参考: https://www.cnblogs.com/bhlsheji/p/4017816.html 参考:https://blog.csdn.net/boling_c ...

  8. Point-wise Mutual Information

    Point-wise Mutual Information (Yao, et al 2019) reclaimed a clear description of Point-wise Mutual I ...

  9. (WPF)360安全卫士界面设计

    点击下载 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&qu ...

  10. java下载文件时文件名出现乱码的解决办法

    转: java下载文件时文件名出现乱码的解决办法 2018年01月12日 15:43:32 橙子橙 阅读数:6249   java下载文件时文件名出现乱码的解决办法: String userAgent ...