Spring的Ioc和AOP扩展
多种方式实现依赖注入:
这里唯一需要说明的是如果要使用P命名空间实现属性注入,需要添加命名空间的声明:
如我的xml里红色字体:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/bean http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 设置注入(通过访问器注入) 1.使用灵活2.时效性不足 -->
<bean id="a" class="cn.cnti.ioc.A">
<property name="a1" value="a1 hello"></property>
<property name="a2">
<!-- 赋值为特殊字符时需要用 -->
<value><![CDATA[<><><><><><>]]></value>
</property>
<property name="a3" value="60"></property>
</bean>
<bean id="a1" class="cn.cnti.ioc.A">
<property name="a1">
<value>a1 hello</value>
</property>
<property name="a2">
<value>a2 hello</value>
</property>
<property name="a3">
<value>80</value>
</property>
</bean>
<!-- 通过构造注入 1.时效性好2.灵活性不足 -->
<bean id="a2" class="cn.cnti.ioc.A">
<constructor-arg index="0" value="a1 hello"></constructor-arg>
<constructor-arg index="1" value="a2 hello"></constructor-arg>
<constructor-arg index="2" value="98"></constructor-arg>
</bean>
<bean id="a3" class="cn.cnti.ioc.A">
<constructor-arg name="a1" value="a1 hello"></constructor-arg>
<constructor-arg name="a2" value="a2 hello"></constructor-arg>
<constructor-arg name="a3" value="99"></constructor-arg>
</bean>
<bean id="a4" class="cn.cnti.ioc.A">
<constructor-arg type="java.lang.String" value="a1 hello"></constructor-arg>
<constructor-arg type="java.lang.String" value="a2 hello"></constructor-arg>
<constructor-arg type="java.lang.Integer" value="100"></constructor-arg>
</bean>
<!-- 使用p命名空间的注入,首先需要添加p命名空间的声明 -->
<bean id="a5" class="cn.cnti.ioc.A" p:a1="a1 hello" p:a2="a2 hello"
p:a3="101" />
<!-- 注入集合类型的属性 -->
<bean id="a6" class="cn.cnti.ioc.A">
<!-- list集合 -->
<property name="list">
<list>
<value>hello1</value>
<value>hello2</value>
<value>hello3</value>
</list>
</property>
<property name="listB">
<list>
<ref bean="b" />
<ref bean="b" />
<ref bean="b" />
</list>
</property>
<!-- set集合 -->
<property name="set">
<set>
<value>he</value>
<value>hel</value>
<value>hello</value>
</set>
</property>
<property name="setB">
<set>
<ref bean="b" />
</set>
</property>
<!-- map集合 -->
<property name="map">
<map>
<entry>
<key>
<value>one</value>
</key>
<value>1</value>
</entry>
<entry>
<key>
<value>two</value>
</key>
<value>2</value>
</entry>
<entry>
<key>
<value>three</value>
</key>
<value>3</value>
</entry>
</map>
</property>
<property name="mapO">
<map>
<entry>
<key>
<value>one</value>
</key>
<ref bean="b" />
</entry>
<entry>
<key>
<value>two</value>
</key>
<ref bean="b" />
</entry>
<entry>
<key>
<value>three</value>
</key>
<ref bean="b" />
</entry>
</map>
</property>
</bean>
<bean id="b" class="cn.cnti.ioc.B" p:b="hello b"></bean>
<!-- map集合(集合里没有对象)的另一种简单用法 -->
<bean id="a7" class="cn.cnti.ioc.A">
<property name="map">
<props>
<prop key="one">he</prop>
<prop key="two">hel</prop>
<prop key="three">hello</prop>
</props>
</property>
</bean>
<!-- 使用内部bean -->
<bean id="a8" class="cn.cnti.ioc.A">
<property name="b">
<bean class="cn.cnti.ioc.B">
<property name="b" value="i am b"></property>
</bean>
</property>
</bean>
</beans>
我的实体类A:
package cn.cnti.ioc;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class A {
private String a1;
private String a2;
private Integer a3;
// 测试内部bean
private B b;
// 测试各种集合
List<String> list;
List<B> listB;
Set<String> set;
Set<B> setB;
Map<String, String> map;
Map<String, Object> mapO;
public A() {
}
public A(String a1, String a2, Integer a3) {
this.a1 = a1;
this.a2 = a2;
this.a3 = a3;
}
public String getA1() {
return a1;
}
public void setA1(String a1) {
this.a1 = a1;
}
public String getA2() {
return a2;
}
public void setA2(String a2) {
this.a2 = a2;
}
public Integer getA3() {
return a3;
}
public void setA3(Integer a3) {
this.a3 = a3;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public List<B> getListB() {
return listB;
}
public void setListB(List<B> listB) {
this.listB = listB;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Set<B> getSetB() {
return setB;
}
public void setSetB(Set<B> setB) {
this.setB = setB;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Map<String, Object> getMapO() {
return mapO;
}
public void setMapO(Map<String, Object> mapO) {
this.mapO = mapO;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
@Override
public String toString() {
return "A [a1=" + a1 + ", a2=" + a2 + ", a3=" + a3 + ", b=" + b
+ ", list=" + list + ", listB=" + listB + ", set=" + set
+ ", setB=" + setB + ", map=" + map + ", mapO=" + mapO + "]";
}
}
我的实体类B:
package cn.cnti.ioc;
public class B {
private String b;
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
@Override
public String toString() {
return "B [b=" + b + "]";
}
}
我的实体类C:
package cn.cnti.ioc;
public class C {
private String C;
public String getC() {
return C;
}
public void setC(String c) {
C = c;
}
@Override
public String toString() {
return "C [C=" + C + "]";
}
}
Spring的Ioc和AOP扩展的更多相关文章
- Spring的IOC和AOP之深剖
今天,既然讲到了Spring 的IOC和AOP,我们就必须要知道 Spring主要是两件事: 1.开发Bean:2.配置Bean.对于Spring框架来说,它要做的,就是根据配置文件来创建bean实例 ...
- Spring框架IOC和AOP介绍
说明:本文部分内容参考其他优秀博客后结合自己实战例子改编如下 Spring框架是个轻量级的Java EE框架.所谓轻量级,是指不依赖于容器就能运行的.Struts.Hibernate也是轻量级的. 轻 ...
- 展开说说,Spring Bean IOC、AOP 循环依赖
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 延迟满足能给你带来什么? 大学有四年时间,但几乎所有人都是临近毕业才发现找一份好工作 ...
- spring的IOC和AOP
spring的IOC和AOP 1.解释spring的ioc? 几种注入依赖的方式?spring的优点? IOC你就认为他是一个生产和管理bean的容器就行了,原来需要在调用类中new的东西,现在都是 ...
- Spring 的IOC和AOP总结
Spring 的IOC和AOP IOC 1.IOC 许多应用都是通过彼此间的相互合作来实现业务逻辑的,如类A要调用类B的方法,以前我们都是在类A中,通过自身new一个类B,然后在调用类B的方法,现在我 ...
- Spring中IOC和AOP的详细解释(转)
原文链接:Spring中IOC和AOP的详细解释 我们是在使用Spring框架的过程中,其实就是为了使用IOC,依赖注入,和AOP,面向切面编程,这两个是Spring的灵魂. 主要用到的设计模式有工厂 ...
- spring的IOC和AOP详细讲解
1.解释spring的ioc? 几种注入依赖的方式?spring的优点? IOC你就认为他是一个生产和管理bean的容器就行了,原来需要在调用类中new的东西,现在都是有这个IOC容器进行产生,同时, ...
- Spring 的IOC 和Aop
Spring 的IOC 和Aop
- # Spring 练习ioc 、aop
Spring 练习 通过学习spring的基础知识,了解了Spring为了降低Java开发的复杂性,采取了以下4种关键策略: 基于POJO的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: ...
随机推荐
- Linux学习总结
1.软链接和硬链接 ln 命令可用来创建硬链接或是符号链接.它的使用方式有两种. ln file link 用来创建硬链接 ln -s item link 用来创建符号链接,这里的item可以是文件也 ...
- Leetcode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 搭建centos测试环境:window安装xshell,WinSCP 。 centos安装jdk tomcat
通过ssh实现远程访问linux系统: 由于xshell 连接centos,需要centos开启ssh服务.所以先启动SSH服务,没有ssh需要先安装. 1 . 查看SSH是否安装命令:rpm -qa ...
- VS2013 - 自定义新建文件模版
一直想统一下项目中的关于-(新建文件时,添加个人信息,如:创建者,创建时间等个性化信息). 从网络上学习到方法很简单,只需要把IDE安装目录下的模板进行修改保存,即可每次创建拥有固定的模板呈现. 具体 ...
- 阿里云SLB双机IIS多站点负载均衡部署笔记
首先SLB是通过局域网与ECS链接 ECS1服务器 test文件夹增加index.html test1文件夹增加index.html 设置ECS1服务器(130)IIS test站点 设置test主机 ...
- idea intellij 混淆anroid代码
idea intellij 混淆anroid代码 在project.properties中加入 target=android-14proguard.config=proguard.cfg 点击 Bui ...
- css3动画效果
css3中的transform中有旋转,放缩,倾斜,平移的功能,分别对应的属性是:rotate,scale,skew,translate 旋转:(rotate) [css] view plaincop ...
- jQueryMobile引入文件后样式无法正常显示
jQueryMobile引入文件后样式无法正常显示解决方法: jQuery文件必须放在jQueryMobile文件之前 eg:
- Android自定义控件----RadioGroup实现APP首页底部Tab的切换
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- 批处理命令 BAT备份MySQL数据库
批处理命令 BAT备份MySQL数据库 作者: 字体:[增加 减小] 类型:转载 时间:2009-07-23我要评论 MySQL数据的备份工具也许有很多,在这我要给大家分享一下通过DOS批处理命令和M ...