构造注入
<!--2.构造注入--> <bean id="stu" class="cn.happy.XMLDI.Student"> <constructor-arg name="name" value="meme"></constructor-arg> <constructor-arg index="1" value="18"></constructor-arg> <constructor-arg index="2" ref="car"></constructor-arg> </bean>
<!--4.集合注入Array--> <bean id="array" class="cn.happy.XMLDI.MyCollection"> <property name="array"> <array> <value>太阳</value> <value>权萌萌 </value> <value>top</value> </array> </property> </bean> <!--list--> <bean id="list" class="cn.happy.XMLDI.MyCollection"> <property name="list"> <list> <value>你</value> <value>我</value> <value>他</value> </list> </property> </bean> <!--set--> <bean id="set" class="cn.happy.XMLDI.MyCollection"> <property name="set"> <set> <value>么</value> <value>哈</value> <value>我</value> </set> </property> </bean> <!-- Map--> <bean id="map" class="cn.happy.XMLDI.MyCollection"> <property name="map"> <map> <entry key="001" value="hh"></entry> <entry key="002" value="kokoko"></entry> <entry key="003"> <value>jhjhjh</value> </entry> </map> </property> </bean>
2.基于注解的DI
注解:@Override 标识这个方法一定来源于父类
@Test 单元测试方法
@Before
@After
注解Spring和JDK注解
1.@Component 组件,用于取代<beanid="" class="">
1.1引入
spring-aop-4.2.0.RELEASE.jar
引入aopjar包是因为注解用到了aop的内容
1.2 添加xsd约束
context节点可以使用
<?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" 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">
1.3 配置包扫描器
因为只有配置了包扫描器,该包以及该包下的子包中的类才可以被Spring扫描。
去寻找被注解的类和属性,让Spring容器,管理赋值
component:组件
一个bean,就是一个组件
scan:浏览
<context:component-scan base-package=""></context:component-scan>