首页 > 代码库 > Springd的Ioc和AOP扩展
Springd的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="http://www.mamicode.com/a1 hello"></property>
<property name="a2">
<!-- 赋值为特殊字符时需要用 -->
<value><![CDATA[<><><><><><>]]></value>
</property>
<property name="a3" value="http://www.mamicode.com/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="http://www.mamicode.com/a1 hello"></constructor-arg>
<constructor-arg index="1" value="http://www.mamicode.com/a2 hello"></constructor-arg>
<constructor-arg index="2" value="http://www.mamicode.com/98"></constructor-arg>
</bean>
<bean id="a3" class="cn.cnti.ioc.A">
<constructor-arg name="a1" value="http://www.mamicode.com/a1 hello"></constructor-arg>
<constructor-arg name="a2" value="http://www.mamicode.com/a2 hello"></constructor-arg>
<constructor-arg name="a3" value="http://www.mamicode.com/99"></constructor-arg>
</bean>
<bean id="a4" class="cn.cnti.ioc.A">
<constructor-arg type="java.lang.String" value="http://www.mamicode.com/a1 hello"></constructor-arg>
<constructor-arg type="java.lang.String" value="http://www.mamicode.com/a2 hello"></constructor-arg>
<constructor-arg type="java.lang.Integer" value="http://www.mamicode.com/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="http://www.mamicode.com/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 + "]";
}
}
Springd的Ioc和AOP扩展